def send_api_mail(subject, to, body): sg = sendgrid.SendGridAPIClient(apikey=os.getenv('SENDGRID_API_KEY')) from_email = SGEmail('Grey Li <*****@*****.**>') to_email = SGEmail(to) content = Content("text/plain", body) email = SGMail(from_email, subject, to_email, content) sg.client.mail.send.post(request_body=email.get())
def send_api_mail(subject, to, body): sg = sendgrid.SendGridAPIClient(apikey=os.getenv('SENDGRID_API_KEY')) from_email = SGEmail('Frank Zheng <*****@*****.**>') to_email = SGEmail(to) content = Content("text/plain", body) email = SGMail(from_email, subject, to_email, content) sg.client.mail.send.post(request_body=email.get())
def send_api_mail(subject, to, body): sg = sendgrid.SendGridAPIClient(apikey=os.getenv('SENDGRID_API_KEY')) from_email = SGEmail('*****@*****.**') to_email = SGEmail(to) content = Content("text/plain", body) email = SGMail(from_email, subject, to_email, content) # 发送邮件post表单 sg.client.mail.send.post(request_body=email.get())
def send_by_api(body, subject, to): sg = sendgrid.SendGridAPIClient(os.getenv('SENDGRID_API_KEY')) from_email = 'ThomasYoung <*****@*****.**>' text_content = body html_content = '<h1>订阅</h1>' print(body, subject, to) email = SGMail(from_email=from_email, subject=subject, to_emails=to, plain_text_content=text_content, html_content=html_content) response = sg.send(email) print('code', response.status_code) print('body', response.body) print('headers', response.headers)
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())