gpt4 book ai didi

python - 使用来自带有 html 正文的 gmail 帐户的 SMTP python 的电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:15 24 4
gpt4 key购买 nike

我正在尝试使用 SMTP 库从 python 脚本使用 gmail 帐户发送电子邮件。它适用于正常的邮件正文。但是当我尝试使用 HTML 正文发送它时。它不允许我发送。

# 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."
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()

最佳答案

在尝试登录之前添加这两行,它不会给你身份验证错误。

server.ehlo()
server.starttls()

所以你的代码应该是这样的:

    # 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()

关于python - 使用来自带有 html 正文的 gmail 帐户的 SMTP python 的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45322783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com