Mandrill setup Ruby on Rails - ruby-on-rails

I'm trying to setup the Mandrill API to send transactional emails. To start with I just want to get a welcome email sent to new users that have just successfully signed up. Once I've completed that I should be able to figure out any other additional emails. I've already coded a few bits, but to be honest I'm not entirely sure I'm doing it right! The code doesn't currently work during testing, which is really annoying.
All the code is included below, all input is welcome :)
I have both the following gems installed.
'mandrill-api'
'mandrill_mailer'
I'm unsure where to place the following code, the documentation for the gem says mail.rb in config/initializers https://github.com/renz45/mandrill_mailer (Look under 'Usage')
ActionMailer::Base.smtp_settings = {
:address => "smtp.mandrillapp.com",
:port => 587,
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_API_KEY'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
MandrillMailer.configure do |config|
config.api_key = ENV['MANDRILL_API_KEY']
end
I won't be using Action Mailer so I guess I'll just change
ActionMailer::Base.smtp_settings
to
MandrillMailer::TemplateMailer.smtp_settings
???
I have a transactional_mailer.rb file under app/mailers
class TransactionalMailer < MandrillMailer::TemplateMailer
default from: 'hello#randomemail.com'
def welcome(user)
mandrill_mail template: 'welcome_template',
subject: "hello",
to: {email: user.email, name: user.name},
vars: {
'USER_IDENTIFIER' => user.email,
'LIST_COMPANY' => 'Your Company'
},
important: true,
inline_css: true,
async: true
end
end
The app currently calls the method in the registration controller
MandrillMailer.welcome(resource).deliver
Should it be
TransactionalMailer.welcome(#user).deliver
???
It's not working during testing on the local server (rails s) I'm new to coding and trying to work out why.
All help is appreciated!
Thanks :)

Related

ActionMailer SMTP working in production but not locally

Seems to be the opposite problem to a lot of the questions on here. I've not been able to get the mailers working locally but then I deployed my app to Heroku and everything is fine.
No apparent errors when sending locally. Strangely though, I can have incorrect credentials locally and it will still appear to have sent ok. Almost as if it's not trying to deliver through smtp. I get a message like this in the console after sending:
UploadMailer#complete: processed outbound mail in 32.1ms Delivered mail 63ded40c98545_b9cbb90-440#Chriss-MacBook-Pro.local.mail (1238.6ms)
There's nothing in the SendGrid dashboard showing that it has reached their servers.
config/application.rb
...
config.action_mailer.delivery_method :smtp
ActionMailer::Base.smtp_settings = {
:user_name => 'apikey',
:password => ENV['SENDGRID_API_KEY'],
:address => 'smtp.sendgrid.net',
:domain => ENV['SENDGRID_DOMAIN'],
:port => 587,
:authentication => 'plain',
:enable_starttls_auto => true
}
Mailer and mailer view
class UploadMailer < ApplicationMailer
def complete
mail(to: 'me#myemail.com', subject: 'foo')
end
end
# app/views/upload_mailer/complete.html.erb
<h2>
Hello
</h2>
Environment
Ruby: 3.0.2 (rbenv)
Rails: 6.1.7.2
Tried on Mac and Windows(WSL)
Looks like I was just missing
config.action_mailer.perform_deliveries = true
in config/environments/development.rb

How do I send emails using Sendgrids API to send emails with rails 4? I can seem to put all the pieces together

I want to send emails with sendgrids API from a google cloud platform instance. They have a short tutorial on how to do this but it has the email message, who its from, who to send to, and other info all in app.rb which isnt the normal way messages are sent in rails apps.
I looked at the sendgrid ruby docs and they also dont have very good information. All the info in in one place and they dont state what files to put them in or even mention any smtp settings to use.
Here is what I have so far
development.rb
config.action_mailer.delivery_method = :smtp
# SMTP settings
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 465,
:domain => "mydomain.com",
:user_name => ENV['sendgrid_username'],
:password => ENV['sendgrid_password'],
:authentication => :plain,
:ssl => true,
:enable_starttls_auto => true
}
gemfile
gem "sendgrid-ruby"
The email views are in app/views, the mailer methods are in app/mailer the same as normal rails 4 apps have it setup.
So I guess here are my main questions:
Where do I call the environment variables holding the sendgrid api key?
Where do I tell Action Mailer to use sendgrid or SendGrid::Mail as I've seen in a couple of places?
Do I even need the sendgrid gem?
Are the smtp settings correct for sending over SSL with sendgrid?
I'm new to sending emails like this in a rails app and would really appreciate the help.
Put apikey as the name and the actual apikey as password:
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "yourwebsite.com",
authentication: :plain,
user_name: 'apikey',
password: Rails.application.secrets.sendgrid_api_key
}
Source
Use Figaro gem: https://github.com/laserlemon/figaro
It will generate an application.yml file for you, where you can store your sendgrid_username and sendgrid_password.
Have you generated your mailer? For example, generate mailer user_notifier where you can define your a default-from email, and some methods like this:
r
# send a signup email to the user, pass in the user object that contains the
user email address
default :from => 'hello#yourdomain.com'
def send_signup_email(user)
#user = user
mail( :to => #user.email,
:subject => 'Thanks for signing up!'
)
end
No, you don't need it.
https://sendgrid.com/docs/Classroom/Basics/Email_Infrastructure/smtp_ports.html

