📜  使用Python从您的 Gmail 帐户发送邮件(1)

📅  最后修改于: 2023-12-03 14:49:49.794000             🧑  作者: Mango

使用Python从您的Gmail帐户发送邮件

使用Python编写脚本来发送电子邮件是一项很有用的技能。在本文中,我们将探讨如何使用Python从Gmail帐户发送电子邮件。

前提条件

在继续之前,请确保已满足以下条件:

  • 您已经拥有一个Gmail帐户。
  • 您已经使用此帐户登录过您搜索的计算机。
安装所需的库

要使用Python发送电子邮件,我们需要使用smtplib库来建立SMTP连接并发送电子邮件。

如下是在terminal命令行中用pip安装。

pip install smtplib
登录Gmail帐户

要发送电子邮件,我们需要使用Gmail的SMTP服务器。 Gmail要求我们使用SSL加密连接到SMTP服务器。我们需要先登录我们的Gmail帐户才能发送电子邮件。

import smtplib

smtp_server = "smtp.gmail.com"
port = 587
sender_email = "your_email_address@gmail.com"
# Replace password with your Gmail account password
password = "your_password"

context = ssl.create_default_context()

with smtplib.SMTP(smtp_server, port) as server:
    server.starttls(context=context)
    server.login(sender_email, password)
    # TODO: Send email here

在上面的代码中,我们使用smtplib库建立了SMTP连接并登录了Gmail帐户。使用上的凭证确保这些凭证不会保存到代码库或共享的位置中。

发送电子邮件

现在我们已经登录了我们的Gmail帐户并准备好发送电子邮件了。有两种方法可以发送电子邮件:通过纯文本或HTML消息。

发送纯文本消息
import smtplib
import ssl

smtp_server = "smtp.gmail.com"
port = 587
sender_email = "your_email_address@gmail.com"
receiver_email = "receiver_email@gmail.com"
# Replace password with your Gmail account password
password = "your_password"
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()

with smtplib.SMTP(smtp_server, port) as server:
    server.starttls(context=context)
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

在上面的代码中,我们定义了一些变量:

  • sender_email:发送电子邮件的Gmail帐户。
  • receiver_email:接收邮件的收件人Gmail地址。
  • message:邮件消息的内容和格式。

我们使用smptlib.SMTP.sendmail()方法来发送邮件,并指定发送方和接收方的电子邮件地址,以及消息的内容。

发送HTML消息
import smtplib
import ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage


smtp_server = "smtp.gmail.com"
port = 587
sender_email = "your_email_address@gmail.com"
receiver_email = "receiver_email@gmail.com"
# Replace password with your Gmail account password
password = "your_password"

message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email

# Create the plain-text and HTML version of your message
text = """\
Hi,
How are you?
"""
html = """\
<html>
  <body>
    <p>Hi,<br>
       How are you?<br>
       <img src="cid:image1">
    </p>
  </body>
</html>
"""

# Turn these into plain/html MIMEText objects
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)

# attach an image to the email
img_file = 'image.jpg'
with open(img_file, "rb") as f:
    img_data = f.read()

image = MIMEImage(img_data, name=os.path.basename(img_file))
message.attach(image)

context = ssl.create_default_context()

with smtplib.SMTP(smtp_server, port) as server:
    server.starttls(context=context)
    server.login(sender_email, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )

在上面的代码中,我们使用email.mime模块创建一个HTML消息。我们在邮件的主体中包含一个图像,并在此处定义电子邮件正文的内容,使其更具人性化。我们使用.attach()方法将消息文本添加到邮件中,使用.attach(image)方法将图像添加到邮件中并以正确的格式呈现出来。

最后,我们通过sendmail()方法发送电子邮件。

结论

如此简单,使用Python发送电子邮件。我们已经了解了如何使用smtplib和ssl库。我们已经讨论了两种发送电子邮件的方法:通过文本消息或HTML消息。你已经准备好开始发送邮件了!

更多关于 smtplib 库的信息,请访问 https://docs.python.org/3/library/smtplib.html