ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用带有html正文的gmail帐户的SMTP python发送电子邮件

2019-08-28 10:57:30  阅读:280  来源: 互联网

标签:python smtp


我正在尝试使用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,smtp
来源: https://codeday.me/bug/20190828/1751169.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有