Sendgrid for an email feature within Ruby on Rails app

I would like to use Sendgrid to manage outgoing emails from a 3.2.2 version rails app I am developing with the help of a friend. She has email working from within the app using gmail, on her local/dev build. I need sendgrid up and running.
I cannot even get it to work locally.
From my development.rb file
config.action_mailer.default_url_options = { :host => 'localhost:3030' }
config.action_mailer.smtp_settings = {
:user_name => ENV['EMAIL_USERNAME'],
:password => ENV['EMAIL_PASSWORD'],
:domain => 'myapplicationdomain.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => 'plain',
:enable_starttls_auto => true
}
config.action_mailer.delivery_method = :smtp
Then I have a variable file in the root of my application that includes the following:
export EMAIL_USERNAME=sendgridusername
export EMAIL_PASSWORD=sendgridpassword
export MAIL_TO=report#myapplicationdomain.com
Here is the code from my mailer
class StatusMailer < ActionMailer::Base
default from: "reports#myapplicationdomain.com"
def status_report(report)
#greeting = "Hello"
#report = report
if ENV['MAIL_TO']
email = ENV['MAIL_TO'] if ENV['MAIL_TO']
else
email = #report.user.email
end
#statuses = #report.statuses
#reviewers = #report.user.reviewers
bcc = []
#reviewers.each do |reviewer|
bcc.append(reviewer.email)
end
#bcc = bcc
mail(to: email, bcc: bcc, subject: 'Status Report')
end
end
Am I missing some other setting? What about the MAIL_TO field in the variable, I am not certain what that should be set to, or if it even needs to be declared.
Is there another file that I should be editing? I had this working several days ago, but functionality somehow slipped away :0
Rails server says that emails were sent, but sendgrid has no record; nor are the emails being received by addresses on the distribution list.
Thank you in advance for any assistance.
Do you have the following settings in your config/environments/development.rb?
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
If not, add them to your config file and restart your server.
Update:
This error suggests that you're not authenticated. Are you sure the values of your ENV['EMAIL_USERNAME'] and ENV['EMAIL_PASSWORD'] variables are present/correct?
This post:
Sendgrid / email sending issues in Ruby on Rails (hosted on Heroku)
Got me up and running. The key being putting the SMTP and sendgrid information in the environment.rb file.
I can't explain exactly why that made the difference, but it did.

Actionmailer not delivering mail, with rails 3

