使用sendgrid&添加标题到邮件蟒蛇

时间:2017-06-28 14:32:41

标签: python email header sendgrid

我正在尝试使用sendGrid为邮件添加标题:

这是从官方网站发送电子邮件的代码,

string apikey = "......."
sg = sendgrid.SendGridAPIClient( apikey =apikey)
to_email = Email("....@gmail.com")   
from_email =Email ("....")
subject= '...'
content=Content('..')
mail =Mail(...)
response = sg.client.mail.send.post(request_body=mail.get())

&安培;它工作正常,现在添加我做的标题(使用此:https://github.com/sendgrid/smtpapi-python

header = SMTPAPIHeader()
header.set_unique_args({'orderNumber':'123456'})  
mail.add_header(header)

然后

response = sg.client.mail.send.post(request_body=mail.get())

但我明白了:

in get(self)
     70             headers = {}
     71             for key in self.headers:
---> 72                 headers.update(key.get())
     73             mail["headers"] = headers
     74 

AttributeError: 'SMTPAPIHeader' object has no attribute 'get'

我怎么解决这个问题?

2 个答案:

答案 0 :(得分:0)

事实上它确实完成了个性化:

mail.personalizations[0].add_header(Header("..","..."))


response = sg.client.mail.send.post(request_body=mail.get())

答案 1 :(得分:0)

试试这个,您可以使用set_headers方法为sendgrid电子邮件设置标头。

import sendgrid

sg = sendgrid.SendGridClient("XXXX-UR-API-KEY-XXXXX")

message = sendgrid.Mail()
message.add_to('to@email.com')
message.set_from("from@email.com>")
message.set_subject("email subject")
message.set_html('html body')

message.set_headers({'X-Priority' : '2'}) #<=== To add priority

sg.send(message)