mailexample.py
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.utils import formatdate
FROM_ADDR = 'myAcount@gmail.com'
TO_ADDR = 'to@example.com'
CC_ADDR = 'cc@example.com'
BCC_ADDR = 'bcc@example.com'
ENCODING = 'iso-2022-jp'
def workaround(mailstr):
return mailstr.encode(ENCODING).decode('iso-8859-1')
message = MIMEText(
workaround('本文'),
'plain',
ENCODING,
)
message['Subject'] = str(Header(workaround('題名'), ENCODING))
message['From'] = '%s <%s>' % (
str(Header(workaround('差出人'), ENCODING)),
FROM_ADDR
)
message['To'] = '%s <%s>' % (
str(Header(workaround('宛先'), ENCODING)),
TO_ADDR
)
message['CC'] = '%s <%s>' % (
str(Header(workaround('炭素複写'), ENCODING)),
CC_ADDR
)
message['Date'] = formatdate()
s = smtplib.SMTP('192.168.1.2:25')
s.sendmail (
FROM_ADDR,
[TO_ADDR, CC_ADDR, BCC_ADDR],
message.as_string()
)
s.close()