在即时通讯软件如此发达的今天,电子邮件仍然是互联网上使用最为普遍的应用之一,公司向应聘者发出录用通知、网站向用户发送一个激活帐号的连接、银行向客户推广它们的理财产品等几乎都是经过电子邮件来完成的,而这些任务应该都是由程序自动完成的。php
就像咱们能够用HTTP(超文本传输协议)来访问一个网站同样,发送邮件要使用SMTP(简单邮件传输协议),SMTP也是一个创建在TCP(传输控制协议)提供的可靠数据传输服务的基础上的应用级协议,它规定了邮件的发送者如何跟发送邮件的服务器进行通讯的细节,而Python中的smtplib模块将这些操做简化成了几个简单的函数。python
下面的代码演示了如何在Python发送邮件。git
from smtplib import SMTP from email.header import Header from email.mime.text import MIMEText def main(): # 请自行修改下面的邮件发送者和接收者 sender = 'abcdefg@126.com' receivers = ['uvwxyz@qq.com', 'uvwxyz@126.com'] message = MIMEText('用Python发送邮件的示例代码.', 'plain', 'utf-8') message['From'] = Header('王大锤', 'utf-8') message['To'] = Header('骆昊', 'utf-8') message['Subject'] = Header('示例代码实验邮件', 'utf-8') smtper = SMTP('smtp.126.com') # 请自行修改下面的登陆口令 smtper.login(sender, 'secretpass') smtper.sendmail(sender, receivers, message.as_string()) print('邮件发送完成!') if __name__ == '__main__': main()
若是要发送带有附件的邮件,那么能够按照下面的方式进行操做。github
from smtplib import SMTP from email.header import Header from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart import urllib def main(): # 建立一个带附件的邮件消息对象 message = MIMEMultipart() # 建立文本内容 text_content = MIMEText('附件中有本月数据请查收', 'plain', 'utf-8') message['Subject'] = Header('本月数据', 'utf-8') # 将文本内容添加到邮件消息对象中 message.attach(text_content) # 读取文件并将文件做为附件添加到邮件消息对象中 with open('/Users/Hao/Desktop/hello.txt', 'rb') as f: txt = MIMEText(f.read(), 'base64', 'utf-8') txt['Content-Type'] = 'text/plain' txt['Content-Disposition'] = 'attachment; filename=hello.txt' message.attach(txt) # 读取文件并将文件做为附件添加到邮件消息对象中 with open('/Users/Hao/Desktop/汇总数据.xlsx', 'rb') as f: xls = MIMEText(f.read(), 'base64', 'utf-8') xls['Content-Type'] = 'application/vnd.ms-excel' xls['Content-Disposition'] = 'attachment; filename=month-data.xlsx' message.attach(xls) # 建立SMTP对象 smtper = SMTP('smtp.126.com') # 开启安全链接 # smtper.starttls() sender = 'abcdefg@126.com' receivers = ['uvwxyz@qq.com'] # 登陆到SMTP服务器 # 请注意此处不是使用密码而是邮件客户端受权码进行登陆 # 对此有疑问的读者能够联系本身使用的邮件服务器客服 smtper.login(sender, 'secretpass') # 发送邮件 smtper.sendmail(sender, receivers, message.as_string()) # 与邮件服务器断开链接 smtper.quit() print('发送完成!') if __name__ == '__main__': main()
发送短信也是项目中常见的功能,网站的注册码、验证码、营销信息基本上都是经过短信来发送给用户的。在下面的代码中咱们使用了互亿无线短信平台(该平台为注册用户提供了50条免费短信以及经常使用开发语言发送短信的demo,能够登陆该网站并在用户自服务页面中对短信进行配置)提供的API接口实现了发送短信的服务,固然国内的短信平台不少,读者能够根据本身的须要进行选择(一般会考虑费用预算、短信达到率、使用的难易程度等指标),若是须要在商业项目中使用短信服务建议购买短信平台提供的套餐服务。web
import urllib.parse
import http.client import json def main(): host = "106.ihuyi.com" sms_send_uri = "/webservice/sms.php?method=Submit" # 下面的参数须要填入本身注册的帐号和对应的密码 params = urllib.parse.urlencode({'account': '你本身的帐号', 'password' : '你本身的密码', 'content': '您的验证码是:147258。请不要把验证码泄露给其余人。', 'mobile': '接收者的手机号', 'format':'json' }) print(params) headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain'} conn = http.client.HTTPConnection(host, port=80, timeout=30) conn.request('POST', sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() jsonstr = response_str.decode('utf-8') print(json.loads(jsonstr)) conn.close() if __name__ == '__main__': main()