Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get message-id of email sent from smtplib

I want to record users reply to my mail and display it as thread in my application. For this purpose I am using help of message-id in present in the email head. When I sent a mail I can see message-id being printed on the screen how do i get this message-id. Also the message-id created by me is overrided. my code is as below.

import smtplib
from email.mime.text import MIMEText

subject = 'Hello!'
message = 'hiii!!!'
email = '[email protected]'
send_from = '[email protected]'
msg = MIMEText(message, 'html', 'utf-8')
msg['Subject'] = subject
msg['From'] = send_from
msg['To'] = email
msg['Message-ID'] = '01234567890123456789abcdefghijklmnopqrstuvwxyz'
send_to = [email]

smtp_server = 'email-smtp.us-east-1.amazonaws.com'
smtp_port = 587
user_name = 'abcd'
password = 'abcd'
try:
    server = smtplib.SMTP(smtp_server, smtp_port)
    server.set_debuglevel(True)
    server.starttls()
    server.ehlo()
    server.login(user_name,password)
    server.sendmail(send_from, send_to, msg.as_string())

except Exception, e:
    print e
like image 925
Sar009 Avatar asked Apr 08 '14 13:04

Sar009


1 Answers

Use email.utils.make_msgid to create RFC 2822-compliant Message-ID header:

msg-id = [CFWS] "<" id-left "@" id-right ">" [CFWS]
like image 77
eigenein Avatar answered Sep 20 '22 07:09

eigenein