首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用python发送空白电子邮件

用python发送空白电子邮件
EN

Stack Overflow用户
提问于 2019-04-18 20:22:48
回答 1查看 70关注 0票数 0

当我使用python函数发送电子邮件时,我能够接收到电子邮件。但它是空白的。

这是我的功能:

代码语言:javascript
复制
def send_email(first_name, password, access_key, secret_key, user_group_list, user_secrets_list, aws_account, aws_account_number, mail_body):
    ## Get the address to send to
    to_addr = str(input("Enter the recipient's email address: "))
    from_addr = 'cloudops@noreply.company.com'
    ## Get the user's first name
    print(Fore.YELLOW)
    first_name = input("Enter the recipient's first name: ")
    subject = 'Welcome to AWS'
    content = mail_body
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = subject
    print("This is the content:",  content, "\n")
    body = MIMEText(content, 'html')
    print("This is the body: " , body, "\n")
    print("This is the Mesage: ", msg, "\n")
    #msg.attach(body)
    server = smtplib.SMTP('smtpout.us.companyworld.company.com', 25)
    server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])

我已经验证了我在函数中使用的变量的内容:

这是内容:<font size=2 face=Verdana color=black>Hello ,<br><br>You have been given access to this AWS account:<br><br>*jf-python-dev369812892824<br><br>You can get started by using the sign-in information provided below.<br><br>-------------------------------------------------<br><br>Sign-in URL:https://jf-python-dev.signin.aws.amazon.com/console<br>User name:tdunphy<br>Password:<br><br>-------------------------------------------------<br><br>When you sign in for the first time, you must change your password.<br><br>The user nametdunphy belongs to these groups:<br><br>grp-cloud-admins, grp-mfa-enforce<br><br>Regards,<br>Cloud Ops</font>

这是身体:

代码语言:javascript
复制
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<font size=2 face=Verdana color=black>Hello ,<br><br>You have been given access to this AWS account:<br><br>*jf-python-dev369812892824<br><br>You can get started by using the sign-in information provided below.<br><br>-------------------------------------------------<br><br>Sign-in URL:https://jf-python-dev.signin.aws.amazon.com/console<br>User name:tdunphy<br>Password:<br><br>-------------------------------------------------<br><br>When you sign in for the first time, you must change your password.<br><br>The user nametdunphy belongs to these groups:<br><br>grp-cloud-admins, grp-mfa-enforce<br><br>Regards,<br>Cloud Ops</font>

这是味精:

代码语言:javascript
复制
Content-Type: multipart/mixed; boundary="===============1031248993=="
MIME-Version: 1.0
From: cloudops@noreply.company.com
To: tdunphy@company.com
Subject: Welcome to AWS

--===============1031248993==

--===============1031248993==--

为什么我会收到空白邮件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-20 07:56:15

在问题中的代码中,主体没有附加到消息,因此它没有被发送。这个版本的功能:

代码语言:javascript
复制
def send_email():
    # Get the address to send to
    to_addr = "to@example.com"
    from_addr = "from@example.com"
    # Get the user's first name
    subject = "Welcome to AWS"
    content = "<p>Hello world</p>"
    msg = MIMEMultipart()
    msg["From"] = from_addr
    msg["To"] = to_addr
    msg["Subject"] = subject
    print("This is the content:", content, "\n")
    body = MIMEText(content, "html")
    msg.attach(body)
    print("This is the body: ", body, "\n")
    print("This is the Message: ", msg, "\n")
    server = smtplib.SMTP("localhost", 1025)
    server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])

产生这个输出:

代码语言:javascript
复制
This is the content: <p>Hello world</p> 

This is the body:  Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<p>Hello world</p> 

This is the Message:  Content-Type: multipart/mixed; boundary="===============7817399740373236689=="
MIME-Version: 1.0
From: from@example.com
To: to@example.com
Subject: Welcome to AWS

--===============7817399740373236689==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<p>Hello world</p>
--===============7817399740373236689==--

这是smtpd调试服务器的输出:

代码语言:javascript
复制
---------- MESSAGE FOLLOWS ----------
b'Content-Type: multipart/mixed; boundary="===============7817399740373236689=="'
b'MIME-Version: 1.0'
b'From: from@example.com'
b'To: to@example.com'
b'Subject: Welcome to AWS'
b'X-Peer: ::1'
b''
b'--===============7817399740373236689=='
b'Content-Type: text/html; charset="us-ascii"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: 7bit'
b''
b'<p>Hello world</p>'
b'--===============7817399740373236689==--'
------------ END MESSAGE ------------

如果您想发送没有其他MIME部件的html主体,则使用MIMEText作为消息容器更简单。

这个版本的函数

代码语言:javascript
复制
def send_email(): 
    # Get the address to send to
    to_addr = "to@example.com"
    from_addr = "from@example.com"
    # Get the user's first name
    subject = "Welcome to AWS"
    content = "<p>Hello world</p>"
    msg = MIMEText(content, 'html')
    msg["From"] = from_addr
    msg["To"] = to_addr
    msg["Subject"] = subject
    print("This is the content:", content, "\n")
    print("This is the Message: ", msg, "\n")
    server = smtplib.SMTP("localhost", 1025)
    server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])

产生这个输出:

代码语言:javascript
复制
This is the content: <p>Hello world</p> 

This is the Message:  Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
From: from@example.com
To: to@example.com
Subject: Welcome to AWS

<p>Hello world</p> 

这是smtpd调试服务器的输出:

代码语言:javascript
复制
---------- MESSAGE FOLLOWS ----------
b'Content-Type: text/html; charset="us-ascii"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: 7bit'
b'From: from@example.com'
b'To: to@example.com'
b'Subject: Welcome to AWS'
b'X-Peer: ::1'
b''
b'<p>Hello world</p>'
------------ END MESSAGE ------------
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55753245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档