Reputation: 704
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"
Upvotes: 4
Views: 5294
Reputation: 1035
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.)
Upvotes: 9
Reputation: 10846
This doesn't appear to be a question about sending emails, but rather how to organise them to GMail will thread them correctly.
See this page for a description on how threading works. Basically you need subsequent emails to include "Re: " at the start of the subject line. Since you don't show the code that generates the message I can't say how you might do that.
Upvotes: 1