我想從使用SMTP庫的python腳本使用Gmail帳戶發送電子郵件。它正常工作正常的郵件正文。但是,當我嘗試使用HTML正文發送它。它不允許我發送。使用來自gmail帳戶與HTML正文的電子郵件使用SMTP python
# 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 = '[email protected]'
addr_from = "[email protected]"
# Define SMTP email server details
smtp_server = 'smtp.gmail.com'
smtp_user = '[email protected]'
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."
html = """\
<html>
<head></head>
<body>
<p>This is a test message.</p>
<p>Text and HTML</p>
</body>
</html>
"""
# 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.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()
你能告訴我們你所得到的錯誤? – Peni
這是給「auth」錯誤 –
嘗試打開您的Gmail帳戶中的「允許安全性較低的應用程序」,您用於登錄 – Sankar