$ python -V
Python 2.6.6
import smtplib
from email.mime.text import MIMEText
LOGIN = 'my_email@mail.ru'
FROM = 'Andrew'
PASS = '_My_Pass_for_mailru'
#SUBTYPE = 'plain' #обычный текст
SUBTYPE = 'html' #сообщение в html
SMTP_SERVER = 'smtp.mail.ru'
CHARSET='cp1251' #системная локаль - utf-8, отправляем в cp1251
def send_mail(to, subj, message):
msg = MIMEText(message.decode('utf8').encode(CHARSET),SUBTYPE,CHARSET)
msg['Content-Type'] = "text/html; charset=cp1251"
msg['Subject'] = subj.decode('utf8').encode(CHARSET)
msg['From'] = FROM+' <'+LOGIN+'>'
msg['To'] = to
try:
conn = smtplib.SMTP(SMTP_SERVER)
conn.ehlo('e.mail.ru')
conn.set_debuglevel(True)
conn.login(LOGIN,PASS)
conn.sendmail(LOGIN, to, msg.as_string())
finally: conn.close()
...........
_______________________________________________
для TLS:
Sending mail from Python using SMTP
Python 2.6.6
import smtplib
from email.mime.text import MIMEText
LOGIN = 'my_email@mail.ru'
FROM = 'Andrew'
PASS = '_My_Pass_for_mailru'
#SUBTYPE = 'plain' #обычный текст
SUBTYPE = 'html' #сообщение в html
SMTP_SERVER = 'smtp.mail.ru'
CHARSET='cp1251' #системная локаль - utf-8, отправляем в cp1251
def send_mail(to, subj, message):
msg = MIMEText(message.decode('utf8').encode(CHARSET),SUBTYPE,CHARSET)
msg['Content-Type'] = "text/html; charset=cp1251"
msg['Subject'] = subj.decode('utf8').encode(CHARSET)
msg['From'] = FROM+' <'+LOGIN+'>'
msg['To'] = to
try:
conn = smtplib.SMTP(SMTP_SERVER)
conn.ehlo('e.mail.ru')
conn.set_debuglevel(True)
conn.login(LOGIN,PASS)
conn.sendmail(LOGIN, to, msg.as_string())
finally: conn.close()
...........
_______________________________________________
для TLS:
smtp.connect('YOUR.MAIL.SERVER', 587)
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('USERNAME@DOMAIN', 'PASSWORD')
Sending mail from Python using SMTP