トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

PRG-PY-mta

  python でメールの送信

#!/usr/bin/env python3
# -*- coding: utf-8 -*- 

import smtplib

from email.mime.text import MIMEText
from email.header import Header
from email.utils import formatdate

from_address = 'senders@address'
to_address   = 'recived@add.ress'

charset = 'ISO-2022-JP'
subject = u'メールの件名です'
text    = u'メールの本文です'

msg = MIMEText(text.encode(charset), 'plain', charset)
msg['Subject'] = Header(subject, charset)
msg['From'] = from_address
msg['To'] = to_address
msg['Date'] = formatdate(localtime=True)

smtp = smtplib.SMTP('192.168.11.200')
smtp.sendmail(from_address, to_address, msg.as_string())
smtp.close()