2016-12-07 48 views
2

我需要發送多封電子郵件(如每天200個定製的電子郵件),但都具有相同的pdf附件。是否有可能只上傳附件一次以節省上傳時間? 甚至比這更好,是否有可能在谷歌服務器上只上傳文件一次,每天只需引用該文件?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」標題中所有收件人的電子郵件嗎? – onlynone

+0

@onlynone每封電子郵件都是爲收件人定製的(比如他們的名字),所以不可能發送一封郵件。 – apadana

回答

0

附件被這裏加載:

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

但對於報頭可以是參考任何地方

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

你可以寫一個函數來添加這個頭之前發送馬並遍歷所有的收件人。

+0

它是上傳到服務器還是隻是加載到內存? – apadana

+0

在內存中。它需要在服務器上首先「打開」它。 –

+0

因此,如果我理解正確,這仍然會將每封電子郵件的附件上傳到Gmail服務器。 – apadana