LogoLogo
v1.6.1
v1.6.1
  • 🌸What is Petal Pro?
  • πŸ’‘Changelog
  • ⏫Upgrade guide
  • Guides
    • πŸš€Creating a web app from start to finish
  • πŸ‘©β€πŸ³Recipes
    • πŸ’How to apply a recipe with git cherry pick
    • #️⃣UUIDs
    • ✍️First/Last name
    • πŸ“¦NPM packages
    • πŸ—ΊοΈGoogle Maps
  • Fundamentals
    • πŸ’ΏInstallation
    • πŸ“‚Folder structure
    • πŸ—ƒοΈIncluded Pages
    • πŸ˜€Users & Authentication
    • 🏒Organizations & Multitenancy
    • 🧊Components
    • ⬛Dark mode
    • 🎨Branding
    • 🌱Seeding
    • πŸ“„Layouts & Menus
    • πŸ–ΌοΈImage uploads
    • πŸ‘₯Impersonation
    • πŸ› οΈBackground Tasks and Jobs
    • πŸ”§Util & Helpers
    • πŸ“§Emails
    • πŸͺJavascript Hooks
    • πŸ“šExtra Hex Libraries
    • πŸ—οΈGenerators
    • πŸ—£οΈTranslations
    • πŸ–οΈContributing
    • πŸ›«Deployment
    • πŸ›‘οΈTesting
Powered by GitBook
On this page
  • Transactional emails
  • Marketing emails with MailBluster

Was this helpful?

  1. Fundamentals

Emails

PreviousUtil & HelpersNextJavascript Hooks

Last updated 1 year ago

Was this helpful?

Transactional emails

Open up email.ex and you will see a list of functions that generate Swoosh email structs. Eg:

def confirm_register_email(email, url) do
  base_email()
  |> to(email)
  |> subject("Confirm instructions")
  |> render_body("confirm_register_email.html", %{url: url})
  |> premail()
end

If I run this function in IEX I can see the Swoosh struct:

An Swoosh.Email struct can be delivered to an email address by a Swoosh mailer (see mailer.ex). Eg:

  Remindly.Email.confirm_register_email(user.email, url)
  |> Remindly.Mailer.deliver()

So email.ex creates the Swoosh structs and the functions for actually delivering emails like the previous code example are in user_notifier.ex. Think of email.ex functions like view templates, and user_notifer.ex functions like controller actions.

So the steps to creating a new email will be:

  1. Create the function to generate a Swoosh struct in email.ex

  2. Create a new user_notifier.ex function for use in our application

Marketing emails with MailBluster

Marketing emails are emails you wish to send to your whole subscriber base or some segment of them to push some kind of marketing or product updates email. It differs from a transactional email in that you haven't programmed it in your codebase - instead, you create it manually.

Since Petal Pro doesn't come with an email campaign builder, you'll have to rely on a third-party service. Some services, like Sendgrid, will do both your transactional emails and marketing emails. Others, like Postmark and SES, will only send transactional emails.

All you need to do is drop in an API key in the form of MAIL_BLUSTER_API_KEY in your .envrc file and you users will start syncing with MailBluster (see lib/petal_pro/vendor/mail_bluster.ex ). If you have already deployed to production and have some users, then you can sync them up by running PetalPro.MailBluster.sync_all_users() in IEX on your production server.

If you add some functionality that modifies a user - eg their name, you will need to re-sync that user with Mailbluster - PetalPro.MailBluster.sync_user(user).

Once you're all synced you can just log in to MailBluster and send a mass email.

Tags

MailBluster allows you to tag your users so you can segment your users. For example, we currently send the tags is_confirmed or is_suspended based on whether a user is either of those things. That way you can send emails only to users with confirmed email addresses.

To add/remove tags you can modify the code in mail_bluster.ex:

# To add another tag, firstly add it to this list:
defp all_potential_user_tags(),
    do: [
      "is_confirmed",
      "is_suspended"
    ]

# Secondly, add a function for whether or the tag should be included (eg return true if the tag should be added for a given user)
defp apply_tag?("is_confirmed", user), do: !!user.confirmed_at
defp apply_tag?("is_suspended", user), do: !!user.is_suspended

Ensure it looks good by navigating to "" and creating a new action in email_testing_controller.ex that simply renders the html_body value of the struct

To see a more detailed write up on creating an email - see in the "Complete web app" guide.

We chose as the default service to send marketing emails because of its very lost cost, and it uses SES under the hood.

πŸ“§
http://localhost:4000/dev/emails
MailBluster
this section