使用 Python 发送电子邮件

使用 Python 发送电子邮件

在实际运用中,我们可能需要通过电子邮件告诉我们 Python 运行的结果的是否与预期的一致。现在我们就来看一下,怎么通过 Python 发送邮件。

这个示例中我们需要用到的第三方库有:

  • smtplib
  • email.mime.text 里面的 MIMEText
  • email.header 里面的 Header

* 如果没有对应的库,需要使用 pip install 进行安装

代码

import smtplib
from email.mime.text import MIMEText
from email.header import Header


subject = "Python E-Mail Test"
sender = "<the email address of the sender>"
to_receiver = ["<the email address of the receiver>"]
cc_receiver = ["<the email address of the CC receiver>"]
receiver = to_receiver + cc_receiver

message = MIMEText("Hi,\n\nThis is Python Challenge test email, please ignore it. \n\nRegards,\nAaron" , "plain", "UTF-8")
message["Subject"] = Header(subject, "UTF-8")
message["From"] = sender
message['To'] = ";".join(to_receiver)
message["Cc"] = ";".join(cc_receiver)

try:
    smtpObj = smtplib.SMTP('<smtp server  address>')
    smtpObj.sendmail(sender, receiver, message.as_string())
    print("Email sent successfully")
except smtplib.SMTPException:
    print("Error")

代码说明

使用 importfrom … import … 导入第三方库

import smtplib
from email.mime.text import MIMEText
from email.header import Header

通过变量指定邮件主题,收件人等信息,其中 subject 是邮件的主题;sender 是发件人的地址;to_ receiver 是收件人的邮件地址,是一个列表,你可以在这个列表中添加多个收件人的地址;cc_receiver 是抄送的邮件地址,也是一个列表,在这个列表中也可以添加多个抄送地址;将 to_ receiver 和 cc_receiver 合并,就是所有收件人的地址。

subject = "Python E-Mail Test"
sender = "<the email address of the sender>"
to_receiver = ["<the email address of the receiver>"]
cc_receiver = ["<the email address of the CC receiver>"]
receiver = to_receiver + cc_receiver

通过 MIMEText 编写邮件正文,正文的内容有三部分,第一部分是你要发送的邮件内容,可以使用转义字符(如,\n, \t 等)改变内容的格式;然后指定邮件的文本格式,我们使用的是 plain(纯文本),你也可以将 plain 换成 html,使用 html 格式;最后面是内容的编码,我使用的是 UTF-8;最后将这些内容赋值给一个变量 message,这个就使我们我们要发送的内容。

message = MIMEText("Hi,\n\nThis is Python Challenge test email, please ignore it. \n\nRegards,\nAaron" , "plain", "UTF-8")

将前面定义的邮件主题变量 subject 通过 hearder 添加到邮件内容中,然后在邮件的 from 栏中添加 sender 的邮件地址;将 to_receiver 变量中的地址添加到邮件的 to 栏中,如果 to_receiver 中有多个地址,则使用 **;**隔开;将 cc_receiver 中的邮件地址,添加到邮件的 cc 栏中,如果 cc_receiver 有多个地址,则使用 **;**隔开。

message["Subject"] = Header(subject, "UTF-8")
message["From"] = sender
message['To'] = ";".join(to_receiver)
message["Cc"] = ";".join(cc_receiver)

try … except … 是 Python 中的异常处理,可以在出错的情况下,跳过错误,继续运行,在本示例中,这是代码的最后一部分,不管出没出错,这段代码运行完都会结束,所有我们这里的异常处理,就只有 print 一个 “Error” 信息。

使用 smtplib.SMTP 指定发送邮件使用的 SMTP 服务器地址,并将赋值给一个变量 smtp;然后使用 sendmail 发送电子邮件,并指定发件人 sender,所有收件人 receiver,以及使用 message.as_string() 将 message 的 MIMEText 对象转换成 str;使用 smtp.quit() 结束 SMTP 会话,并 print 一个信息,告诉你邮件发送已经完成。

try:
    smtp = smtplib.SMTP('<smtp server  address>')
    smtp.sendmail(sender, receiver, message.as_string())
    smtp.quit()
    print("Email sent successfully")
except smtplib.SMTPException:
    print("Error")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发送电子邮件,您可以使用Python内置的smtplib模块。以下是发送电子邮件的基本步骤: 1.导入smtplib模块并创建SMTP对象来连接SMTP邮件服务器。 ``` import smtplib smtp_server = smtplib.SMTP('smtp.example.com', 587) smtp_server.starttls() smtp_server.login('your_email@example.com', 'your_password') ``` 2.创建MIMEText对象来设置邮件正文和主题。 ``` from email.mime.text import MIMEText message = MIMEText('Hello, this is the message body') message['Subject'] = 'Test email from Python' message['From'] = 'your_email@example.com' message['To'] = 'recipient@example.com' ``` 3.使用SMTP对象发送电子邮件。 ``` smtp_server.sendmail('your_email@example.com', 'recipient@example.com', message.as_string()) smtp_server.quit() ``` 完整代码示例: ``` import smtplib from email.mime.text import MIMEText smtp_server = smtplib.SMTP('smtp.example.com', 587) smtp_server.starttls() smtp_server.login('your_email@example.com', 'your_password') message = MIMEText('Hello, this is the message body') message['Subject'] = 'Test email from Python' message['From'] = 'your_email@example.com' message['To'] = 'recipient@example.com' smtp_server.sendmail('your_email@example.com', 'recipient@example.com', message.as_string()) smtp_server.quit() ``` 请注意,您需要将“smtp.example.com”替换为您的SMTP服务器地址,并使用正确的端口号。同时,您需要将“your_email@example.com”和“your_password”替换为您的电子邮件地址和密码,将“recipient@example.com”替换为收件人的电子邮件地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值