1 首先到mailgun官网注册账号,并激活账号
点击domains,进入默认的域名,最底下那个sandbox域名就是默认的测试域名
如果自己有域名,也可以添加自己的域名测试,具体参考:ssr panel魔改前端登陆面板配置邮件服务
前4步添加自己的域名
2 查看域名信息
3 添加测试账号,添加完之后,会受到确认邮件,点击确认之后就可以发送测试邮件
点击上面图片manage Authorized Recipients添加
这个时候进入邮箱确认
4 示例代码
# -*- coding=utf- -*- import smtplib import time #from pymongo import MongoClient from email.mime.text import MIMEText """ people [{"id":"贝贝","address":"lghher1991@163.com"},{"id":"航航","address":"lghher1990@163.com"}] email {"sub":"标题","html":"<html><h1>贝贝</h1></html>"} """ def sendEmail(u_list,sub,content): smtp_host = 'smtp.mailgun.com' account = 'postmaster@sandbox34daf3c7c32248fb9ab29b5468216c7f.mailgun.org' password='3986a06d1a14ab1eb7d19b0a237dc408' msg = MIMEText(content,'html','utf-8') msg["Accept-Language"]="zh-CN" msg["Accept-Charset"]="ISO-8859-1,utf-8" msg['Subject']=sub msg['From']='sunland@126.com' # msg['To']=';'.join(u_list) msg['To']=(u_list) smtp = smtplib.SMTP(smtp_host) smtp.login(account,password) smtp.sendmail(account,u_list,msg.as_string()) smtp.quit() print "发送成功".decode('utf-8').encode('mbcs') user = 'ymnlwyy@sina.com' sub = 'testmailgun' content = 'hello mailgun' sendEmail(user,sub,content)
打开vi 编辑器,复制上面代码,保存并运行代码
vi sendmail.py//复制代码 wq:保存 python sendmail.py
如图,表示运行成功:
此时可以打开自己邮箱,查看发送的邮件
也可以在mailgun的log里面看到发送的邮件
错误汇总:
1 smtplib.SMTPAuthenticationError: (535, '5.7.0 Mailgun is not loving your login or password')
说明用户名(account)和密码(password)填写错误,请刷新domain页面查看密码,填入新的密码,在运行代码
2 smtplib.SMTPDataError: (554, 'Sandbox subdomains are for test purposes only. Please add your own domain or add the address to authorized recipients in Account Settings.')
说明没有添加测试账号,请回到步骤3添加测试邮箱后再运行代码