这个博客网站,是我扒别人的框架,正在修改中,有的功能不对,你们要小心点。
必备工具
-
Amazon aws account AWS 中有个叫Amazon Simple Email Service,可以提供SMTP服务,达到第三方发送邮件的功能。
-
两个邮箱,一个用来发送,一个用来接收。在后续步骤中,将不再需要接收邮箱。
操作流程
-
前往AWS官网注册开发者账号 点我点我 在网页右上角(正常人都会注册吧。。)
-
注册完毕后登录,在AWS管理控制台中,查找服务中搜索Simple Email Service. 点击它进入如下所示页面。
-
点击左侧 Email Addresses,然后Verify a New Email Address.将准备的两个邮箱分别验证,它会发送验证邮件到邮箱,点击即可完成验证。
-
点击左侧 SMTP Settings, 然后点击Create My SMTP Credentials. 记住IAM User Name,点击右下角的创建按钮。你将会得到SMTP安全凭证。如下所示 至此为止,aws 已经允许你使用它的服务来发送邮件。
-
接下来就是如何使用Python代码来发送邮件了。官网给出了各种编程语言的最佳代码示例。 Example 这里我主要介绍Python。
import smtplib
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Replace sender@example.com with your "From" address.
# This address must be verified.
SENDER = 'sender@example.com'
SENDERNAME = 'Sender Name'
# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = 'recipient@example.com'
# Replace smtp_username with your Amazon SES SMTP user name.
USERNAME_SMTP = "smtp_username"
# Replace smtp_password with your Amazon SES SMTP password.
PASSWORD_SMTP = "smtp_password"
# (Optional) the name of a configuration set to use for this message.
# If you comment out this line, you also need to remove or comment out
# the "X-SES-CONFIGURATION-SET:" header below.
CONFIGURATION_SET = "ConfigSet"
# If you're using Amazon SES in an AWS Region other than 美国西部(俄勒冈),
# replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
# endpoint in the appropriate region.
HOST = "email-smtp.us-west-2.amazonaws.com"
PORT = 587
# The subject line of the email.
SUBJECT = 'Amazon SES Test (Python smtplib)'
# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Amazon SES Test\r\n"
"This email was sent through the Amazon SES SMTP "
"Interface using the Python smtplib package."
)
# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
<h1>Amazon SES SMTP Email Test</h1>
<p>This email was sent with Amazon SES using the
<a href='https://www.python.org/'>Python</a>
<a href='https://docs.python.org/3/library/smtplib.html'>
smtplib</a> library.</p>
</body>
</html>
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = SUBJECT
msg['From'] = email.utils.formataddr((SENDERNAME, SENDER))
msg['To'] = RECIPIENT
# Comment or delete the next line if you are not using a configuration set
msg.add_header('X-SES-CONFIGURATION-SET',CONFIGURATION_SET)
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(BODY_TEXT, 'plain')
part2 = MIMEText(BODY_HTML, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Try to send the message.
try:
server = smtplib.SMTP(HOST, PORT)
server.ehlo()
server.starttls()
#stmplib docs recommend calling ehlo() before & after starttls()
server.ehlo()
server.login(USERNAME_SMTP, PASSWORD_SMTP)
server.sendmail(SENDER, RECIPIENT, msg.as_string())
server.close()
# Display an error message if something goes wrong.
except Exception as e:
print ("Error: ", e)
else:
print ("Email sent!")
- Emmm,下个博客在介绍具体意思吧(很久很久以后。。),反正也不难你们自己能看懂。下个博客我会介绍怎么写HTML email(就是花里胡哨的),如何不需要验证就可以发送给别人邮件,如何用在项目里(比如,用户注册发送notification 邮件。但是我最近很忙,这篇纯属是练手,毕竟github io创建了不能空白,所以我可能鸽了你们。