import smtplib
to = 'where@gmail.com'
server = "smtp.gmail.com"
#port = 465 # ssl
port = 587 # tls
gmail_user = 'youraccount@gmail.com'
gmail_pwd = 'yourpassword or application password'
#smtpserver = smtplib.SMTP_SSL(server, long(port)) # ssl
smtpserver = smtplib.SMTP(server, long(port)) # tls
smtpserver.ehlo()
smtpserver.starttls() # tls
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:test \n'
print header
msg = header + '\n this is test \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.quit()
*如果你有使用兩階段驗證,必須先到 google 個人設定頁,增加應用程式密碼,smtp 才會驗證通過…
https://myaccount.google.com/apppasswords
沒有留言:
張貼留言