【问题标题】:Automatically send emails using an Alias使用别名自动发送电子邮件
【发布时间】:2019-01-03 16:04:51
【问题描述】:
我正在创建一个应用程序,它可以自动向 Google 群组的不同用户发送电子邮件,其中电子邮件发件人是 Google 群组地址。
该应用程序是用 Python 编写的,并使用 Mandrill 发送电子邮件。电子邮件的分发工作正常,但我需要发件人电子邮件成为 Google 组。我将它设置为我的 Gmail 上的别名,这允许我手动选择别名并从 Google 网上论坛地址发送电子邮件。我正在寻找一种方法来自动从别名发送电子邮件,而无需从 Gmail 手动发送。
【问题讨论】:
标签:
gmail
gmail-imap
google-groups
【解决方案1】:
这里是如何使用 python 通过 SMTP 发送电子邮件的代码示例。您可以配置“发件人”字段,以便将其用作发件人。请注意正在使用的python库:smtplib、os和email。
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
msg = MIMEMultipart('alternative')
msg['Subject'] = "Hello from Mandrill, Python style!"
msg['From'] = "John Doe <john@doe.com>" # Your from name and email address
msg['To'] = "recipient@example.com"
text = "Mandrill speaks plaintext"
part1 = MIMEText(text, 'plain')
html = "<em>Mandrill speaks <strong>HTML</strong></em>"
part2 = MIMEText(html, 'html')
username = os.environ['MANDRILL_USERNAME']
password = os.environ['MANDRILL_PASSWORD']
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP('smtp.mandrillapp.com', 587)
s.login(username, password)
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
欲了解更多信息,请查看此链接How to Send via SMTP with Popular Programming Languages?
【解决方案2】:
您可以尝试使用Users.settings.sendAs 资源。
与发送为别名关联的设置,可以是
与帐户关联的主登录地址或自定义“发件人”
地址。发送别名对应于"Send Mail As" 中的功能
网页界面。
{
"sendAsEmail": string,
"displayName": string,
"replyToAddress": string,
"signature": string,
"isPrimary": boolean,
"isDefault": boolean,
"treatAsAlias": boolean,
"smtpMsa": {
"host": string,
"port": integer,
"username": string,
"password": string,
"securityMode": string
},
"verificationStatus": string
}
此资源的sendAsEmail 属性代表使用此别名发送的邮件的“发件人:”标头中显示的电子邮件地址。这对于除创建之外的所有操作都是只读的。
有关管理别名的其他信息,您可以查看此documentation。