2011年10月21日金曜日

pythonでcoreserverからmail送信

chihiroさんのこのページのほぼ丸写し。
chihiroさんありがとうございます。
# -*- coding: utf-8 -*-

import smtplib
from email.MIMEText import MIMEText
from email.Utils import formatdate
from pit import Pit

def create_message(from_addr, to_addr, subject, body):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Date'] = formatdate()
    return msg

def send_via_SSL(from_addr, to_addr, msg, email_account):
    print from_addr + ' to ' + to_addr
    mail_pit = Pit.get(email_account)
    s = smtplib.SMTP_SSL(mail_pit['servername'], 465) #
    s.ehlo()
    s.login(mail_pit['username'], mail_pit['password'])
    s.sendmail(from_addr, [to_addr], msg.as_string())
    s.close()

if __name__ == '__main__':
    from_addr = 'hoge@hogehoge.jp'
    to_addr = 'hoge@hogehoge.com'
    email_account = 'hoge@hogehoge.jp'
    msg = create_message(from_addr, to_addr, 'test subject', from_addr + ' to ' + to_addr + ' using ' + email_account + ' with SSL')
    send_via_SSL(from_addr, to_addr, msg, email_account)
 違いは赤い字のSMTP_SSLあたり

0 件のコメント:

コメントを投稿