向多个收件人发送电子邮件

时间:2013-06-10 15:16:10

标签: python email

我正在尝试使用下面的pyton脚本向多个收件人发送电子邮件。我在论坛上搜索了答案,但未能正确实现其中任何一个。如果有人有时间审查我的脚本并发现/解决问题,我们将不胜感激。

这是我的脚本,我收集的问题是在'sendmail'部分,但无法弄清楚如何修复它:

gmail_user = "sender@email.com"
gmail_pwd = "sender_password"
recipients = ['recipient1@email.com','recipient2@email.com']

def mail(to, subject, text, attach):
   msg = MIMEMultipart()
   msg['From'] = gmail_user
   msg['To'] = ", ".join(recipients)
   msg['Subject'] = subject

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   mailServer.close()

mail("recipient1@email.com, recipient2@email.com",
   "Subject",
   "Message",
   "attchachment")

非常感谢任何见解。

最佳,

马特

3 个答案:

答案 0 :(得分:3)

应该更像是

mail(["recipient1@email.com", "recipient2@email.com"],
   "Subject",
   "Message", 
   "attchachment")

您已经声明了一组收件人,这些收件人也是全局的,您可以使用它而不将其作为参数传递给mail

答案 1 :(得分:0)

我写了这段代码来做你想要的。如果你发现了一个错误让我知道(我已经测试了它并且它有效):

import email as em
import smtplib as smtp
import os

ENDPOINTS = {KEY: 'value@domain.com'}


class BoxWriter(object):

    def __init__(self):
        pass

    def dispatch(self, files, box_target, additional_targets=None, email_subject=None, body='New figures'):
        """
        Send an email to multiple recipients

        :param files: list of files to send--requires full path
        :param box_target: Relevant entry ENDPOINTS dict
        :param additional_targets: other addresses to send the same email
        :param email_subject: optional title for email
        """

        destination = ENDPOINTS.get(box_target, None)
        if destination is None:
            raise Exception('Target folder on Box does not exist')

        recipients = [destination]
        if additional_targets is not None:
            recipients.extend(additional_targets)

        subject = 'Updating files'
        if email_subject is not None:
            subject = email_subject

        message = em.MIMEMultipart.MIMEMultipart()
        message['From'] = 'user@domain.com'
        message['To'] = ', '.join(recipients)
        message['Date'] = em.Utils.formatdate(localtime=True)
        message['Subject'] = subject

        message.attach(em.MIMEText.MIMEText(body + '\n' +'Contents: \n{0}'.format('\n'.join(files))))

        for f in files:
            base = em.MIMEBase.MIMEBase('application', "octet-stream")
            base.set_payload(open(f, 'rb').read())
            em.Encoders.encode_base64(base)
            base.add_header('Content-Disposition', 'attachment; filename={0}'.format(os.path.basename(f)))
            message.attach(base)

        conn = smtp.SMTP('smtp.gmail.com', 587)
        un = 'user@gmail.com'
        pw = 'test1234'
        conn.starttls()
        conn.login(un, pw)
        conn.sendmail('user@domain.com', recipients, message.as_string())
        conn.close()

答案 2 :(得分:0)

我遇到了同样的问题,我现在解决了这个问题。这是我的代码-

{"Animais-1.ttf": "http://ggt-des.br/styles/fontes/Animais-1.ttf",
"Animais-2.ttf": "http://ggt-des.br/styles/fontes/Animais-2.ttf",
"Animais.ttf": "http://ggt-des.br/styles/fontes/Animais.ttf",
"CPRM_Fontes.ttf": "http://ggt-des.br/styles/fontes/CPRM_Fontes.ttf",
"ESRI-Electric.ttf": "http://ggt-des.br/styles/fontes/ESRI-AMFM-Electric.ttf",
"ESRI-AMFM-Gas.ttf": "http://ggt-des.br/styles/fontes/ESRI-AMFM-Gas.ttf"}