Send multiple emails in the same thread using python and gmail
Question: 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"  
    


Answer: 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](http://www.sensefulsolutions.com/2010/08/how-does-email-
threading-work-in-gmail.html) 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.

