【问题标题】:How to send email with pdf attachment in Python? [duplicate]如何在 Python 中发送带有 pdf 附件的电子邮件? [复制]
【发布时间】:2012-08-08 21:47:47
【问题描述】:

可能重复:
How to send Email Attachments with python

我想编辑以下代码并发送带有附件的电子邮件。附件是一个pdf文件,在/home/myuser/sample.pdf下,linux环境下。下面我应该改变什么?

import smtplib  
fromaddr = 'myemail@gmail.com'  
toaddrs  = 'youremail@gmail.com'  
msg = 'Hello'  


# Credentials (if needed)  
username = 'myemail'  
password = 'yyyyyy'  

# The actual mail send  
server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(username,password)  
server.sendmail(fromaddr, toaddrs, msg)  
server.quit()  

【问题讨论】:

  • 现在几乎是一个问题。你能解释一下为什么谷歌搜索“smtplib 附加文件”的第一页的结果都不合适吗?
  • 使用email模块,docs.python.org/library/email-examples.html中的例子
  • 试试这个教程email_attachments。正如其他答案所提到的,它也在使用 MIME。
  • 在这里问这个问题而不是在谷歌上搜索“smtplib attach file”有什么问题?
  • 当您在 Google 上搜索“smtplib 附加文件”时,它现在是热门搜索之一。 :-)

标签: python email email-attachments smtplib


【解决方案1】:

推荐的方法是使用 Python 的 email 模块,以便正确撰写 格式化的 MIME 消息。查看文档

对于python 2
https://docs.python.org/2/library/email-examples.html

对于python 3
https://docs.python.org/3/library/email.examples.html

【讨论】:

  • 此链接现已损坏
  • 如果我想发送svg 文件怎么办?
【解决方案2】:

在这种情况下,您使用电子邮件包创建消息 -

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
msg = MIMEMultipart()
msg.attach(MIMEText(open("/home/myuser/sample.pdf").read()))

然后发送消息。

import smtplib
mailer = smtplib.SMTP()
mailer.connect()
mailer.sendmail(from_, to, msg.as_string())
mailer.close()

这里有几个例子 - http://docs.python.org/library/email-examples.html

更新

更新链接,因为上面会产生 404 https://docs.python.org/2/library/email-examples.html。谢谢@Tshirtman


更新 2:附加 pdf 的最简单方法

要附加 pdf,请使用 pdf 标志:

def send_email_pdf_figs(path_to_pdf, subject, message, destination, password_path=None):
    ## credits: http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script
    from socket import gethostname
    #import email
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    import smtplib
    import json

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    with open(password_path) as f:
        config = json.load(f)
        server.login('me@gmail.com', config['password'])
        # Craft message (obj)
        msg = MIMEMultipart()

        message = f'{message}\nSend from Hostname: {gethostname()}'
        msg['Subject'] = subject
        msg['From'] = 'me@gmail.com'
        msg['To'] = destination
        # Insert the text to the msg going by e-mail
        msg.attach(MIMEText(message, "plain"))
        # Attach the pdf to the msg going by e-mail
        with open(path_to_pdf, "rb") as f:
            #attach = email.mime.application.MIMEApplication(f.read(),_subtype="pdf")
            attach = MIMEApplication(f.read(),_subtype="pdf")
        attach.add_header('Content-Disposition','attachment',filename=str(path_to_pdf))
        msg.attach(attach)
        # send msg
        server.send_message(msg)

灵感/致谢:http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script

【讨论】:

  • 我不知道自回答以来这是否发生了变化。但现在对我来说,适用于 pdf 的类型是 MIMEApplication
  • 您提供的代码中的file 是什么?是 Python 函数吗?
  • 对于 python3.7x,这些 MIME* 类现在可以导入为:from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart
  • file 给出错误。这是什么?
  • 我投-1,因为不清楚什么是file()
猜你喜欢
  • 2011-10-09
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 2020-11-20
  • 2014-02-28
  • 1970-01-01
  • 2014-05-29
相关资源
最近更新 更多