Python Forum
[Solved]Help Displaying Emails properly via Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved]Help Displaying Emails properly via Python
#1
Hello,

I have a sendEmail Function for my voice assistant which allows it to send emails. The emails send successfully, but they don't display properly on the receiver end (The subject line and body don't display).

I want the user to enter the recipient's e-mail and the e-mail's body/message/content (The Subject and From lines are pre-filled so when the receiver gets the email it should show like the screenshots below), but every time I test it out with user input, the e-mail displays without a subject line or message.

Any way to fix this?

Thanks in advance.

With the code I want to use (this one), the E-mails show without a subject or a body:
#-------------------------------------------------------------------------------------
#                                E-Mail Function
#-------------------------------------------------------------------------------------
def sendEmail():
    # Define the transport variables
    ctx = ssl.create_default_context()
    password = "12345"   
    sender = "[email protected]"   

    autoTypeAnimation('Who should I send the E-mail to?')
    receiver = UserInput()

    autoTypeAnimation('What should I say?')
    msg = UserInput()

    message = """\
    From: "Baxter"
    Subject: Alert!
    """ + msg
    
    #Send Email
    with smtplib.SMTP_SSL("smtp.gmail.com", port=465, context=ctx) as server:
        server.login(sender, password)
        server.sendmail(sender, receiver, message)
        autoTypeAnimation('E-mail sent')
#-------------------------------------------------------------------------------------
The Email's Display properly with this code (But the user won't be able to enter any information in):
import smtplib
import ssl

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Define the transport variables
ctx = ssl.create_default_context()
password = "12345"    
sender = "[email protected]"    
receiver = "[email protected]" 

message = """\
From: "Baxter"
Subject: Alert!

Hello Commander,
This is a test.
"""

with smtplib.SMTP_SSL("smtp.gmail.com", port=465, context=ctx) as server:
    server.login(sender, password)
    server.sendmail(sender, receiver, message)

Attached Files

Thumbnail(s)
       
Reply
#2
The working example has an empty line following the subject.
Reply
#3
I tried adding an empty line and it didn't work.
I have attached a screenshot below to show the results when I type in "Test 1234" for the message using this code:
#-------------------------------------------------------------------------------------
#                                E-Mail Function
#-------------------------------------------------------------------------------------
def sendEmail():
    ctx = ssl.create_default_context()
    password = "12345"    
    sender = "[email protected]" 

    autoTypeAnimation('Who should I send the E-mail to?')
    receiver = UserInput()

    autoTypeAnimation('What should I say?')
    msg = UserInput()

    message = """\
    From: "Baxter"
    Subject: Alert!
    
    """ + msg #msg is the user's message/command which is taken in as a string
    
    #Send Email
    with smtplib.SMTP_SSL("smtp.gmail.com", port=465, context=ctx) as server:
        server.login(sender, password)
        server.sendmail(sender, receiver, message)
        autoTypeAnimation('E-mail sent')
#-------------------------------------------------------------------------------------

Attached Files

Thumbnail(s)
   
Reply
#4
I would add this code between where you get the receiver and the message, and where you send the email. This should work as it did in your test because you are using the same receiver and message body as in your test.
sender = "[email protected]"    
receiver = "[email protected]" 
message = """\
From: "Baxter"
Subject: Alert!
 
Hello Commander,
This is a test.
"""

    #Send Email
If this works, remove the receiver reassignment and test if it still works:

If that works the error must be in the message. Remove the message reassignment to verify. I'd also add in some code to print out what message is sent:
    autoTypeAnimation('What should I say?')
    msg = UserInput()
 
    message = """\
    From: "Baxter"
    Subject: Alert!
    """ + msg
    print(type(message))
    print(len(message))
    print(message)
    #Send Email
Is the printed information what you expected to see?
Reply
#5
I got it to work.

Apparently he message body didn't like being inside the function because it was tabbed out so I put it at the top of my code as a variable so I didn't have to tab it out and now it works as it should and the emails display properly.

This displays it properly.
#-------------------------------------------------------------------------------------
#                                Define Variables
#-------------------------------------------------------------------------------------
console = Console(width=100) 

#E-mail display format for E-Mail Function
emailFormat = """\
From: "Baxter"
Subject: Alert!

""" 
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
#                                E-Mail Function
#-------------------------------------------------------------------------------------
def sendEmail():
    #Tutorial Link: https://www.abstractapi.com/guides/sending-email-with-python
    #Define the transport variables
    ctx = ssl.create_default_context()
    password = "12345"
    sender = "[email protected]" 

    autoTypeAnimation('Who should I send the E-mail to?')
    receiver = UserInput()

    autoTypeAnimation('What should I say?')
    msg = UserInput()

    #Preview the E-mail
    print("\n[green]Baxter: [/green]")
    print(emailFormat + msg + "\n")
    
    #Send Email
    try:
        with smtplib.SMTP_SSL("smtp.gmail.com", port=465, context=ctx) as server:
            server.login(sender, password)
            server.sendmail(sender, receiver, emailFormat + msg)
            autoTypeAnimation('E-mail sent')
    except:
        autoTypeAnimation('E-mail failed to send')
#-------------------------------------------------------------------------------------
Reply
#6
server.sendmail(sender, receiver, f"From: \"Baxter\"\nSubject: Alert!\n\n{msg}")
or
email_format = "From: \"Baxter\"\nSubject: Alert!\n\n{}"
...
server.sendmail(sender, receiver, email_format.format(msg)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 588 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  Sending Emails on Autopilot Gyga_Hawk 3 1,629 Mar-15-2022, 08:20 AM
Last Post: Larz60+
  Mark outlook emails as read using Python! shane88 2 6,451 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  Trying out the parsing/reading of emails from my outlook cubangt 0 6,053 Jan-12-2022, 08:59 PM
Last Post: cubangt
  reading shared outlook emails zarize 0 2,422 Mar-03-2020, 01:47 PM
Last Post: zarize
  Python Library for Reading POP Emails? bmccollum 1 3,563 Jan-06-2020, 06:37 PM
Last Post: micseydel
  Read in trades from emails semantina 2 2,065 Nov-06-2019, 06:12 PM
Last Post: semantina
  In Visual Studio Python is not properly installed jalea148 4 3,037 Sep-26-2019, 12:31 PM
Last Post: snippsat
  Python 2.7.13 Issue Reading .txt files Properly username1145 3 2,492 Mar-24-2019, 03:08 PM
Last Post: username1145
  Displaying image on PC using Arduino and Python GallowMilk 1 2,287 Sep-19-2018, 06:38 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020