Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting different reply-to message in Python email/smtplib

I am using Python email and smtplib to send an email from Python. I am doing this via the Gmail SMTP server using my Gmail credentials. This works fine, however I would like to specify a Reply-to email address different from the from address, so that replies go to a separate address (non-Gmail.)

I have tried creating a reply to parameter like this:

   msg = MIMEMultipart()     msg['From'] = "[email protected]"    msg['To'] = to    msg['Subject'] = subject    msg['Reply-to'] = "[email protected]" 

But this doesn't work. Can't find any info on this in the Python docs.

Thanks.

like image 347
eli Avatar asked May 09 '11 15:05

eli


People also ask

How do you send a custom email in Python?

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 does Smtplib mean in Python?

Source code: Lib/smtplib.py. The smtplib module defines an SMTP client session object that can be used to send mail to any internet machine with an SMTP or ESMTP listener daemon.

How do you send multiple emails in Python?

To send email to multiple recipients using Python smtplib, we can use the sendmail method. We create the SMTP instance by using the SMTP server address as the argument. Then we create the message with the MIMEText class. We combine the recipients into a string with join .

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

The smtplib module The smtplib is a Python library for sending emails using the Simple Mail Transfer Protocol (SMTP). The smtplib is a built-in module; we do not need to install it. It abstracts away all the complexities of SMTP.


1 Answers

Here's my take on it. I believe that the "Reply-To" header should be set explicitly. The likely reason is that it's less commonly used than headers such as "Subject", "To", and "From".

python Python 2.6.6 (r266:84292, May 10 2011, 11:07:28)  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> MAIL_SERVER = 'smtp.domain.com' >>> TO_ADDRESS = '[email protected]' >>> FROM_ADDRESS = '[email protected]' >>> REPLY_TO_ADDRESS = '[email protected]' >>> import smtplib >>> import email.mime.multipart >>> msg = email.mime.multipart.MIMEMultipart() >>> msg['to'] = TO_ADDRESS >>> msg['from'] = FROM_ADDRESS >>> msg['subject'] = 'testing reply-to header' >>> msg.add_header('reply-to', REPLY_TO_ADDRESS) >>> server = smtplib.SMTP(MAIL_SERVER) >>> server.sendmail(msg['from'], [msg['to']], msg.as_string()) {} 
like image 83
mattbornski Avatar answered Sep 23 '22 06:09

mattbornski