I have a program running. When that program gets a result, it sends me an email using this function:
def send_email(message):
import smtplib
gmail_user = OMITTED
gmail_pwd = OMITTED
FROM = OMITTED
TO = OMITTED #must be a list
try:
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
Disclaimer: I found this code somewhere here on Stack Overflow. It is not mine. I cut out some parts of it as they seemed to have no special meaning.
Sometimes my code gets many results, and I get 150+ different emails in less than 20 seconds.
How can I modify the function above in order for the program to send me all the results in the same thread?
In case you are not getting what my idea is, I want my inbox to look like this:
[email protected](150) ...
... (other emails from other senders)
instead of:
[email protected] ...
[email protected] ...
[email protected] ...
[email protected] ...
[email protected] ...
...
[email protected] ...
... (other emails from other senders)
EDIT
To solve the problem, all I needed to do was reinsert the parts of the code I had previously deleted. The full function is this one:
def send_email(TEXT):
import smtplib
gmail_user = OMITTED
gmail_pwd = OMITTED
FROM = OMITTED
TO = OMITTED #must be a list
SUBJECT = "Big brother candidate"
#TEXT = "Testing sending mail using gmail servers"
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
#server = smtplib.SMTP(SERVER)
server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
#server.quit()
server.close()
print 'successfully sent the mail'
except:
print "failed to send mail"
This is an old question, but I felt compelled to answer it because there is a way to do what the OP wanted. You can achieve by adding headers to your messages, and reference them when sending another email. For example
from email.utils import make_msgid
my_id = make_msgid()
#Build your email as you normally do, and add ID as a message header
message = MIMEMultipart()
message["Message-ID"] = my_id
message["Subject"] = "test"
message["From"] = from_email
# ...etc and send your email using smtp.sendmail
# On the reply (or when sending another email), add the following headers
message["In-Reply-To"] = my_id
message["References"] = my_id
# ...send your email using smtp.sendmail
When you check your mail client, you will see that the latter email will be posted as a reply to the former email thus creating the thread that you normally see in the popular email clients (Gmail, Inbox, Outlook, Yahoo, etc.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With