With some help off of here, I've set up some code to send emails via Python using SMTP. However the subject does not work. How can I also print variable values in the email body?
port = 587 # For starttls
smtp_server = "smtp.server.com"
sender_email = "sender@outlook.com"
receiver_email = "receiver@outlook.com"
password = "password"
message = """ Subject: Subject
This message is sent from Python."""
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
Please could you inform me as to:
How to correctly add a subject so that it correctly appears when the email is sent
How to include a print of a variable
Currently the only thing which appears in the email is "This message is sent from Python"
SUBJECT = "SUBJECT of mail"
AGE = 18
message = "Subject: {}\n\n My age is {}".format(SUBJECT, AGE)
you can use format function of string in python to use variable. and \n\n is necessary to differ subject and message