在python中发送空白电子邮件

问题描述 投票:0回答:1

当我使用我的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 = '[email protected]'
    ## 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: [email protected]
To: [email protected]
Subject: Welcome to AWS

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

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

为什么我收到空白电子邮件?

python email
1个回答
1
投票

在问题的代码中,正文未附加到消息,因此不会发送。这个版本的功能:

def send_email():
    # Get the address to send to
    to_addr = "[email protected]"
    from_addr = "[email protected]"
    # 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])

产生这个输出:

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: [email protected]
To: [email protected]
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调试服务器的输出:

---------- MESSAGE FOLLOWS ----------
b'Content-Type: multipart/mixed; boundary="===============7817399740373236689=="'
b'MIME-Version: 1.0'
b'From: [email protected]'
b'To: [email protected]'
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作为消息容器更简单。

这个版本的功能

def send_email(): 
    # Get the address to send to
    to_addr = "[email protected]"
    from_addr = "[email protected]"
    # 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])

产生这个输出:

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: [email protected]
To: [email protected]
Subject: Welcome to AWS

<p>Hello world</p> 

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

---------- MESSAGE FOLLOWS ----------
b'Content-Type: text/html; charset="us-ascii"'
b'MIME-Version: 1.0'
b'Content-Transfer-Encoding: 7bit'
b'From: [email protected]'
b'To: [email protected]'
b'Subject: Welcome to AWS'
b'X-Peer: ::1'
b''
b'<p>Hello world</p>'
------------ END MESSAGE ------------
© www.soinside.com 2019 - 2024. All rights reserved.