本文整理匯總了Python中sendgrid.helpers.mail.Content方法的典型用法代碼示例。如果您正苦於以下問題:Python mail.Content方法的具體用法?Python mail.Content怎麽用?Python mail.Content使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sendgrid.helpers.mail
的用法示例。
在下文中一共展示了mail.Content方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: send_email
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Content [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 Content [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 Content [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: _build_content
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Content [as 別名]
def _build_content(self, using_jinja=False) -> Content:
with open(self.template_path) as template_file:
email_content = template_file.read()
if self.message.template_variables is not None:
if using_jinja:
template = Template(email_content)
email_content = template.render(**self.message.template_variables)
else:
email_content = email_content.format(
**self.message.template_variables
)
return Content(self.message.content_type, email_content)
示例5: send_email_message
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Content [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
示例6: create_attachment
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Content [as 別名]
def create_attachment(cls,
file_location=None,
content_type=None,
filename=None,
disposition='attachment',
content_id=None):
"""Create a SendGrid attachment.
SendGrid attachments file content must be base64 encoded.
Args:
file_location (str): The path of the file.
content_type (str): The content type of the attachment.
filename (str): The filename of attachment.
disposition (str): Content disposition, defaults to "attachment".
content_id (str): The content id.
Returns:
Attachment: A SendGrid Attachment.
"""
file_content = ''
with open(file_location, 'rb') as f:
file_content = f.read()
content = base64.b64encode(file_content)
attachment = mail.Attachment()
attachment.content = content.decode('utf-8')
attachment.type = content_type
attachment.filename = filename
attachment.disposition = disposition
attachment.content_id = content_id
return attachment
示例7: send
# 需要導入模塊: from sendgrid.helpers import mail [as 別名]
# 或者: from sendgrid.helpers.mail import Content [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 Content [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