carbon_mailersend_adapter

Version, currently master branch1 version
  • master branchlatestOct 26, 2022

github.com/balakhorvathnorbert/carbon_mailersend_adapter

Mailersend adapter for Lucky Framework's Carbon

2 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  carbon_mailersend_adapter:
    github: balakhorvathnorbert/carbon_mailersend_adapter
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
>= 0.36.1, < 2.0.0
License
MIT
Author
Norbert Balák-Horváth

Dependencies

Runtime Dependencies

  • carbon~> 0.3.0github: luckyframework/carbon
  • habitat~> 0.4.7github: luckyframework/habitat

Development Dependencies

  • ameba~> 0.14.2github: veelenga/amebadev
  • lucky_env~> 0.1.1github: luckyframework/lucky_envdev

README

MIT licensed

Mailersend adapter for Carbon

This is luckyframework/carbon's adapter for mailersend: https://www.mailersend.com

https://github.com/luckyframework/carbon

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      carbon_mailersend_adapter:
        github: balakhorvathnorbert/carbon_mailersend_adapter
  2. Run shards install

Usage

Set your MAILERSEND_API_KEY variable inside .env

and update your config/email.cr file with:

require "carbon_mailersend_adapter"

BaseEmail.configure do |settings|
  if LuckyEnv.production?
    mailersend_key = mailersend_key_from_env
    settings.adapter = Carbon::MailersendAdapter.new(api_key: mailersend_key)
  else
    settings.adapter = Carbon::DevAdapter.new(print_emails: true)
  end
end

private def mailersend_key_from_env
  ENV["MAILERSEND_API_KEY"]? || raise_missing_key_message
end

private def raise_missing_key_message
  puts "Missing MAILERSEND_API_KEY. Set the MAILERSEND_API_KEY env variable to '' if not sending emails, or set the MAILERSEND_API_KEY ENV var.".colorize.red
  exit(1)
end

Sending emails using templates

In your email class /src/emails/YOUR_EMAIL_CLASS.cr define the template_id:

class PasswordResetRequestEmail < BaseEmail
  Habitat.create { setting stubbed_token : String? }
  delegate stubbed_token, to: :settings

  def initialize(@user : User)
    @token = stubbed_token || Authentic.generate_password_reset_token(@user)
  end

  to @user
  from "example@example.com" # or set a default in src/emails/base_email.cr
  subject "Reset your password"
  templates html, text
  def template_id
    "xxxxxxxxxxxx"
  end
  def variables
    {
      "var": "name"
      "value": "example@example.com"
    }
  end
end

Contributing

  1. Fork it (https://github.com/your-github-user/mailersend_adapter/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors