Python sendgrid.helpers.mail.Email() Examples
The following are 8
code examples of sendgrid.helpers.mail.Email().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sendgrid.helpers.mail
, or try the search function
.
Example #1
Source File: flask_sendgrid.py From flask-sendgrid with MIT License | 7 votes |
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())
Example #2
Source File: sendgrid_email.py From open-source-library-data-collector with MIT License | 6 votes |
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
Example #3
Source File: send_email.py From beans with MIT License | 6 votes |
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())
Example #4
Source File: sendgrid_connector.py From forseti-security with Apache License 2.0 | 6 votes |
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
Example #5
Source File: flask_sendgrid.py From flask-sendgrid with MIT License | 5 votes |
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'])
Example #6
Source File: sendgrid_email_adapter.py From Flask-User with MIT License | 5 votes |
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
Example #7
Source File: sendgrid_connector.py From forseti-security with Apache License 2.0 | 4 votes |
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
Example #8
Source File: utils.py From ok with Apache License 2.0 | 4 votes |
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