当我使用python函数发送电子邮件时,我能够接收该电子邮件。但这是空白。
这是我的功能:
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>
这是身体:
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>
这是味精:
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==--
为什么我会收到空白电子邮件?
答案 0 :(得分:1)
在问题代码中,正文未附加到消息中,因此不会发送。此版本的功能:
@Query
产生以下输出:
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])
这是smtpd调试服务器的输出:
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==--
如果要发送不包含其他MIME部分的html正文,则使用MIMEText作为消息容器更为简单。
此功能的版本
---------- 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 ------------
产生以下输出:
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])
这是smtpd调试服务器的输出:
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>