email sending module
This commit is contained in:
		
							parent
							
								
									efffae848a
								
							
						
					
					
						commit
						5081305229
					
				
							
								
								
									
										40
									
								
								sourcing/mail.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								sourcing/mail.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,40 @@
 | 
			
		|||
from flask import render_template, current_app
 | 
			
		||||
from email.mime.text import MIMEText
 | 
			
		||||
from email.utils import formatdate, make_msgid
 | 
			
		||||
from email import charset
 | 
			
		||||
from email.utils import formataddr
 | 
			
		||||
import smtplib
 | 
			
		||||
 | 
			
		||||
charset.add_charset('utf-8', charset.SHORTEST, charset.QP)
 | 
			
		||||
 | 
			
		||||
def format_message(user, subject, body):
 | 
			
		||||
    from_name = current_app.config['FROM_NAME']
 | 
			
		||||
    from_addr = current_app.config['FROM_ADDR']
 | 
			
		||||
 | 
			
		||||
    msg = MIMEText(body, 'plain', 'UTF-8')
 | 
			
		||||
    msg['Subject'] = subject
 | 
			
		||||
    msg['To'] = formataddr((user.mail_to_name, user.email))
 | 
			
		||||
    msg['From'] = formataddr((from_name, from_addr))
 | 
			
		||||
    msg['Date'] = formatdate()
 | 
			
		||||
    msg['Message-ID'] = make_msgid()
 | 
			
		||||
 | 
			
		||||
    return msg
 | 
			
		||||
 | 
			
		||||
def send_mail(user, subject, body):
 | 
			
		||||
    bounce_addr = current_app.config['FROM_ADDR']
 | 
			
		||||
 | 
			
		||||
    msg = format_message(user, subject, body)
 | 
			
		||||
 | 
			
		||||
    msg_as_string = msg.as_string()
 | 
			
		||||
 | 
			
		||||
    if not current_app.config['REALLY_SEND_MAIL']:  # during development
 | 
			
		||||
        return
 | 
			
		||||
    s = smtplib.SMTP('localhost')
 | 
			
		||||
    s.sendmail(bounce_addr, [user.email], msg_as_string)
 | 
			
		||||
    s.quit()
 | 
			
		||||
 | 
			
		||||
def send_signup_mail(user):
 | 
			
		||||
    ''' unused so far '''
 | 
			
		||||
    subject = u'xanadu: verify your account'
 | 
			
		||||
    body = render_template('mail/signup.txt', user=user)
 | 
			
		||||
    send_mail(user, subject, body)
 | 
			
		||||
		Loading…
	
		Reference in a new issue