简体   繁体   中英

Send multiple emails in the same thread using python and gmail

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:

sender@gmail.com(150) ...  
... (other emails from other senders)  

instead of:

sender@gmail.com ...  
sender@gmail.com ...  
sender@gmail.com ...  
sender@gmail.com ...  
sender@gmail.com ...  
...  
sender@gmail.com ...  
... (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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM