如何在 Python 中发送带有 pdf 附件的电子邮件? [英] How to send email with pdf attachment in Python?

查看:161
本文介绍了如何在 Python 中发送带有 pdf 附件的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
如何使用 python 发送电子邮件附件

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

导入smtplibfromaddr = 'myemail@gmail.com'toaddrs = 'youremail@gmail.com'msg = '你好'# 凭据(如果需要)用户名 = '我的邮箱'密码 = 'yyyyyy'# 实际发送的邮件server = smtplib.SMTP('smtp.gmail.com:587')server.starttls()server.login(用户名,密码)server.sendmail(fromaddr, toaddrs, msg)服务器.退出()

解决方案

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

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

然后发送消息.

导入smtplib邮件程序 = smtplib.SMTP()邮件程序连接()mailer.sendmail(from_, to, msg.as_string())邮件程序关闭()

这里有几个例子 - http://docs.python.org/library/email-示例.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):## 学分:http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script从套接字导入 gethostname#导入邮箱从 email.mime.application 导入 MIMEApplication从 email.mime.multipart 导入 MIMEMultipart从 email.mime.text 导入 MIMEText导入 smtplib导入json服务器 = smtplib.SMTP('smtp.gmail.com', 587)server.starttls()使用 open(password_path) 作为 f:配置 = json.load(f)server.login('me@gmail.com', config['password'])# 工艺消息 (obj)msg = MIMEMultipart()message = f'{message}
从主机名发送:{gethostname()}'msg['主题'] = 主题msg['From'] = 'me@gmail.com'msg['To'] = 目的地# 将文本插入到通过电子邮件发送的 msg 中msg.attach(MIMEText(message, "plain"))# 将 pdf 附加到通过电子邮件发送的 msg使用 open(path_to_pdf, rb") 作为 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(附加)# 发送消息server.send_message(msg)

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

Possible Duplicate:
How to send Email Attachments with python

I would like to edit the following code and send an email with an attachment. Attachment is a pdf file, it is under /home/myuser/sample.pdf, in linux environment. What should I change below?

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()  

解决方案

You create a message with an email package in this case -

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()))

and then send the message.

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

Several examples here - http://docs.python.org/library/email-examples.html

UPDATE

Updating the link since the above yields a 404 https://docs.python.org/2/library/email-examples.html. Thanks @Tshirtman


Update2: Simplest way to attach pdf

To attach the pdf use the pdf flag:

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}
Send 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)

inspirations/credits to: http://linuxcursor.com/python-programming/06-how-to-send-pdf-ppt-attachment-with-html-body-in-python-script

这篇关于如何在 Python 中发送带有 pdf 附件的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