使用python和gmail在同一线程中发送多封电子邮件 [英] Send multiple emails in the same thread using python and gmail

查看:105
本文介绍了使用python和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"  

免责声明:我在Stack Overflow的某处找到了此代码。不是我的我删去了某些部分,因为它们似乎没有特殊含义。

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.

有时候我的代码得到了很多结果,并且我在不到20秒的时间内收到了150多封不同的电子邮件

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)  

而不是:

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

编辑

要解决该问题,我要做的就是重新插入以前删除的代码部分。完整的功能是这样的:

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"  


推荐答案

这似乎不是发送电子邮件的问题,而是如何组织电子邮件GMail将正确地对其进行线程化。

This doesn't appear to be a question about sending emails, but rather how to organise them to GMail will thread them correctly.

请参见此页面中有关线程如何工作的描述。基本上,您需要后续的电子邮件在主题行的开头包含 Re:。由于您没有显示生成消息的代码,因此我无法说出您可能会怎么做。

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.

这篇关于使用python和gmail在同一线程中发送多封电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