""" A workflow node adapter that sends an email and asynchronously. Author: Reinaldo """ from __future__ import with_statement import logging from sendgrid.helpers.mail import * from sendgrid import * import email_adapter_constants from vi_workflow.base import ViWorkflowAsyncNode class ViSimpleEmailAdapterAsync(ViWorkflowAsyncNode): """Creates an Async Node to send email and wait for a callback.""" def run(self, receiver_email_address=None, subject=None, message=None, **kwargs): """Invoked by the framework. Args: receiver_email_address: A string containing the TO email address. subject: The subject line of the email that will be sent. message: A String containing the message to be sent. Yields: An Asynchronous workflow node. """ logging.info('receiver_email_address: %s', receiver_email_address) random_token = kwargs.pop( email_adapter_constants.CALLBACK_CGI_NAME_RANDOM_TOKEN) hostname = email_adapter_constants.HOSTNAME approve_params = { email_adapter_constants.CALLBACK_CGI_NAME_RANDOM_TOKEN: random_token, email_adapter_constants.CALLBACK_CGI_NAME_CHOICE: email_adapter_constants.CALLBACK_CGI_VALUE_APPROVE } cancel_params = { email_adapter_constants.CALLBACK_CGI_NAME_RANDOM_TOKEN: random_token, email_adapter_constants.CALLBACK_CGI_NAME_CHOICE: email_adapter_constants.CALLBACK_CGI_VALUE_REJECT } approve_url = email_adapter_constants.CALLBACK_URL_TMPL % ( hostname, self.get_callback_url(**approve_params)) cancel_url = email_adapter_constants.CALLBACK_URL_TMPL % ( hostname, self.get_callback_url(**cancel_params)) body = message from_email = Email("list-build-tool@informationretrieval.systems", "ListBuildingToolPoweredByAppengine") to_email = Email( receiver_email_address, receiver_email_address) content = Content("text/plain", message) mail = Mail(from_email, subject, to_email, content) mail_settings = MailSettings() # Mark the message as sandbox so it is not actually sent out by Sendgrid. mail_settings.sandbox_mode = SandBoxMode(True) mail.mail_settings = mail_settings sg = SendGridAPIClient(apikey=email_adapter_constants.SENDGRID_API_KEY) data = mail.get() response = sg.client.mail.send.post(request_body=data) logging.info('SendMail result: %s, %s, %s', response.status_code, response.headers, response.body) self.complete(True)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question