5. Sending the Email
Now we’re ready for the final step. We’re going to take each of those story responses and collate them into an email body and then send it off to our recipient.
Service Request: send_email
send_emailNavigate to the Build page, go to the Service Requests for our integration, and add a new row. Enter the following values:
Sequence:
200System:
EMAIL_SMTPService Name:
n/aFormula Variable:
send_emailBefore Prepare Request Hook: copy and paste the code below. This is the most complicated Python code we have shown to the reader thus far. Basically, we are creating a standard message for each story we received, and writing it to a file. We will later read this file for the body of the email.
from datetime import datetime stories = [sr.response.payload for sr in get_story_by_id] stories.sort(key=lambda story: story.score, reverse=True) query_ct = 500 if not input.payload.limitQuery else input.payload.limitQuery email_limit = 50 if not input.payload.limitEmail else input.payload.limitEmail with open_glyuefile("email_body.txt", "w") as file: file.write(f"Here are the top {email_limit} of the latest {query_ct} articles from Hacker news:\n\n") for story in stories[:email_limit]: time = datetime.fromtimestamp(story.time).strftime("%m/%d/%Y, %H:%M") file.writelines([ f"{story.title}\n", f"{story.url}\n", f"Posted by [{story.by}] on {time} ({story.score} points, {story.descendants} comments)\n", f"Discussion: https://news.ycombinator.com/item?id={story.id}\n\n" ])
Field Mappings for SR: send_email
send_emailRight-click the newly-created SR and go to Field Mappings. Add 4 rows with these values and then save.
First row
Field:
from_addressValue:
"[email protected]"(use the same email as was entered for the Email SMTP Adapter Config, and don’t forget the quotes)Value Type:
str
Second row
Field:
to_addressesValue:
[input.payload.email]Value Type:
list
Third row
Field:
subjectValue:
"Top HackerNews Stories"Value Type:
str
Fourth row
Field:
bodyValue:
open_glyuefile("email_body.txt").read()Value Type:
str
Don’t forget to save!
Last updated
Was this helpful?