本文整理汇总了Python中sendgrid.Mail方法的典型用法代码示例。如果您正苦于以下问题:Python sendgrid.Mail方法的具体用法?Python sendgrid.Mail怎么用?Python sendgrid.Mail使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sendgrid
的用法示例。
在下文中一共展示了sendgrid.Mail方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_email
# 需要导入模块: import sendgrid [as 别名]
# 或者: from sendgrid import Mail [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 Mail [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 Mail [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)
示例4: compose
# 需要导入模块: import sendgrid [as 别名]
# 或者: from sendgrid import Mail [as 别名]
def compose(sender, recipient, subject, text="", html="None"):
"""
RECOMMENDED: use this method to compose the email.
Returns: A mail instance.
"""
message = sendgrid.Mail()
message.add_to(recipient)
message.set_subject(subject)
message.set_html(html)
message.set_text(text)
message.set_from(sender)
return message
示例5: publish_diff
# 需要导入模块: import sendgrid [as 别名]
# 或者: from sendgrid import Mail [as 别名]
def publish_diff(self, diff, feed_config):
if diff.emailed:
raise AlreadyEmailedError(diff.id)
elif not (diff.old.archive_url and diff.new.archive_url):
raise SendgridArchiveUrlNotFoundError()
api_token = feed_config.get("api_token", self.api_token)
sender = feed_config.get("sender", self.sender)
recipients = None
if feed_config.get("recipients"):
recipients = self.build_recipients(feed_config.get("recipients"))
else:
recipients = self.recipients
if not all([api_token, sender, recipients]):
raise SendgridConfigNotFoundError
subject = self.build_subject(diff)
message = Mail(
from_email=sender,
subject=subject,
to_emails=recipients.pop(0),
html_content=self.build_html_body(diff),
)
if recipients:
message.bcc = recipients
try:
self.mailer(api_token).send(message)
diff.emailed = datetime.utcnow()
logging.info("emailed %s", subject)
diff.save()
except Exception as e:
logging.error("unable to email: %s", e)