Python: "subject" not shown when sending email using smtplib module

Attach it as a header:

message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)

and then:

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

Also consider using standard Python module email - it will help you a lot while composing emails.


You should probably modify your code to something like this:

from smtplib import SMTP as smtp
from email.mime.text import MIMEText as text

s = smtp(server)

s.login(<mail-user>, <mail-pass>)

m = text(message)

m['Subject'] = 'Hello!'
m['From'] = <from-address>
m['To'] = <to-address>

s.sendmail(<from-address>, <to-address>, m.as_string())

Obviously, the <> variables need to be actual string values, or valid variables, I just filled them in as place holders. This works for me when sending messages with subjects.


This will work with Gmail and Python 3.6+ using the new "EmailMessage" object:

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg.set_content('This is my message')

msg['Subject'] = 'Subject'
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"

# Send the message via our own SMTP server.
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("[email protected]", "password")
server.send_message(msg)
server.quit()

try this:

import smtplib
from email.mime.multipart import MIMEMultipart
msg = MIMEMultipart()
msg['From'] = 'sender_address'
msg['To'] = 'reciver_address'
msg['Subject'] = 'your_subject'
server = smtplib.SMTP('localhost')
server.sendmail('from_addr','to_addr',msg.as_string())

Tags:

Python

Smtplib