简体   繁体   中英

Sending blank emails in python

When I send emails using my python function, I am able to receive the email. But it's blank.

Here is my function:

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])

I've verified the contents of the variables I use in the function:

This is the content: <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>

This is the body:

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>

This is the msg:

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==--

Why am I receiving blank emails?

In the code in the question, the body is not attached to the message, hence it is not sent. This version of your function:

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])

produces this output:

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==--

and this is the output of the smtpd debug server:

---------- 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 ------------

If you want to send an html body with no other MIME parts, using MIMEText as the message container is simpler.

This version of your function

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])

produces this output:

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> 

and this is the output of the smtpd debug server:

---------- 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 ------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM