1. 程式人生 > >python 傳送電子郵件

python 傳送電子郵件

from smtplib import SMTP
from email.header import Header
from email.mime.text import MIMEText
#傳送郵件
def send_email():
    # 請自行修改下面的郵件傳送者和接收者
	sender = '[email protected]'
	receivers = ['[email protected]', '[email protected]']
	message = MIMEText('用Python傳送郵件', 'plain', 'utf-8')
	message['From'] = Header('a的郵箱', 'utf-8')
	message['To'] = Header('b的郵箱', 'utf-8')
	message['Subject'] = Header('示例程式碼實驗郵件', 'utf-8')
	smtper = SMTP('smtp.126.com')
    # 請自行修改下面的登入口令
	smtper.login(sender, 'secretpass')
	smtper.sendmail(sender, receivers, message.as_string())
	print('郵件傳送完成!')

if __name__ == '__main__':
	send_email()