I've read all the other SO answers, but I still can't fix this.
Given my RewardMailer has the following email defined:
# A test email to check up on the configurations
def test_mail
#recipients = 'aminshahgilani#gmail.com'
#from = 'postmaster#sandboxXXXXXXXXXXXXXXXXX.mailgun.org'
#subject = 'test from the Rails Console'
#body = 'This is a test email'
end
And that my config/environments/development.rb file contains:
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.mailgun.org',
port: 587,
domain: 'sandboxXXXXXXXXXXXXXXXXX.mailgun.org',
user_name: 'postmaster#sandboxXXXXXXXXXXXXXXXXX.mailgun.org',
password: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
authentication: :plain
}
And when I check from the Rails console, I confirm the configuration with:
Rails.application.config.action_mailer
=> {:perform_deliveries=>true, :raise_delivery_errors=>true, :delivery_method=>:smtp, :smtp_settings=>{:address=>"smtp.mailgun.org", :port=>587, :domain=>"sandbox0574dfb215d14874ac51fb9f9052131b.mailgun.org", :user_name=>"postmaster#sandboxXXXXXXXXXXXXXXXXX.mailgun.org", :password=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", :authentication=>:plain}, :assets_dir=>"/home/gilani/Sandbox/mailman/public", :javascripts_dir=>"/home/gilani/Sandbox/mailman/public/javascripts", :stylesheets_dir=>"/home/gilani/Sandbox/mailman/public/stylesheets", :preview_path=>"/home/gilani/Sandbox/mailman/test/mailers/previews", :asset_host=>nil, :relative_url_root=>nil}
Why does the following not deliver my email? Or show any error for it at all
RewardMailer.test_mail().deliver_now
RewardMailer#test_mail: processed outbound mail in 0.1ms
=> nil
I checked with my Mailgun logs as well, nothing. The logs are empty!
You are not calling mail method in your test_mail.
Related
I am trying to implement sendgrid into my backend api rails system so that when a user signs up I can send them a welcome email. After making a post request and handling user creation, I get this verification:
UserMailer#send_sign_up_email: processed outbound mail in 43.5ms
Sent mail to *******#gmail.com (185.8ms)
Date: Wed, 28 Feb 2018 16:54:05 -0800
From: *******#gmail.com
To: *******#gmail.com
Message-ID: <5a974f2d39c92_c5b2abcd76769fc423e0#albert-VirtualBox.mail>
Subject: Welcome to BottlesTonight albert jin!
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
My code looks exactly like in this link https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html.
This looks all fine and well, but the email is just not sending (I put stars here for the email but I actually put in my email, and I used another one of emails as the default for sending). There is no email in my outbox or inbox.
However, now that I think about it, I never "logged in" with my email or entered the password, so the application shouldn't have access to send emails with my email. I also never did anything with the api key that I made on my sendgrid account. Furthermore, for the environment.rb file, I wasn't sure what to put in domain, so I put gmail.com. These all seem kinda sketchy to me, I think the tutorial doesn't contain everything. Does anyone know how to configure this? I've been stuck on it for a while.
Edit:
I tried doing it on production and it is not working. Here is more info:
My production.rb looks like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => ENV['DEFAULT_HOST'] }
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
}
I have a heroku sendgrid add on. I have set the heroku config vars. In my registrations controller I merely added the line:
UserMailer.send_sign_up_email(#current_user).deliver
My mailer class looks like:
def send_sign_up_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! #{#user.first_name}")
end
However, when I sign up on my website, the user gets added to the database but the email is not sending. Does anyone know why, or how I can debug?
I would suggest to remove all config for ActionMailer from your environment files (i.e. files under /config/environments), except following ones
# Don't care if the mailer can't send.
config.action_mailer.perform_caching = false
# Reference: http://stackoverflow.com/a/20770131/936494
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
Then create an initializer /config/initializers/mail_config.rb and add following code to it:
TEST_ENVS = %w(test)
FILESYSTEM_ENVS = TEST_ENVS + %w(development)
# =========== DELIVERY METHOD SETTING
delivery_method = case Rails.env.to_sym
when :production, :staging, :experimental
:sendmail
when :test
:test
else
:smtp
end
ActionMailer::Base.delivery_method = delivery_method
# =========== SMTP SETTINGS
ENVS_TO_USE_GMAIL_CONFIG = %w(development test)
gmail_config = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: ENV['MAIL_USER_NAME'],
password: ENV['MAIL_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true
}
if :smtp == delivery_method
use_gmail_config = ENVS_TO_USE_GMAIL_CONFIG.include?(Rails.env)
smtp_settings = ( use_gmail_config ? gmail_config : {} )
ActionMailer::Base.smtp_settings = smtp_settings
end
# =========== DEFAULT URL OPTIONS
default_url_options_settings = Settings.default_url_options
host = default_url_options_settings.host
port = default_url_options_settings.port
protocol = default_url_options_settings.protocol
default_url_options = {}
default_url_options[:host] = host if host.present?
default_url_options[:port] = port if port.present?
default_url_options[:protocol] = protocol if protocol.present?
ActionMailer::Base.default_url_options = default_url_options
The Settings object is available as part of using config gem. If you do not want to use that gem for configuring env-specific values then you can just hard-code them in the initializer and try it.
My /config/settings.yml looks like
default_url_options:
host: ''
port: ''
protocol: ''
and my /config/settings/development.yml looks like
default_url_options:
host: 'localhost'
port: '3000'
Having a centralized mailer config helps in diagnosing mailer settings related issues in quick manner.
First try it for Gmail account and if it works you can be sure that sending email works. Just make sure in your Gmail account Less Secure Apps setting is enabled.
I am having some problem with sending email in my web-application. When I am running development the email is not sent even though it is showing as sent in the Console.
This is my settings in config/environments/development.rb
# Email
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: '587',
enable_starttls_auto: true,
user_name: ENV["gmail_username"],
password: ENV["gmail_password"],
authentication: :plain,
domain: 'gmail.com'
}
In my controller I am sending mail using this call:
JobMailer.send_accept_job_mail(#app_user, #job.id).deliver
The mail is showing as "sent" in the Console, but it is not sent to the specified mail.
You need to use deliver! to send out the mail immediately.
Just update the code to
JobMailer.send_accept_job_mail(#app_user, #job.id).deliver!
-- edited to use deliver! method, per comments.
We use SendGrid in a production app and it works fine. We were recently trying to test a new feature/email in development however and cannot seem to get an email to send. Any idea where we're going wrong? We are using similar features to production and we also followed SendGrid's implementation guide. Feels like I'm missing something simple!
First I exported the SENDGRID_USERNAME and SENDGRID_PASSWORD and for kicks added it to my .bash_profile
export SENDGRID_USERNAME=xxxxxxx
export SENDGRID_PASSWORD=xxxxxxx
I've confirmed in the console that these exist and are correct.
Created a developer_email.html.erb file:
<p>Hi! Sendgrid test</p>
And a DeveloperMailer file:
class DeveloperMailer < ActionMailer::Base
default from: "tom#xxxxxx.com"
def developer_email(developer_id)
#recipients = ["tom#xxxxxx.com"]
mail(to: #recipients, subject: 'Does sendgrid work?')
end
end
Updated the development.rb file:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
port: '587',
domain: 'localhost:3000',
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true }
When I go to send the email in the console, it acts like it sent, but the email never actually arrives:
DeveloperMailer.developer_email(1) #to send the email. Seems to work:
2.3.1 :001 > DeveloperMailer.developer_email(1)
Rendered developer_mailer/developer_email.html.erb (1.5ms)
DeveloperMailer#developer_email: processed outbound mail in 133.3ms
=> #<Mail::Message:70263824429080, Multipart: false, Headers: <From: tom#xxxxx.com>, <To: ["tom#xxxx.com"]>, <Subject: Does SendGrid Work?>, <Mime-Version: 1.0>, <Content-Type: text/html>>
#But I never get anything sent to my email
Any idea what I might be missing?
EDIT
Updated development.rb file:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.sendgrid.net',
domain: 'example.com',
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
authentication: :plain,
enable_starttls_auto: true }
Still no email though.
First, do telnet by following commands
telnet smtp.sendgrid.net 2525
If you get proper response from SMTP Service then change SMTP port from 587 to 2525
In my case "587 port" is not responding
After changing the port it works for me
domain in this context is an SMTP HELO domain, not the actual environment's domain. Change it to the same domain you use in your SendGrid profile and don't specify a port and give it a try.
You'd need to verify the sender email or domain of the sender email before you can send an email with SendGrid. See https://sendgrid.com/docs/ui/sending-email/sender-verification/ and https://sendgrid.com/docs/for-developers/sending-email/sender-identity/#domain-authentication
I am trying to send an email using mail gem. But Unfortunately it is not working.
This is my controller.
def create
fn = params["firstname"]
ln = params["lastname"]
email = params["email"]
file = params["file"]
summery = params["summery"]
email_body = "Hello\n This is Your favorite website.\nA I want to personaly say hi."
mail = Mail.new do
from 'someone#gmail.com'
to email
subject "Saying Hi"
body email_body
end
mail.add_file(filename: file.original_filename, content: File.read(file.tempfile.path)) unless file.nil?
mail.deliver!
render json: {message: "A bug has been created", success: true}, status: 201
end
This code is producing this error
Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:
However when I am installing the mailcatcher and configure my controller to send the mail to mailcatcher, I can see the email in my mailcatcher UI.
Mail.defaults do
delivery_method :smtp, address: "localhost", port: 1025
end
Also I have add this two lines to my config/environment/development.rb
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
From my searches I saw that some people are mentioning that dont send email on development mode, however on this case I really want to test the full capability.
Update
As #Uzbekjon and #SimoneCarletti suggested I change my code to use the ActionMailer. I created the a file in app/mailer/ and I am calling that from my controller.
def create
fn = params["firstname"]
ln = params["lastname"]
email = params["email"]
file = params["file"]
summery = params["summery"]
email_body = "Hello\n This is Your favorite website.\nA I want to personaly say hi."
WelcomeMailer.welcome(fn, ln, email, file, email_body).deliver_now
render json: {message: "An Email has been send", success: true}, status: 201
end
and This is my mailer
class WelcomeMailer < ActionMailer::Base
default from: "someone#yahoo.com"
def welcome(first_name, last_name, email, file, email_body)
attachments["#{file.original_filename}"] = File.read("#{file.tempfile.path}")
mail(
to: email,
subject: 'Welcome to My Awesome Site',
body: email_body
)
end
end
However I am still getting the same error.
Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:
Answer
So I found the solution. Yes you need to use the ActionMailer. After that you need to go to the config/environments/development.rb , and modify and add these lines:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:user_name => "YOUR EMAIL",
:password => "YOUR Password",
:authentication => "plain",
:enable_starttls_auto => true
}
Also If Gmail complained about this:
Net::SMTPAuthenticationError - 534-5.7.9 Application-specific password required
Go to this link and let less secure application access Gmail.
Other configurations are available for other services like Yahoo. Just Google it.
Errno::ECONNREFUSED - Connection refused - connect(2) for "localhost" port 25:
Looks like mail gem is trying to connect to your local smtp server on port 25. Most probably you don't have the service running and receiving connections on port 25.
To solve, install and run sendmail or postfix on your machine.
PS. Use ActionMailer.
You don't have a mail server running on port 25. You can install postfix and start the server using
sudo postfix start
And then modify the settings in config/environments/development.rb
config.action_mailer.delivery_method = :sendmail
Hope this helps.
My Rails mailer worked perfectly a few months ago. I didn't deal with it for a long time, and I don't remember changing anything either. But now, no email is sent when I activate the mailer via my console, although I don't receive any errors.
I triple-checked that the email addresses and passwords are correct. I also set up my email according to this answer. But no emails are sent. If something is going wrong, shouldn't I at least be getting an error in my console?
I'm using Rails 4.0.10.
config/environments/development.rb
Website::Application.configure do
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host:'localhost', port: '3000' }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default :charset => "utf-8"
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'localhost:3000',
user_name: 'xxxxxx#xxxxxxx',
password: 'password',
authentication: 'plain',
enable_starttls_auto: true
}
end
app/mailers/user_mailer
class UserMailer < ActionMailer::Base
default from: "xxxxxx#xxxxx"
def notify(user)
#user = user
mail(to: #user.email,subject: "subject")
end
end
Console
$ UserMailer.notify(User.first)
Rendered user_mailer/notify.html.erb (0.4ms)
=> #<Mail::Message:2623523, Multipart: false, Headers: <From: xxxxxx#xxxxx>, <To: xxxxxxx#xxxxx>, <Subject: subject>, <Mime-Version: 1.0>, <Content-Type: text/html>>
Use Mailer methods deliver,deliver_now or deliver_later in your controller or Model
UserMailer.notify(User.first).deliver
You have to call .deliver_now or .deliver_later on the result of your notify method. Check out how to use ActionMailer for more information.