2016-12-07 4 views
2

複数のメールを送信する必要があります(毎日200個のカスタマイズされたメールのように)。アップロード時間を節約するために添付ファイルを1回だけアップロードすることは可能ですか? それよりも優れているのは、ファイルをGoogleサーバーに一度だけアップロードすることが可能で、毎日そのファイルを参照するだけですか?アタッチメントはここでロードさGmailとPythonは同じ添付ファイルで別のメールを送信します

# main function 
def SendMessageAttachment(sender, to, subject, msgHtml, msgPlain, attachmentFile): 
    credentials = get_credentials() 
    http = credentials.authorize(httplib2.Http()) 
    service = discovery.build('gmail', 'v1', http=http) 
    message1 = create_message_with_attachment(sender, to, subject, msgPlain, attachmentFile) 
    SendMessageInternal(service, "me", message1) 

def SendMessageInternal(service, user_id, message): 
    try: 
     message = (service.users().messages().send(userId=user_id, body=message).execute()) 
     print 'Message Id: %s' % message['id'] 
     return message 
    except errors.HttpError, error: 
     print 'An error occurred: %s' % error 


def create_message_with_attachment(
    sender, to, subject, message_text, attachmentFile): 
    """Create a message for an email. 

    Args: 
     sender: Email address of the sender. 
     to: Email address of the receiver. 
     subject: The subject of the email message. 
     message_text: The text of the email message. 
     file: The path to the file to be attached. 

    Returns: 
     An object containing a base64url encoded email object. 
    """ 
    message = MIMEMultipart() 
    message['to'] = to 
    message['from'] = sender 
    message['subject'] = subject 

    msg = MIMEText(message_text) 
    message.attach(msg) 

    print "create_message_with_attachment: file:", attachmentFile 
    content_type, encoding = mimetypes.guess_type(attachmentFile) 

    if content_type is None or encoding is not None: 
     content_type = 'application/octet-stream' 
    main_type, sub_type = content_type.split('/', 1) 
    if main_type == 'text': 
     fp = open(attachmentFile, 'rb') 
     msg = MIMEText(fp.read(), _subtype=sub_type) 
     fp.close() 
    elif main_type == 'image': 
     fp = open(attachmentFile, 'rb') 
     msg = MIMEImage(fp.read(), _subtype=sub_type) 
     fp.close() 
    elif main_type == 'audio': 
     fp = open(attachmentFile, 'rb') 
     msg = MIMEAudio(fp.read(), _subtype=sub_type) 
     fp.close() 
    else: 
     fp = open(attachmentFile, 'rb') 
     msg = MIMEBase(main_type, sub_type) 
     msg.set_payload(fp.read()) 
     fp.close() 
    filename = os.path.basename(attachmentFile) 
    msg.add_header('Content-Disposition', 'attachment', filename=filename) 
    message.attach(msg) 

    return {'raw': base64.urlsafe_b64encode(message.as_string())} 
+0

「BCC」ヘッダーにすべての受信者を含む1つのメールを送信できますか? – onlynone

+0

@onlynone各メールは受信者(名前など)にカスタマイズされているため、1通のメールを送信することはできません。 – apadana

答えて

0

part = MIMEApplication(open("mypdf.pdf","rb").read()) 

ことができるが、ヘッダの参照だけここに参考のため

コード(修正Googleの現像剤サンプルコードからビット)でありますどこでも

part.add_header('Content-Disposition', 'attachment', filename="file.pdf") 
msg.attach(part) 

maを送信する前にこのヘッダーを追加する関数を書くことができますilとすべての受信者を繰り返します。

+0

サーバーにアップロードされているのですか、またはメモリにロードされていますか? – apadana

+0

メモリ内にあります。最初にサーバーを開く必要があります。 –

+0

これは私が正しく理解すれば、すべてのメールの添付ファイルをGmailサーバにアップロードします。 – apadana

関連する問題