48

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@gmail.com"
   msg['To'] = to
   msg['Subject'] = subject
   msg['Reply-to'] = "email2@example.com"

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
eli
  • 4,636
  • 7
  • 25
  • 29
  • The "From" address is non-Gmail too (matches the Reply-To). Is this the problem? What happens when you say "this doesn't work"? – MattH May 09 '11 at 15:18
  • Hi, I changed that back so that From was the Gmail address, and it still didn't work. So don't think that's the source of the problem, but thanks – eli May 09 '11 at 15:27
  • 8
    `msg['Reply-To']` worked just fine for me. – Jonathon Reinhart May 21 '15 at 00:47

5 Answers5

55

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.example'
>>> TO_ADDRESS = 'you@gmail.com'
>>> FROM_ADDRESS = 'email@domain.example'
>>> REPLY_TO_ADDRESS = 'email2@domain2.example'
>>> 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())
{}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
mattbornski
  • 11,895
  • 4
  • 31
  • 25
  • 1
    it seems that add_header behaves pretty the same like __setitem__(), therefore the original approach (with proper spelling/upper/lowercase) should also work, compare the docs: https://docs.python.org/release/2.6.6/library/email.message.html#email.message.Message.add_header – Nico Albers Sep 03 '18 at 08:51
24

I had the same question and all I had to do to make it work was to set the header in lowercase like so:

msg['reply-to'] = "email2@example.com"
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
turbofan
  • 459
  • 4
  • 5
3

For Python3 in 2021 I would recommend the following to construct the message:

from email.message import EmailMessage
from email.utils import formataddr

msg = EmailMessage()
msg['Subject'] = "Message Subject"
msg['From'] = formataddr(("Sender's Name", "email@gmail.com"))
msg['Reply-To'] = formataddr(("Name of Reply2", "email2@domain2.example"))
msg['To'] = formataddr(("John Smith", "john.smith@gmail.com"))
msg.set_content("""\
<html>
  <head></head>
  <body>
    <p>A simple test email</p>
  </body>
</html>
""", subtype='html')

Then to send the message I use the following for my mail server which uses StartTLS on port 587:

from smtplib import SMTP
from ssl import create_default_context as context

with SMTP('smtp.domain.example', 587) as server:
    server.starttls(context=context())
    server.login('email@domain.example', password)
    server.send_message(msg)
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
HyperActive
  • 1,129
  • 12
  • 12
2

As Jonathon Reinhart pointed out, the "To" needs to be upper case:

msg['Reply-To'] = "email2@example.com"
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
gerardw
  • 5,822
  • 46
  • 39
0

Only this worked for me:

msg['In-Reply-To'] = "email2@example.com"

Look here: Reply to email using python 3.4 by urban

My Work
  • 2,143
  • 2
  • 19
  • 47
M14
  • 444
  • 6
  • 8