本文整理匯總了Python中sendgrid.helpers.mail.Email方法的典型用法代碼示例。如果您正苦於以下問題:Python mail.Email方法的具體用法?Python mail.Email怎麽用?Python mail.Email使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sendgrid.helpers.mail
的用法示例。
在下文中一共展示了mail.Email方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: send_email
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def send_email(self, to_email, from_email, subject, body):
"""Send the email
:param to_email: who the email is going to
(e.g. 'First Last <email@example.com>')
:param from_email: who the email is coming from
(e.g. 'First Last <email@example.com>')
:param subject: the email subject line
:param body: the email body in HTML format
:type to_email: string
:type from_email: string
:type subject: string
:type body: string
:returns: HTML status code and JSON message from SendGrid's API
:rtype: Integer, JSON
"""
from_email = Email(from_email)
subject = subject
to_email = Email(to_email)
soup = BeautifulSoup(body, "html.parser")
content = Content("text/plain", soup.get_text())
mail = Mail(from_email, subject, to_email, content)
response = self.sendgrid.client.mail.send.post(request_body=mail.get())
return response.status_code, response.body
示例2: send_single_email
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def send_single_email(email, subject, template, template_arguments):
""" Send an email using the SendGrid API
Args:
- email :string => the user's work email (ie username@company.com)
- subject :string => the subject line for the email
- template :string => the template file, corresponding to the email sent.
- template_arguments :dictionary => keyword arguments to specify to render_template
Returns:
- SendGrid response
"""
load_secrets()
env = Environment(loader=PackageLoader('yelp_beans', 'templates'))
template = env.get_template(template)
rendered_template = template.render(template_arguments)
message = mail.Mail(
from_email=mail.Email(SENDGRID_SENDER),
subject=subject,
to_email=mail.Email(email),
content=Content("text/html", rendered_template)
)
return send_grid_client.client.mail.send.post(request_body=message.get())
示例3: send_email
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def send_email(self, to_email, subject, from_email=None, html=None, text=None, *args, **kwargs): # noqa
if not any([from_email, self.default_from]):
raise ValueError("Missing from email and no default.")
if not any([html, text]):
raise ValueError("Missing html or text.")
self.from_email = Email(from_email or self.default_from)
self.subject = subject
personalization = Personalization()
if type(to_email) is list:
for email in self._extract_emails(to_email):
personalization.add_to(email)
elif type(to_email) is Email:
personalization.add_to(to_email)
elif type(to_email) is str:
personalization.add_to(Email(to_email))
self.add_personalization(personalization)
content = Content("text/html", html) if html else Content("text/plain", text)
self.add_content(content)
return self.client.mail.send.post(request_body=self.get())
示例4: _add_recipients
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def _add_recipients(email, email_recipients):
"""Add multiple recipients to the sendgrid email object.
Args:
email (SendGrid): SendGrid mail object
email_recipients (Str): comma-separated text of the email recipients
Returns:
SendGrid: SendGrid mail object with multiple recipients.
"""
personalization = mail.Personalization()
recipients = email_recipients.split(',')
for recipient in recipients:
personalization.add_to(mail.Email(recipient))
email.add_personalization(personalization)
return email
示例5: _extract_emails
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def _extract_emails(self, emails):
if type(emails[0]) is Email:
for email in emails:
yield email
elif type(emails[0]) is dict:
for email in emails:
yield Email(email['email'])
示例6: send_email_message
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def send_email_message(self, recipient, subject, html_message, text_message, sender_email, sender_name):
""" Send email message via sendgrid-python.
Args:
recipient: Email address or tuple of (Name, Email-address).
subject: Subject line.
html_message: The message body in HTML.
text_message: The message body in plain text.
"""
if not current_app.testing: # pragma: no cover
try:
# Prepare Sendgrid helper objects
from sendgrid.helpers.mail import Email, Content, Substitution, Mail
from_email = Email(sender_email, sender_name)
to_email = Email(recipient)
text_content = Content('text/plain', text_message)
html_content = Content('text/html', html_message)
# Prepare Sendgrid Mail object
# Note: RFC 1341: text must be first, followed by html
mail = Mail(from_email, subject, to_email, text_content)
mail.add_content(html_content)
# Send mail via the Sendgrid API
response = self.sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
except ImportError:
raise ConfigError(SENDGRID_IMPORT_ERROR_MESSAGE)
except Exception as e:
print(e)
print(e.body)
raise
示例7: send
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def send(self, email_sender=None, email_recipient=None,
email_subject=None, email_content=None, content_type=None,
attachment=None):
"""Send an email.
This uses the SendGrid API.
https://github.com/sendgrid/sendgrid-python
The minimum required info to send email are:
sender, recipient, subject, and content (the body)
Args:
email_sender (str): The email sender.
email_recipient (str): The email recipient.
email_subject (str): The email subject.
email_content (str): The email content (aka, body).
content_type (str): The email content type.
attachment (Attachment): A SendGrid Attachment.
Raises:
EmailSendError: An error with sending email has occurred.
"""
if not email_sender or not email_recipient:
LOGGER.warning('Unable to send email: sender=%s, recipient=%s',
email_sender, email_recipient)
raise util_errors.EmailSendError
email = mail.Mail()
email.from_email = mail.Email(email_sender)
email.subject = email_subject
email.add_content(mail.Content(content_type, email_content))
email = self._add_recipients(email, email_recipient)
if attachment:
email.add_attachment(attachment)
try:
response = self._execute_send(email)
except urllib.error.HTTPError as e:
LOGGER.exception('Unable to send email: %s %s',
e.code, e.reason)
raise util_errors.EmailSendError
if response.status_code == 202:
LOGGER.info('Email accepted for delivery:\n%s',
email_subject)
else:
LOGGER.error('Unable to send email:\n%s\n%s\n%s\n%s',
email_subject, response.status_code,
response.body, response.headers)
raise util_errors.EmailSendError
示例8: send_email
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Email [as 別名]
def send_email(to, subject, body, cc=(), from_name='Ok',
link=None, link_text="Sign in",
template='email/notification.html', reply_to=None, **kwargs):
""" Send an email using sendgrid.
Usage: send_email('student@okpy.org', 'Hey from OK', 'hi',
cc=['test@example.com'], reply_to='ta@cs61a.org')
"""
try:
sg = _get_sendgrid_api_client()
except ValueError as ex:
logger.error('Unable to get sendgrid client: %s', ex)
return False
if not link:
link = url_for('student.index', _external=True)
html = render_template(template, subject=subject, body=body,
link=link, link_text=link_text, **kwargs)
mail = sg_helpers.Mail()
mail.set_from(sg_helpers.Email('no-reply@okpy.org', from_name))
mail.set_subject(subject)
mail.add_content(sg_helpers.Content("text/html", emailFormat(html)))
if reply_to:
mail.set_reply_to(sg_helpers.Email(reply_to))
personalization = sg_helpers.Personalization()
personalization.add_to(sg_helpers.Email(to))
for recipient in cc:
personalization.add_cc(sg_helpers.Email(recipient))
mail.add_personalization(personalization)
try:
response = sg.client.mail.send.post(request_body=mail.get())
except HTTPError:
logger.error("Could not send the email", exc_info=True)
return False
if response.status_code != 202:
logger.error("Could not send email: {} - {}"
.format(response.status_code, response.body))
return False
return True