示例#1
0
 def send(self, message, envelope_from=None):
     assert message.send_to, "No recipients have been added"
     assert message.sender, (
         "The message does not specify a sender and a default sender "
         "has not been configured")
     if message.has_bad_headers():
         raise BadHeaderError
     if message.date is None:
         message.date = time.time()
     data = {'to': ', '.join(list(sanitize_addresses(message.send_to)))}
     if hasattr(message, 'mailgun_variables') and isinstance(
             message.mailgun_variables, dict):
         for key, val in message.mailgun_variables.items():
             data['v:' + str(key)] = val
     response = requests.post(
         self.mail.api_url,
         auth=HTTPBasicAuth('api', self.mail.api_key),
         data=data,
         files={'message': ('mime_message', message.as_string())})
     if response.status_code >= 400:
         sys.stderr.write("SendGrid status code: " +
                          str(response.status_code) + "\n")
         sys.stderr.write("SendGrid response headers: " +
                          repr(response.headers) + "\n")
         try:
             sys.stderr.write(repr(response.body) + "\n")
         except:
             pass
         raise Exception("Failed to send e-mail message to " +
                         self.mail.api_url)
     email_dispatched.send(message, app=current_app._get_current_object())
示例#2
0
 def send(self, message, envelope_from=None):
     assert message.send_to, "No recipients have been added"
     assert message.sender, (
         "The message does not specify a sender and a default sender "
         "has not been configured")
     if message.has_bad_headers():
         raise BadHeaderError
     if message.date is None:
         message.date = time.time()
     if not message.subject:
         message.subject = word("(no subject)")
     sgmessage = SGMail(
         from_email=Email(message.sender),
         to_emails=[
             To(addressee)
             for addressee in sanitize_addresses(message.recipients)
         ],
         subject=message.subject,
         plain_text_content=message.body,
         html_content=message.html)
     if message.reply_to:
         sgmessage.reply_to = ReplyTo(message.reply_to)
     if message.cc:
         for recipient in list(sanitize_addresses(message.cc)):
             sgmessage.add_cc(recipient)
     if message.bcc:
         for recipient in list(sanitize_addresses(message.bcc)):
             sgmessage.add_bcc(recipient)
     if message.attachments:
         for flask_attachment in message.attachments:
             attachment = Attachment()
             attachment.file_content = FileContent(
                 base64.b64encode(flask_attachment.data).decode())
             attachment.file_type = FileType(flask_attachment.content_type)
             attachment.file_name = FileName(flask_attachment.filename)
             attachment.disposition = Disposition(
                 flask_attachment.disposition)
             sgmessage.add_attachment(attachment)
     sg = SendGridAPIClient(self.mail.api_key)
     response = sg.send(sgmessage)
     if response.status_code >= 400:
         sys.stderr.write("SendGrid status code: " +
                          str(response.status_code) + "\n")
         sys.stderr.write("SendGrid response headers: " +
                          repr(response.headers) + "\n")
         try:
             sys.stderr.write(repr(response.body) + "\n")
         except:
             pass
         raise Exception("Failed to send e-mail message to SendGrid")
     email_dispatched.send(message, app=current_app._get_current_object())
示例#3
0
 def send(self, message, envelope_from=None):
     assert message.send_to, "No recipients have been added"
     assert message.sender, (
             "The message does not specify a sender and a default sender "
             "has not been configured")
     if message.has_bad_headers():
         raise BadHeaderError
     if message.date is None:
         message.date = time.time()
     response = requests.post(self.mail.api_url,
                              auth=HTTPBasicAuth('api', self.mail.api_key),
                              data={'to': ', '.join(list(sanitize_addresses(message.send_to)))},
                              files={'message': ('mime_message', message.as_string())})
     if response.status_code >= 400:
         raise Exception("Failed to send e-mail message to " + self.mail.api_url)
     email_dispatched.send(message, app=current_app._get_current_object())