Посилаємо листа через Gmail

Найкращий варіант - юнікод і smtplib
GMAIL_SMTP_SERVER = 'smtp.gmail.com'
GMAIL_SMTP_PORT = 587
GMAIL_USERNAME = 'gmailusername'
GMAIL_PASSWORD = 'gmailpassword'
import smtplib
from email import Charset
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

subject = u'тестовий сабжект'
sender = 'vasyl@gmail.com'
recipient = 'ivan@gmail.com'
from_address = u'Василь '

text = u'Привіт Світ!\nВеселих свят!'

Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
multipart = MIMEMultipart('alternative')
multipart['Subject'] = Header(subject.encode('utf-8'), 'UTF-8').encode()
multipart['To'] = Header(recipient.encode('utf-8'), 'UTF-8').encode()
multipart['From'] = Header(from_address.encode('utf-8'), 'UTF-8').encode()

textpart = MIMEText(text.encode('utf-8'), 'plain', 'UTF-8')
multipart.attach(textpart)

session = smtplib.SMTP(settings.GMAIL_SMTP_SERVER, settings.GMAIL_SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo()
session.login(settings.GMAIL_USERNAME, settings.GMAIL_PASSWORD)
session.sendmail(sender, recipient, multipart.as_string())
session.quit()

Немає коментарів: