TimothyP TimothyP - 8 months ago 36
Python Question

Specify a sender when sending mail with Python (smtplib)

I have a very simple piece of code (just for testing):

import smtplib
import time

server = 'smtp.myprovider.com'
recipients = ['johndoe@somedomain.com']
sender = 'me@mydomain.com'
message = 'Subject: [PGS]: Results\n\nBlaBlaBla'

session = smtplib.SMTP(server)


This works but the problem is that e-mail clients don't display a sender.
I want to be able to add a sender name to the e-mail. Suggestions?

dF. dF.

smtplib doesn't automatically include a From: header, so you have to put one in yourself:

message = 'From: me@example.com\nSubject: [PGS]: Results\n\nBlaBlaBla'

(In fact, smtplib doesn't include any headers automatically, but just sends the text that you give it as a raw message)