Python 脚本 - 发送电子邮件
Python Script - Sending an E-Mail
我想知道"what is the best way to send an e-mail with attachment through a python script"?
我应该使用“subprocess”并使用此命令行
"mail -s "Test" email@googlemail.com < file.txt"
另一个选项是 smtplib https://docs.python.org/2/library/email-examples.html ?
哪个选项更好?
Python 是更灵活的电子邮件发送方式。而且您已经在使用 Python,这将是最明智的。无需使用子进程调用外部脚本(这可能是不安全的)。
您可以附加更多不同类型的文件,更好地控制正文的内容。如果你愿意,你可以把它变成一个通用函数或class,它可以被赋予文本和文件名、收件人等
如果你有一个像上面那样的 class,你可以将它导入到其他程序中,在那里它可以用于调试,或者在发生错误(或发生一些有趣的事情)时发送标志。
我倾向于使用它来通知我一些自动化进程的健康状况 运行。
正如@hiroprotagonist 提到的那样 - 这将使脚本平台独立。
您所链接的文档中的这个稍微精简的示例就是您真正需要知道的全部内容:
# Import smtplib for the actual sending function
import smtplib
# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Subject'
me = 'email@email.com'
recipient = 'recipient@recipient.com'
msg['From'] = me
msg['To'] = recipient
# Assume we know that the image files are all in PNG format
for file in pngfiles:
# Open the files in binary mode. Let the MIMEImage class automatically
# guess the specific image type.
fp = open(file, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, recipient, msg.as_string())
s.quit()
yagmail(我是开发者)的全部目的是让发送电子邮件变得非常容易,尤其是在 HTML 或需要附件的情况下。
请尝试以下代码:
import yagmail
yag = yagmail.SMTP('email@email.com', 'password')
contents = ['See my attachment below', '/home/you/some_file.txt']
yag.send('recipient@recipient.com', subject = 'Subject', contents = contents)
注意这里的神奇之处:contents
是一个列表,其中等于文件路径的项目将自动加载、猜测并附加 mimetype。
还有更多魔法,例如易于嵌入的图像、无密码脚本、无用户名脚本、简单的别名、智能默认值等等。我 advise/encourage 你去阅读它的 github 页面 :-)。欢迎提出问题或添加功能请求!
yagmail可以通过pip安装获取:
pip install yagmail # Python 2
pip3 install yagmail # Python 3
我想知道"what is the best way to send an e-mail with attachment through a python script"? 我应该使用“subprocess”并使用此命令行
"mail -s "Test" email@googlemail.com < file.txt"
另一个选项是 smtplib https://docs.python.org/2/library/email-examples.html ?
哪个选项更好?
Python 是更灵活的电子邮件发送方式。而且您已经在使用 Python,这将是最明智的。无需使用子进程调用外部脚本(这可能是不安全的)。
您可以附加更多不同类型的文件,更好地控制正文的内容。如果你愿意,你可以把它变成一个通用函数或class,它可以被赋予文本和文件名、收件人等
如果你有一个像上面那样的 class,你可以将它导入到其他程序中,在那里它可以用于调试,或者在发生错误(或发生一些有趣的事情)时发送标志。
我倾向于使用它来通知我一些自动化进程的健康状况 运行。
正如@hiroprotagonist 提到的那样 - 这将使脚本平台独立。
您所链接的文档中的这个稍微精简的示例就是您真正需要知道的全部内容:
# Import smtplib for the actual sending function
import smtplib
# Here are the email package modules we'll need
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
# Create the container (outer) email message.
msg = MIMEMultipart()
msg['Subject'] = 'Subject'
me = 'email@email.com'
recipient = 'recipient@recipient.com'
msg['From'] = me
msg['To'] = recipient
# Assume we know that the image files are all in PNG format
for file in pngfiles:
# Open the files in binary mode. Let the MIMEImage class automatically
# guess the specific image type.
fp = open(file, 'rb')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)
# Send the email via our own SMTP server.
s = smtplib.SMTP('localhost')
s.sendmail(me, recipient, msg.as_string())
s.quit()
yagmail(我是开发者)的全部目的是让发送电子邮件变得非常容易,尤其是在 HTML 或需要附件的情况下。
请尝试以下代码:
import yagmail
yag = yagmail.SMTP('email@email.com', 'password')
contents = ['See my attachment below', '/home/you/some_file.txt']
yag.send('recipient@recipient.com', subject = 'Subject', contents = contents)
注意这里的神奇之处:contents
是一个列表,其中等于文件路径的项目将自动加载、猜测并附加 mimetype。
还有更多魔法,例如易于嵌入的图像、无密码脚本、无用户名脚本、简单的别名、智能默认值等等。我 advise/encourage 你去阅读它的 github 页面 :-)。欢迎提出问题或添加功能请求!
yagmail可以通过pip安装获取:
pip install yagmail # Python 2
pip3 install yagmail # Python 3