import yagmail
#連結郵箱伺服器
yag = yagmail.SMTP( user="******@qq.com", password="授權碼", host='smtp.qq.com')
# 郵箱正文
contents = ['This is the body, and here is just text http://somedomain/image.png',
'You can find an audio file attached.', '/local/path/song.mp3']
# 發送郵件
yag.send('*****@qq.com', 'subject', contents)
#關閉
yag.close()
谷歌郵箱發送(開啟VPN):
import smtplib
GMAIL_USERNAME = "[email protected]"
GMAIL_PASSWORD = "郵箱密碼"
email_subject = "Hello"
recipient = "[email protected]"
body_of_email = "hi"
# The below code never changes, though obviously those variables need values.
session = smtplib.SMTP('smtp.gmail.com', 587)
session.ehlo()
session.starttls()
session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
headers = "\r\n".join(["from: " + GMAIL_USERNAME,
"subject: " + email_subject,
"to: " + recipient,
"mime-version: 1.0",
"content-type: text/html"])
# body_of_email can be plaintext or html!
content = headers + "\r\n\r\n" + body_of_email
session.sendmail(GMAIL_USERNAME, recipient, content)