如何通过gmail API发送邮件时设置发件人姓名和姓氏?
我使用此代码:https://github.com/chris-brown-nz/python-gmail-api
代码很棒,我成功发送邮件。但是,发件人的名称只是@
符号留下的文字。示例:如果John Travis
发送来自johnhomeboy@gmail.com
的电子邮件,则接收方显示的名称将为johnhomeboy
,而不是John Travis
。为了发送带有我姓名和姓名的电子邮件,我需要设置什么属性?
答案 0 :(得分:0)
如AChampion的评论中所述,gmail API接受标准的RFC822格式,因此可以使用通过电子邮件user@example.com
发送的Gmail Python API
create_message('user@example.com', 'DESTINATION_EMAIL', 'TITLE', 'BODY')
发件人姓名可以使用
自定义
create_message("CUSTOMTISED_FROM_TITLE '<user@example.com>'", 'DESTINATION_EMAIL', 'TITLE', 'BODY')
create_message
被定义为
def create_message(sender, to, subject, message_text):
"""Create a message for an email.
Args:
sender: Email address of the sender.
to: Email address of the receiver.
subject: The subject of the email message.
message_text: The text of the email message.
Returns:
An object containing a base64url encoded email object.
"""
message = MIMEText(message_text)
message['to'] = to
message['from'] = sender
message['subject'] = subject
return {'raw': base64.urlsafe_b64encode(message.as_string())}
下面的示例在https://developers.google.com/gmail/api/guides/sending?authuser=2