本文整理汇总了Python中sendgrid.SendGridClient方法的典型用法代码示例。如果您正苦于以下问题:Python sendgrid.SendGridClient方法的具体用法?Python sendgrid.SendGridClient怎么用?Python sendgrid.SendGridClient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sendgrid
的用法示例。
在下文中一共展示了sendgrid.SendGridClient方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_email
# 需要导入模块: import sendgrid [as 别名]
# 或者: from sendgrid import SendGridClient [as 别名]
def send_email(routes_to_report):
"""Send email using sendgrid."""
number_of_routes = len(routes_to_report)
if number_of_routes == 0:
return False
formatted_date = dt.datetime.utcnow().strftime("%A, %b %d")
rich_email_body = render_template(
"email.html",
routes=routes_to_report,
date=formatted_date
)
sg = SendGridClient(config.SG_USERNAME, config.SG_PASSWORD, raise_errors=True)
formatted_time = dt.datetime.utcnow().strftime("%F %T")
subject = '({}): {} routes'
subject = subject.format(formatted_time, number_of_routes)
try:
message = Mail(
to=config.TO_EMAIL,
subject=subject,
html=rich_email_body,
from_email=config.FROM_EMAIL
)
status, msg = sg.send(message)
return msg
except SendGridClientError as e:
print 'Failed to send email: ', e
raise
示例2: Memail
# 需要导入模块: import sendgrid [as 别名]
# 或者: from sendgrid import SendGridClient [as 别名]
def Memail(mto, mfrom, msubject, mbody, email_template_name, context):
if settings.MAIL_SENDER == 'AMAZON':
sending_mail(msubject, email_template_name, context, mfrom, mto)
elif settings.MAIL_SENDER == 'MAILGUN':
requests.post(
settings.MGUN_API_URL,
auth=('api', settings.MGUN_API_KEY),
data={
'from': mfrom,
'to': mto,
'subject': msubject,
'html': mbody
})
elif settings.MAIL_SENDER == 'SENDGRID':
sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD)
sending_msg = sendgrid.Mail()
sending_msg.set_subject(msubject)
sending_msg.set_html(mbody)
sending_msg.set_text(msubject)
sending_msg.set_from(mfrom)
sending_msg.add_to(mto)
reposne = sg.send(sending_msg)
print (reposne)
else:
text_content = msubject
msg = EmailMultiAlternatives(msubject, mbody, mfrom, mto)
msg.attach_alternative(mbody, "text/html")
msg.send()
示例3: _send_to_sendgrid
# 需要导入模块: import sendgrid [as 别名]
# 或者: from sendgrid import SendGridClient [as 别名]
def _send_to_sendgrid(self, message, email_addresses, cc=None, bcc=None,
sender=None):
username = (base.secret('sendgrid_low_priority_username') or
base.secret('sendgrid_username'))
password = (base.secret('sendgrid_low_priority_password') or
base.secret('sendgrid_password'))
assert username and password, "Can't find sendgrid username/password"
client = sendgrid.SendGridClient(username, password, raise_errors=True)
# The sendgrid API client auto-utf8-izes to/cc/bcc, but not
# subject/text. Shrug.
msg = sendgrid.Mail(
subject=self._get_summary().encode('utf-8'),
to=email_addresses,
cc=cc,
bcc=bcc)
if self.html:
# TODO(csilvers): convert the html to text for 'body'.
# (see base.py about using html2text or similar).
msg.set_text(message.encode('utf-8'))
msg.set_html(message.encode('utf-8'))
else:
msg.set_text(message.encode('utf-8'))
# Can't be keyword arg because those don't parse "Name <email>"
# format.
msg.set_from(_get_sender(sender))
client.send(msg)