Python 发送邮件

栏目: Python · 发布时间: 5年前

内容简介:程序人员对于先上示例代码,之后再详解。注:全部代码在Python3环境下测试通过,正常使用,正常显示,无需任何外置模块。

程序人员对于 邮件自动化 的日常需求还是很高的。但是入过了 Linux 的命令行邮件客户端如 Sendmail , Mutt , Alpine 等坑之后,发现现代其实很少人真的在用它们实现邮件自动化,根据搜索引擎里相关文章的数量就可知一二。取而代之的是,现代都在用 PythonPHP 等编程语言直接实现。Python更是自带一套模块实现邮件发送。

先上示例代码,之后再详解。

注:全部代码在Python3环境下测试通过,正常使用,正常显示,无需任何外置模块。

发送HTML格式的漂亮邮件

import smtplib
from email.mime.text import MIMEText

# Settings of sender's server
host = 'smtp.aliyun.com'
sender = 'Jason@aliyun.com'
user = 'Jason@aliyun.com'
password = input('Please type your password: ')
to = ['Jason@outlook.com']

# Content of email
subject = 'Python send html email test'
with open('./test.html', 'r') as f:
    content = f.read()

# Settings of the email string
email = MIMEText(content,'html','utf-8')
email['Subject'] = subject
email['From'] = sender
email['To'] = to[0]
msg = email.as_string()

# Login the sender's server
print('Logging with server...')
smtpObj = smtplib.SMTP() 
smtpObj.connect(host, 25)
smtpObj.login(user, password)
print('Login successful.')

# Send email
smtpObj.sendmail(sender, to, msg) 
smtpObj.quit() 
print('Email has been sent')

发送带附件的邮件

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

# Settings of sender's server
host = 'smtp.aliyun.com'
sender = 'Jason@aliyun.com'
user = 'Jason@aliyun.com'
password = input('Please type your password: ')
to = ['Jason@outlook.com']

# Make content of email
subject = 'Python send email with attachments'
with open('./test.html', 'r') as f:
    content = MIMEText(f.read(),'html','utf-8')
    content['Content-Type'] = 'text/html'
    print('Loaded content.')

# Make txt attachment
with open('./txt.md', 'r') as f:
    txt = MIMEText(f.read(),'plain','utf-8')
    txt['Content-Type'] = 'application/octet-stream'
    txt['Content-Disposition'] = 'attachment;filename="txt.md"'
    print('Loaded txt attachment file.')

# Make image attachment
with open('./pic.png', 'rb') as f:
    img = MIMEImage(f.read())
    img['Content-Type'] = 'application/octet-stream'
    img['Content-Disposition'] = 'attachment;filename="pic.png"'
    print('Loaded image attachment file.')

# Attach content & attachments to email
email = MIMEMultipart()
email.attach(content)
email.attach(txt)
email.attach(img)

# Settings of the email string
email['Subject'] = subject
email['From'] = sender
email['To'] = to[0]
msg = email.as_string()

# Login the sender's server
print('Logging with server...')
smtpObj = smtplib.SMTP() 
smtpObj.connect(host, 25)
smtpObj.login(user, password)
print('Login successful.')

# Send email
smtpObj.sendmail(sender, to, msg) 
smtpObj.quit() 
print('Email has been sent')

发送邮件大杀器:Yagmail

之所以放在最后,是相衬托出传统的发送邮件是多繁琐多麻烦,实际上我们需要的只是超级简单的东西。Yagmail正是为了实现这个而生的,一句话就可以完成所有的登录、发送文字、HTML、附件等功能。

参考Github:yagmail -- Yet Another GMAIL/SMTP client

一句话发送邮件:

yagmail.SMTP('username').send('to@a.com', 'Subject', 'This is the body')

正常一点的发送邮件:

import yagmail

yag = yagmail.SMTP('user', 'password', host='server.com', port='123')
contents = [
    'This is the body, and here is just text http://somedomain/image.png',
    'You can find a file attached.', 
    './dataset/pic.jpg'
]
yag.send('solomonxie@outlook.com', 'yagmail tst', contents)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

新内容创业:我这样打造爆款IP

新内容创业:我这样打造爆款IP

南立新、曲琳 / 机械工业出版社 / 2016-5-10 / 39.00

这是个内容创业爆棚的时代,在采访几十家内容创业公司,与一线最优秀的创业者独家对话之后,作者写作了这本书,其中包括对这个行业的真诚感触,以及希望沉淀下来的体系化思考。 本书共分三个部分讲述了爆红大号的内容创业模式和方法。其中第一部分,讲述了新的生产方式,即内容形态发展的现状--正在被塑造;第二部分,讲述了新的盈利探索,即从贩卖产品到贩卖内容的转变,该部分以多个案例进行佐证,内容翔实;第三部分,......一起来看看 《新内容创业:我这样打造爆款IP》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具