I'm writing a python script to send emails from the terminal. In the mail which I currently send, it is without a subject. How do we add a subject to this email?
My current code:
import smtplib
msg = """From: hello@hello.com
To: hi@hi.com\n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()
解决方案
You need to put the subject in the header of the message.
Example -
import smtplib
msg = """From: hello@hello.com
To: hi@hi.com\n
Subject: \n
Here's my message!\nIt is lovely!
"""
server = smtplib.SMTP_SSL('smtp.example.com', port=465)
server.set_debuglevel(1)
server.ehlo
server.login('examplelogin', 'examplepassword')
server.sendmail('me@me.com', ['anyone@anyone.com '], msg)
server.quit()