Customize the From address in Rails application - ruby-on-rails

I have mailer action in my application, the mailer is configured with gmail smtp. The following is my config details under environment.rb file
require "smtp_tls"
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.default_content_type = "text/html"
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'gmail.com',
:user_name => "info#example.com",
:password => "password",
:authentication => :plain
The think i want to implement is, when ever the application generating email the from address shows "info#example.com".
Is possible to customize the from address. In different places i want to use different From address instead of "info#example.com"
I tried with my mailer model:
#from = "#{user.email}"
In development server log it shows the customized id correctly. if go my email inbox it shows the from address as "info#example.com"
Can any one please guide on this. thanks in advance.

It's not #from. It's the method from.
def my_email_sender_method
from "nobody <noreply#example.com>"
end

Related

Why can't I see any emails in my inbox sent from my Heroku Rails app using SendGrid SMTP Relay in development mode?

I'm trying to switch over to SendGrid from Mandrill in my Rails 4.2 app through SendGrid's SMTP Relay. I have set the 'To Email' to be my personal email address so that I can view the emails that have been sent, however none of the emails actually appear in my inbox despite the rails console claiming to have processed and sent the email.
I am fairly certain all my mailers have the appropriate smtp settings as I have mostly followed the instructions provided on the SendGrid website: https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html
I have also tested my connectivity to SendGrid's SMTP Relay through telnet and the connection is succesful.
My SendGrid dashboard indicated that 0 emails have been sent. None of my emails appear under the Suppressions tab either so it's not like they have bounced or have been blocked.
This is in my config/environment.rb:
ActionMailer::Base.smtp_settings = {
:user_name => 'apikey',
:password => ENV['SENDGRID_API_KEY'],
:domain => 'heroku.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:authentication => :plain,
:enable_starttls_auto => true
}
This is in my config/environments/development.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { :host => 'smtp.sendgrid.net' }
This is the line in my controller that calls my ApplicationMailer:
ApplicationMailer.send_email(user, 'mypersonalemail#email.com', 'Test Subject').deliver
And this is what gets printed in the console when the mailer method is executed:
ApplicationMailer#send_email: processed outbound mail in 789.9ms
Sent mail to mypersonalemail#email.com (103.4ms)
But I still don't get any emails in my inbox or spam folder. Does anyone know how I can solve this? Thanks in advance.
Your domain and host options are wrong. Use localhost:3000 (unless you're using docker or something at which point replace localhost:3000 with 0.0.0.0:8000)
#/environments/development.rb
#Mailer Options
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'localhost:3000',
:enable_starttls_auto => true
}
config.action_mailer.default_url_options = { host: 'http://localhost:3000' }
config.action_mailer.asset_host = 'http://localhost:3000'
Make sure to add the sendgrid credentials to your local machine as environment vars. To get them, go to your heroku app and click on settings, then "reveal config vars". Then add those sendgrid credentials to your local machine as env. vars and you're done.

Sendgrid set up on Rails 4

