Python メール送信

sendmailを使ったメール送信サンプルです.uniuni.pyを使います.

サンプルプログラム

import os
from email.MIMEText import MIMEText
from email.Message import Message
from email.Header import Header

def sendmail(sendmail_location, mailto, mailcc, mailbcc, mailfrom, mailsubject, mailbody ):
	status = 'NG'
	msg = MIMEText(mailbody.encode('ISO-2022-JP'), 'plain', 'ISO-2022-JP')
	if( mailfrom != '' and mailto != '' ):
		msg['From'] = mailfrom
		msg['To'] = mailto
		if( mailcc != '' ):
			msg['Cc'] = mailcc
		if( mailbcc != '' ):
			msg['Bcc'] = mailbcc
		msg['Subject'] = Header(mailsubject.encode('ISO-2022-JP'), 'ISO-2022-JP')

		p = os.popen("%s -t -i -f %s" % ( sendmail_location, mailfrom ), "w")
		p.write( msg.as_string() )
		status = p.close()
	return status

サンプルプログラムの使用例

#!/usr/local/bin/python
#coding: utf-8

from uniuni import uniuni 
from sendmail import sendmail

sendmail_location = "/usr/sbin/sendmail" # sendmail location

mailto   = 'hoge@hoge.jp'
mailcc   = ''
mailbcc  = ''
mailfrom = 'hage@hage.jp'
mailsubject = u'Python Tips'
mailbody = u'''\
テスト~
'''

print sendmail(sendmail_location,mailto,mailcc,mailbcc,mailfrom,uniuni(mailsubject),uniuni(mailbody))

ダウンロード

サンプルプログラム
サンプルプログラムの使用例

参考HP