任何人都请帮助,我起诉sendgrid V3 API。但我无法找到任何方式来发送邮件给多个收件人。预先感谢。
import sendgrid
from sendgrid.helpers.mail import *
sg = sendgrid.SendGridAPIClient(apikey="SG.xxxxxxxx")
from_email = Email("FROM EMAIL ADDRESS")
to_email = Email("TO EMAIL ADDRESS")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
我想发送电子邮件给多个收件人。像to_mail = “[email protected],[email protected]”。
发送电子邮件给多recicpent在sendgrid V3。
import time
import sendgrid
import os
print "Send email to multiple user"
sg = sendgrid.SendGridAPIClient(apikey='your sendrid key here')
data = {
"personalizations": [
{
"to": [{
"email": "[email protected]"
}, {
"email": "[email protected]"
}],
"subject": "Multiple recipent Testing"
}
],
"from": {
"email": "[email protected]"
},
"content": [
{
"type": "text/html",
"value": "<h3 style=\"color: #ff0000;\">These checks are silenced please check dashboard. <a href=\"http://sensu.mysensu.com/#/silenced\" style=\"color: #0000ff;\">Click HERE</a></h3> <br><br> <h3>For any query please contact DevOps Team<h3>"
}
]
}
response = sg.client.mail.send.post(request_body=data)
print(response.status_code)
if response.status_code == 202:
print "email sent"
else:
print("Checking body",response.body)
基于Subhrajyoti达斯的答案,我写了下面的代码,也就是将电子邮件发送到多个收件人的mail_example.py
实例的简化版本。
def SendEmail():
sg = sendgrid.SendGridAPIClient(api_key="YOUR KEY")
from_email = Email ("FROM EMAIL ADDRESS")
to_list = Personalization()
to_list.add_to (Email ("EMAIL ADDRESS 1"))
to_list.add_to (Email ("EMAIL ADDRESS 2"))
to_list.add_to (Email ("EMAIL ADDRESS 3"))
subject = "EMAIL SUBJECT"
content = Content ("text/plain", "EMAIL BODY")
mail = Mail (from_email, subject, None, content)
mail.add_personalization (to_list)
response = sg.client.mail.send.post (request_body=mail.get())
return response.status_code == 202
您可以在下面的方式更新代码。你可以找到目前在Sendgrid的包mail_example.py
相同。
personalization = get_mock_personalization_dict()
mail.add_personalization(build_personalization(personalization))
def get_mock_personalization_dict():
"""Get a dict of personalization mock."""
mock_pers = dict()
mock_pers['to_list'] = [Email("[email protected]",
"Example User"),
Email("[email protected]",
"Example User")]
return mock_pers
def build_personalization(personalization):
"""Build personalization mock instance from a mock dict"""
mock_personalization = Personalization()
for to_addr in personalization['to_list']:
mock_personalization.add_to(to_addr)
return mock_personalization
请注意,与其他的答案的代码在这里,电子邮件的收件人将看到每个人在电子邮件地址字段。为了避免这其中有使用单独的Personalization
对象每一个电子邮件地址:
def SendEmail():
sg = sendgrid.SendGridAPIClient(api_key="YOUR KEY")
from_email = Email ("FROM EMAIL ADDRESS")
person1 = Personalization()
person1.add_to(Email ("EMAIL ADDRESS 1"))
person2 = Personalization()
person2.add_to(Email ("EMAIL ADDRESS 2"))
subject = "EMAIL SUBJECT"
content = Content ("text/plain", "EMAIL BODY")
mail = Mail (from_email, subject, None, content)
mail.add_personalization(person1)
mail.add_personalization(person2)
response = sg.client.mail.send.post (request_body=mail.get())
return response.status_code == 202