How do I need to format my message to fill subject and body in automated email script?

Viewed 94

I am trying to set up an automated alert emailing system for a project and I can format the email properly when it's plain text but when I try to add variables it won't format properly. I am pulling data from a JSON file and I can pull the data I just can't combine it with the email inside the triple quotes (""").

Here is the plain text

 message = """\
    Subject: AMLD Alert


    Something is wrong.""" 

But when I add in my variables it will not fill in the body, but the subject will.

message = 'Subject: AMLD Alert for ' +  d[0]['Project Name'] + ' Project' 
        

    'Car: ' + d[0]['CarID'] +' Driven by: ' + d[0]['DriverID'] + ' is sending alert '+ d[0]['Message']

Here is the entire script if it will help.

import json
import smtplib
import ssl
import socket
socket.getaddrinfo('localhost', 8080)

#Webiste use to help me build script
#https://realpython.com/python-send-email/


port = 587  # For starttls
smtp_server = "smtp.outlook.com"
sender_email = "MyWorkOutlookEmail"  # Enter your address
password = 'Password'
receiver_email = "MyPersonalGmail" # Enter receiver address
context = ssl.create_default_context()
    


#Start of ITTT Code
with open('C:/Python/Messaging/Mes_V1.json') as f:
    d = json.load(f)
    if d[0]['Alert'] == "High":
        print('Sending high alert email...\n')

        message = 'Subject: AMLD Alert for ' +  d[0]['Project Name'] + ' Project' 
            

        'Car: ' + d[0]['CarID'] +' Driven by: ' + d[0]['DriverID'] + ' is sending alert '+ d[0]['Message']
        
        

    elif d[0]['Alert'] == "Medium":
        print('Sending medium alert email...\n')
       

    else:
        print('Sending low alert email...\n')
        
        


context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
    print('Connecting to Server...\n')
    server.starttls(context=context)
    print('Logging in...\n')
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
    print('Email sent.\n')

I'm not sure if Outlook is affecting it in any way or if I need to format the message differently, any help would be very appreciated!

0 Answers
Related