Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/resend/resend-rails-smtp-example
https://github.com/resend/resend-rails-smtp-example
Last synced: 5 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/resend/resend-rails-smtp-example
- Owner: resend
- License: mit
- Created: 2023-08-02T14:37:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-03T10:06:57.000Z (17 days ago)
- Last Synced: 2024-11-03T11:17:34.035Z (17 days ago)
- Language: Ruby
- Size: 69.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
This is an example rails app on how to configure and use the [Resend SMTP Support](https://resend.com/docs/send-with-rails) with Rails Action Mailer
# Setup
Add these lines of code into your environment config file.
```ruby
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.resend.com',
:port => 465,
:user_name => 'resend',
:password => ENV['RESEND_API_KEY'],
:tls => true
}
```Create your mailer class
```ruby
# /app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: '[email protected]' # this domain must be verified with Resend
def welcome_email
@user = params[:user]
attachments["invoice.pdf"] = File.read(Rails.root.join("resources","invoice.pdf"))
@url = "http://example.com/login"
mail(
to: ["[email protected]"],
cc: ["[email protected]"],
bcc: ["[email protected]"],
reply_to: "[email protected]",
subject: "Hello from Rails",
)
end
end
```Create your `ERB Template` for `UserMailer`
```ruby
# /app/views/welcome_email.html.erb
Welcome to example.com, <%= @user.name %>
You have successfully signed up to example.com,
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
```
Now you can send your emails, lets send it using Rails console.
```sh
bundle exec rails c
```Initialize your `UserMailer` class, this should return a `UserMailer` instance.
```ruby
u = User.new name: "derich"
mailer = UserMailer.with(user: u).welcome_email
# => #, , , ...
```You can now send emails with:
```ruby
mailer.deliver_now!
# => {:id=>"a193c81e-9ac5-4708-a569-5caf14220539", :from=>....}
```