Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify a sender when sending mail with Python (smtplib)

Tags:

python

email

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

import smtplib
import time

server = 'smtp.myprovider.com'
recipients = ['[email protected]']
sender = '[email protected]'
message = 'Subject: [PGS]: Results\n\nBlaBlaBla'

session = smtplib.SMTP(server)

session.sendmail(sender,recipients,message);

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?

like image 433
TimothyP Avatar asked Feb 12 '09 11:02

TimothyP


People also ask

How do I send an email using Smtplib?

Set up a secure connection using SMTP_SSL() and . starttls() Use Python's built-in smtplib library to send basic emails. Send emails with HTML content and attachments using the email package.

What is Smtplib in Python How can you use Python built in mail server explain?

Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. host − This is the host running your SMTP server. You can specify IP address of the host or a domain name like tutorialspoint.com.

How do I send an email using Python 3?

To send the mail you use smtpObj to connect to the SMTP server on the local machine. Then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these are not always used to route the mail).


1 Answers

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

message = 'From: [email protected]\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)

like image 175
dF. Avatar answered Sep 21 '22 06:09

dF.