📜  在Python使用 yagmail 发送电子邮件

📅  最后修改于: 2022-05-13 01:55:02.543000             🧑  作者: Mango

在Python使用 yagmail 发送电子邮件

在本文中,我们将了解如何使用yagmail发送电子邮件。 yagmail (Yet Another Gmail) 是Python中的一个模块,用于使用Python发送电子邮件。这个模块什么都不是,只是一个Gmail/SMTP (简单邮件传输协议)客户端,它消除了通过Python发送电子邮件的问题。这有助于 Web 应用程序与 Gmail 交互而不会出现任何问题。

注意:这会使 Gmail 帐户容易受到一些未经授权的访问,因此要在 Gmail 帐户中施加安全性,请使用OAuth2凭据来获取访问权限。

安装:

pip install yagmail

注册用户电子邮件 ID:

通过注册,我们允许yagmail访问我们的 Gmail 帐户以同意发送电子邮件。需要SMTP客户端向客户端提供身份验证以发送电子邮件。

连接到 SMTP 服务器:

要使用 SMTP 客户端启动与 SMTP 服务器的连接,请使用以下命令。

添加内容和交付:

  • 在 send()函数的第一个参数中,传递接收者的电子邮件地址。
  • 然后在第二个中,传递您的发件人发送的邮件的主题。
  • 现在在第三个中,传递邮件的内容,即文本或媒体。

发送简单的电子邮件:

Python3
# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address",
                   "Sender's Email Address Password")
# Adding Content and sending it
yag.send("Reciever's Email Address", 
         "Subject of Email to be send",
         "Content(Text,Media, etc. files) of Email to be send")


Python3
# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address", 
                   "Sender's Email Address Password")
  
# Adding multiple attachments and mailing them
yag.send("Reciever@gmail.com","Subject Of Mail","Content Of Mail",
         attachments=['Attachment1.png','Attachment2.png','Attachment3.png'])


Python3
# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address", 
                   "Sender's Email Address Password")
  
# Adding multiple recipents name in "to" argument
yag.send(to=["recipient1@gmail.com","recipient2@gmail.com",
             "recipient3@gmail.com"],"Subject Of Mail","Content Of Mail")


Python3
# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address", 
                   "Sender's Email Address Password")
  
# Passing other recipients name to cc and bcc arguments
yag.send(to = "Reciever1@gmail.com", cc = "Reciever2@gmail.com",
         bcc = "Reciever1@gmail.com","Subject Of Mail","Content Of Mail")


Python3
# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address",
                   "Sender's Email Address Password")
  
# Passing content inside HTML tags to content argument
yag.send("Reciever@gmail.com","Subject Of Mail",
         "

Content Of Mail

")


发送带有多个附件的电子邮件

在这里,我们将发送一封包含多个附件的电子邮件。在附件中,属性通过必须发送给接收者的附件列表。



代码:

蟒蛇3

# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address", 
                   "Sender's Email Address Password")
  
# Adding multiple attachments and mailing them
yag.send("Reciever@gmail.com","Subject Of Mail","Content Of Mail",
         attachments=['Attachment1.png','Attachment2.png','Attachment3.png'])

输出:

向多个收件人发送电子邮件

在 send()函数的“to”参数中,传递多个收件人电子邮件地址的列表。

代码:

蟒蛇3

# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address", 
                   "Sender's Email Address Password")
  
# Adding multiple recipents name in "to" argument
yag.send(to=["recipient1@gmail.com","recipient2@gmail.com",
             "recipient3@gmail.com"],"Subject Of Mail","Content Of Mail")

输出:



发送带有 CC 和 BCC 字段的电子邮件

在 cc(抄送)中传递 reciever2 电子邮件地址,在第三个即 bcc(密件抄送)中传递 reciever3 电子邮件地址..

代码:

蟒蛇3

# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address", 
                   "Sender's Email Address Password")
  
# Passing other recipients name to cc and bcc arguments
yag.send(to = "Reciever1@gmail.com", cc = "Reciever2@gmail.com",
         bcc = "Reciever1@gmail.com","Subject Of Mail","Content Of Mail")

输出:

发送 HTML 电子邮件

在 HTML 标签内传递邮件的内容。因此,消息将按照您提供的 HTML 语法进行格式化。

代码:

蟒蛇3

# importing yagmail and its packages
import yagmail
  
# initiating connection with SMTP server
yag = yagmail.SMTP("Sender's Email Address",
                   "Sender's Email Address Password")
  
# Passing content inside HTML tags to content argument
yag.send("Reciever@gmail.com","Subject Of Mail",
         "

Content Of Mail

")

输出: