|
我正在嘗試使用SMTP庫從python腳本使用gmail帳戶發(fā)送電子郵件.它與普通的郵件正常工作正常.但是當(dāng)我嘗試使用HTML正文發(fā)送它時(shí).它不允許我發(fā)送.
解決方法: 在嘗試登錄之前添加這兩行,它不會(huì)給您帶來身份驗(yàn)證錯(cuò)誤.
所以你的代碼應(yīng)該是這樣的:
# Import smtplib to provide email functions
import smtplib
# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Define email addresses to use
addr_to = 'xxxx@localdomain.com'
addr_from = "xxxxx@gmail.com"
# Define SMTP email server details
smtp_server = 'smtp.gmail.com'
smtp_user = 'xxxxxx@gmail.com'
smtp_pass = 'xxxxxxx'
# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = *emphasized text*addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'
# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
(your html code)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server,587)
s.ehlo()
s.starttls()
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
來源:https://www./content-1-418751.html
|
|
|