「Python入門」Pythonでメールを送信するサンプルコード

サンプルコード:
__author__ = 'gg88’

import email,sys,os
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.MIMEImage import MIMEImage

SENDER = 'username@arkgame.com’
SMTPSERVER = 'smtpcloud.sohu.com’
#RECEIVERS = "
USERNAME = 'username@arkgame.com’
APIKEY = '******’

def sendMail(subject, receivers, cc, content, atts):
msg = MIMEMultipart('related’)
msg['Subject’] = unicode(subject, “UTF-8″)
msg['From’] = SENDER
msg['To’] = receivers
msg['Cc’] = cc

#メール内容
if os.path.isfile(content):
if(content.split('.’)[-1]==’html’):
cont = MIMEText(open(content).read(),’html’,’utf-8′)
else:
cont = MIMEText(open(content).read(),’plain’,’utf-8′)
else:
cont = MIMEText(content, 'plain’,’utf-8′)
msg.attach(cont)

#添付ファイルの処理
if atts != -1 and atts != ":
for att in atts.split(',’):
os.path.isfile(att)
name = os.path.basename(att)
att = MIMEText(open(att).read(), 'base64’, 'utf-8’)
att[“Content-Type"] = 'application/octet-stream’
#utf-8
att[“Content-Disposition"] = 'attachment; filename=%s’ % name.decode('utf-8’).encode('gbk’)
msg.attach(att)

smtp = smtplib.SMTP()
smtp.connect(SMTPSERVER)
smtp.login(USERNAME, APIKEY)
for recev in receivers.split(',’):
smtp.sendmail(SENDER,recev, msg.as_string())
for c in cc.split(',’):
smtp.sendmail(SENDER,c, msg.as_string())
smtp.quit()

def main():
print “start send mail[sendmail.py]"
subject = sys.argv[1]
receivers = sys.argv[2]
#cc = sys.argv[3]
leng = len(sys.argv)
if leng == 3:
cc = “"
content = “"
atts = -1
elif leng == 4:
print “The parameters is not currect!"
sys.exit(0)
elif leng == 5:
cc = sys.argv[3]
content = sys.argv[4]
atts = -1
elif leng == 6:
cc = sys.argv[3]
content = sys.argv[4]
atts = sys.argv[5]
sendMail(subject, receivers, cc, content, atts)
print “finish send mail[sendmail.py]"

if __name__==’__main__’:
main()

Python

Posted by arkgame