0

以下の 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")

どんな洞察も大歓迎です。

一番、

マット

4

3 に答える 3

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()
于 2013-06-10T15:23:05.657 に答える