I have a rails 4 app. I set up ActionMailer and I can send order confirmation emails via localhost and gmail.
I installed Sendgrid on Heroku and followed the set up instructions. I get a Net::SMTPSyntaxError (501 Syntax error
my environment.rb (i have sendgrid user/pwd in application.yml)
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
in production.rb - the only actionamailer setting i have is this. I have this as a placeholder to put the real domain in later. I'm currently using herokuapp.com.
config.action_mailer.default_url_options = { host: 'localhost:3000' }
in my orders_controller within the order create method, I call the below.
AutoNotifier.orderconf_email(current_user, #order).deliver
auto_notifier.rb
class AutoNotifier < ActionMailer::Base
default from: "Test Email"
def orderconf_email(current_user, order)
#buyer = current_user
#order = order
mail(to: #buyer.email, subject: 'Thank you for your order.')
end
end
What am I missing? It works on localhost with gmail so I'm missing something in the sendgrid settings or in the default_url in production.rb file.
For posterity, here's a working setup for external SMTP in Rails on Heroku:
#config/environments/production.rb
config.action_mailer.smtp_settings = {
:address => "smtp.sendgrid.net",
:port => 587, # ports 587 and 2525 are also supported with STARTTLS
:enable_starttls_auto => true, # detects and uses STARTTLS
:user_name => ENV["SENDGRID_USERNAME"],
:password => ENV["SENDGRID_PASSWORD"], # SMTP password is any valid API key, when user_name is "apikey".
:authentication => 'login',
:domain => 'yourdomain.com', # your domain to identify your server when connecting
}
Change default from: "Test Email" to valid email address, even example#example.com.
I would just like to point out, this is for sending emails via SMTP. While this method is totally ok, you should also consider sending via the API.
To do this, you need to specify an interceptor. Luckily, there's a Gem that helps with that. Here's a good article showing how to use it.
https://rubyplus.com/articles/561-Sending-Emails-using-SendGrid-API-in-Rails-4-1
It took us a long time to resolve the issue when we tried to deploy the SMTP relay on heroku. It worked perfectly fine on local but when pushed we received socket errors and time out issues. Eventually got it working.
Important note: Make sure not to use starttls_auto and SSL/or TLS this causes open SSL issue.

Do I need to provide a "from" email address while using Heroku + SendGrid?

I have the standard setup in config/environments/production.rb
config.action_mailer.default_url_options = { host: "myapp.heroku.com" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:addresses => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
However, I encountered the following error while trying to send a mail:
ArgumentError: An SMTP From address is required to send a message. Set the message smtp_envelope_from, return_path, sender, or from address.
I then tried using the email I found on my heroku add-on page, i.e. app#######heroku.com, but now I got
Errno::ECONNREFUSED: Connection refused - connect(2)
So do I need to specify a from email or not? If yes, which one should I use?
Try this code in your production.rb file:
config.action_mailer.default_options = { from: "my_address#example.com" }
Yes you need to set a from address. All emails need an from address specified.
:from => "address_you_are_sending_from#your_domain.com"
This needs to be set in your _mailer.rb file. You can also set it gloablly in an initiallizer but it must always be referenced in your_mailer.rb files, and within each mail method itself.
You should change the :domain to a domain that you control. But if you were to use Heroku would your apps address not be yourapp.herokuapp.com instead of yourapp.heroku.com?
You can set a from address in your mailer file.
For example if you have an admin_mailer.rb for notifying your personal email of new activity on your site, it could look like this:
def new_customer_trial(customer_object)
#new_customer = customer_object
mail :to => "me#personal_email.com", :subject => "New Customer Trial", :from => "'My Site Notifications' <notifications#my_site.com>"
end
And you'd send that mail from a controller:
AdminMailer.new_customer_trial(customer_object)
or as a delayed job:
AdminMailer.delay.new_customer_trial(customer_object)

emails not sending in ruby on rails

I did the same thing as in this link http://www.tutorialspoint.com/ruby-on-rails/rails-send-email.htm. it is showing mail sent in output window of net-beans with the message but it is not actually sending the mail. can anyone tell me what could be the problem?
i have searched a lot but m not getting any solution.
I am using net-beans with ruby 1.5.1, rails 2.3.8. I have searched but I only got the solution to update j-ruby. If it is so then please tell me how to use updated version of j-ruby in net-beans.
the following code is in environment.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'gmail',
:user_name => "id#gmail.com",
:password => "",
:authentication => 'plain',
:enable_starttls_auto => true }
thanks
can you please check that whether you have do this or not
config.action_mailer.perform_deliveries = true
Please let me know
Thanks
Please check with your network provider if your smtp port is open. or check using telnet commands. If you work under a company network which has restrictions on sending mails, you should explicitly ask for permission and get the port opened.
The mail sent message will be shown in output even if it isnt actually sent. So pls make sure you have the following changes made to know the errors in your config/environments/development.rb or production.rb depending on in which mode you are running your server
config.action_mailer.raise_delivery_errors = true
Visit the following link for more detail about Action Mailer.
Update your mailer settings:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => 'gmail',
:user_name => "id#gmail.com",
:password => "******",
:authentication => 'plain',
:enable_starttls_auto => true }
Where ****** is your email password.

How to setup Google Apps with ActionMailer with Rails 2.3.5?

I've got Google Apps setup with email for my domain, and now I need to configure ActionMailer to use it. But the info I've found seems to be conflicting. Can anyone tell me how exactly to set it up with Rails 2.3.5?
I faced the same problems and got it working with this:
Step 1. Add the following to your development environment:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "domain.com",
:authentication => :plain,
:user_name => "YOUR_USER_NAME", #should be you#domain.com
:password => "YOUR_PASSWORD_HERE"
}
The critical line is :enable_starttls_auto. You have to restart webrick after making changes here.
Step 2. Create a Model like project_mailer.rb
class ProjectMailer < ActionMailer::Base
def confirmation(project)
subject 'Your email subject'
recipients project.email
from 'you#domain.com'
body :project => "hi"
end
end
Step 3. Create a View like /views/project_mailer/confirmation.html.haml (or .erb). This is just a standard view file.
Step 4. Add a line in your controller like:
ProjectMailer.deliver_confirmation(#project)

Resources