I am trying to make an application, that sends an email when user registers.
i put in the smtp settings for gmail in the config/application.rb file and the mail function looks like
mail(:to => "me#me.com", :subject => "Mail!", :from => "another#me.com", :content_type => "text/html")
now when i see the logs, i see that it says mail has been sent, but i never receive any mail at all...
also, when i call the mail deliver function, Emails.signed(#user).deliver, the form page does not redirect, but it works if i comment out the email sending code that is either
Emails.signed(#user).deliver
or
mail(:to => "me#me.com", :subject => "Mail!", :from => "another#me.com", :content_type => "text/html")
Thanks :)
Edit: development.rb
App::Application.configure do
# Settings specified here will take precedence over those in config/environment.rb
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the webserver when you make code changes.
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
end
Somewhat late, but nevertheless maybe this will save someone a few hours of head banging. This is probably only relevant to sending emails from gmail.
First, in order to help debugging the situation, set the following line in development.rb to true (assuming you're in development mode):
config.action_mailer.raise_delivery_errors = true
This will make ActionMailer not to silently ignore errors.
When I did that, I realized gmail is refusing my username and password.
I then went to my configuration file where I put all the Action Mailer config directives (for me it was in development.rb, there is probably a better place to put it), and noticed that :user_name was set to "admin" rather than "admin#thedomain.com". Changing it solved the problem. Here is my corrected part of development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'thedomain.com',
:user_name => 'admin#thedomain.com',
:password => '<password>',
:authentication => 'plain',
:enable_starttls_auto => true }
References:
http://forums.pragprog.com/forums/43/topics/541
http://edgeguides.rubyonrails.org/action_mailer_basics.html
Another thing not to forget: you have to restart the application after making changes in your environment config files. when using passenger this can quickly be missed :)
that's what solved my "problem" when ActionMailer didnt want to send emails without showing any errors..
The things written here did not help me.
I am using Rails 3.2.8 and I spent several hours trying to figure this out and it was very simple in the end. I forgot to call .deliver() on the Mail::Message object that is returned by mail(:to => #user.email, :subject => 'Welcome to the Site') method call.
Just leave everything like it is specified in official RoR tutorial.
That is, in your development/production environment files, make a section like:
# mailer
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: '<username>#gmail.com',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true
}
And then you subclass ActionMailer::Base, for example like this:
class InfoMailer < ActionMailer::Base
default from: "<username>#gmail.com"
def welcome_user_and_send_password(user, password)
default_url_options = self.default_url_options()
#user = user
#password = password
#url = default_url_options[:host]
mail(:to => #user.email, :subject => 'Welcome to the Site').deliver()
end
end
After that, you can simply use this InfoMailer method from your code like a class method:
InfoMailer.welcome_user_and_send_password(user, password)
If you're using the test environment, be sure to comment out this line of code in environments/test.rb:
config.action_mailer.delivery_method = :test

How to send emails from ruby using a gmail account, for example?

I'm kind of new at ruby on rails, and I've read many tutorials about this, but I still can't figure out what's wrong.
I already set up the environment.rb with the following lines at the bottom:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.default_content_type = "text/html"
ActionMailer::Base.smtp_settings = {
:address => 'smtp.gmail.com',
:port => 25,
:domain => 'gmail.com',
:authentication => :login,
:user_name => 'myname#gmail.com',
:password => 'mypassword'
}
Then, I've got some code that came with the baseApp I'm using, and it does the following:
UserMailer.deliver_forgot_password(self)
Where self is the user object, that contains the email, login, etc.
That method(which belongs to the UserMailer class) has the following code:
def forgot_password(user)
setup_email(user)
#subject += "Forgotten password instructions"
#{user.password_reset_code}
#body[:url] = "http://#{Setting.get(:site_url)}/users/reset_password/#"
end
protected
def setup_email(user)
#recipients = "#{user.email}"
#from = "#{Setting.get(:support_name)} <#{Setting.get(:support_email)}>"
#subject = "[#{Setting.get(:site_name)}] "
#sent_on = Time.now
#body[:user] = user
# Get Settings
[:site_name, :company_name, :support_email, :support_name].each do |id|
#body[id] = Setting.get(id)
end
end
Well, the emails are fine, I've already checked them doing raise inspect, but it's not sending...and it doesn't throw me an exception either.
Now I'm getting Errno::ECONNREFUSED error when trying to send an email. What's wrong now?
Thanks,
Brian
Gmail port should be 587 or 465.
and add
:tls => true,
Do you have the ActionMailerTLS Gem installed ?
The new way of doing this as of rails 2.3.2 is
:enable_starttls_auto => true
No need for additional plugins.
On my Mac, I had to turn on postfix to get emails to send. Maybe you don't have a similar mail program turned on capable of sending emails. What that is for Windows 7, I do not know.

Resources