python smtplibモジュールを呼び出してメールを送信

3264 ワード

#!/usr/bin/env python

#coding: utf-8

import smtplib

from email.mime.text import MIMEText

from email.header import Header

sender = '[email protected]'

#receiver = '[email protected]'

receiver = '[email protected]'

subject = 'python email test'

smtpserver = 'smtp.126.com'

username = '[email protected]'

password = 'teaforme80'



msg = MIMEText('  ','plain','utf-8')#     ‘utf-8’,        

msg['Subject'] = Header(subject, 'utf-8')



smtp = smtplib.SMTP()

smtp.connect('smtp.126.com')

smtp.ehlo()

smtp.starttls()

smtp.ehlo()

smtp.set_debuglevel(1)

smtp.login(username, password)

#send message

smtp.sendmail(sender, receiver, msg.as_string())

#quit()

smtp.quit()