Automating Insulin Ordering: Sending the Email to the Pharmacy (Part 4)
Tags: programming, productivity, automation, processoptimisation
Published: 2024-06-05 07:34:51


In this part of the series, we will focus on automating the step of sending an email to the pharmacy to pre-order insulin. We will use Gmail's SMTP server to send the email directly to the pharmacy's inbox. For this, we need the SMTP server details and our account credentials.

I made the choice to use Gmail, because my email in known in the inbox of my pharmacy and therefore my automated email will be unlikely to land in their spam. It also allows my pharmacy to answer my email without me having to worry about anything.

## Setting Up Gmail SMTP
If you have 2FA enabled, you'll need to create an app password:

1. Go to Gmail and click on your avatar.
2. Select "Manage your Google Account".
3. Navigate to the "Security" section.
4. Search for "App passwords" in the search bar.
5. Create a new app password and use it for sending the email.

If you don't have 2FA enabled, you would need to allow less secure apps to use your email address, but I highly recommend to use 2FA for security reasons.

## Sending the Email Using cURL
We will use cURL to send the email via Gmail's SMTP server. Here’s the basic structure of the cURL command:

```bash
curl --url "smtp://$SMTP_SERVER:$SMTP_PORT" \
    --ssl-reqd \
    --mail-from "$SMTP_USERNAME" \
    --mail-rcpt "$TO" \
    --user "$SMTP_USERNAME:$SMTP_PASSWORD" \
    --tlsv1.2 \
    -T <(echo -e "$MESSAGE")
```

## Gathering the Required Data
To send the email, we need to fill in the data in the request above. This means we need the following data:

- SMTP server: `smtp.gmail.com`
- SMTP port: `587`
- SMTP username: Your Gmail account email
- SMTP password: The app password created in your Google account management dashboard
- Recipient email address: The email address of the pharmacy
- Subject and body of the email: The actual content of the email

The message in the email will be added in the last line of the curl request. The email message should consist of a list of headers and then the body text. Each header is the header name, followed by a colon and then the value. The most basic setup for this would be something like: `Subject: <The Subject>\n\n<Email Body>`. Here, the `Subject:` is the header. For me, that would look something like this:

```bash
SUBJECT="Preorder insulin"
BODY=$'Hello\n\nI would like a new insulin prescription.\n\nThanks,\nRobin'

MESSAGE="Subject: $SUBJECT\n\n$BODY"
```

The last thing I want to do in this script is send a copy of the email to myself as a BCC address. This way, I will get a push notification from gmail every time this automation triggers. To do that, we need to ensure that the true recipient is added as a header to the message on it's own line (`To: $TO`). If we then add another email recipient in the curl request, it will be pushed to the BCC list. So my message would look like this:

```bash
MESSAGE="Subject: $SUBJECT\nTo: $TO\n\n$BODY"
```

If we then add `--mail-rcpt "$SMTP_USERNAME"\` to the curl request, a BCC will be sent to the `$SMTP_USERNAME` email address. 

## Complete Script
Below is the complete script to send the email to the pharmacy. It includes error handling to ensure the email is sent successfully.

```bash
function send_pharmacy_email() {
  # Set the default value to true to prevent sending too many emails to my pharmacy while testing
  test=${1:-true}

  SMTP_SERVER="smtp.gmail.com"
  SMTP_PORT="587"
  SMTP_USERNAME="<gmail account email to use>"
  SMTP_PASSWORD="<app password>"

  # Recipient email address
  if [[ $test == 'true' ]]; then
      TO="testemail@test.com"
  else
      TO="mypharmacy@pharmacy.com"
  fi

  SUBJECT="Preorder insulin"
  BODY=$'Hello\n\nI would like a new insulin prescription.\n\nThanks,\nRobin'

  MESSAGE="Subject: $SUBJECT\nTo: $TO\n\n$BODY"

  curl --url "smtp://$SMTP_SERVER:$SMTP_PORT" \
      --ssl-reqd \
      --mail-from "$SMTP_USERNAME" \
      --mail-rcpt "$TO" \
      --mail-rcpt "$SMTP_USERNAME" \
      --user "$SMTP_USERNAME:$SMTP_PASSWORD" \
      --tlsv1.2 \
      -T <(echo -e "$MESSAGE")

  if [ $? -eq 0 ]; then
      echo "Done, e-mail sent to $TO"
      return 0
  else
      echo "Failed to send email"
      return 1
  fi
}
```

Testing this in my terminal result into the following output. 

![Terminal output after running the function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yun0a7cri5aivadtrmwa.png)

Moreover, the proof of the pudding as it were, in the inbox of my test email address I see the email come in correctly.

I added a test variable that defaults to true to prevent sending an overload of messages to my pharmacy while working on the script. That means that once the script should actually run automatically, we need to pass an argument to the `send_pharmacy_email` function in the wrapper to ensure it doesn't run in test mode anymore.

## What’s Next?
In the next part of this series, we will focus on implementing the add_calendar_task function, which automates adding a task to Google Calendar to remind you to pick up the insulin.

Stay tuned as we continue to streamline the insulin ordering process, one step at a time.

Thank you for following along! If you have any questions or suggestions, feel free to leave a comment below. Don't forget to share this post with anyone who might find it useful.