Response from SMTP server with Rails - ruby-on-rails

How could I get the response from an SMTP server in Ruby on Rails using ActionMailer, when I send an email with the Mailer.deliver method?
I found the Actionmailer SMTP Server Response answer but it doesn't work... Any idea?
I need it because AWS SES return a message ID through, and it's the only way to get the message linked to a bounce or spam report they provide after.

if you get the smtp in way like this
response = Mail.your_mail().deliver
and it isn't getting the response that you need, try deliver_now!
response = Mail.your_mail().deliver_now!
pp response.message
This will show you the result: 250 2.0.0 Ok: queued as 3lG8S16Khmz6ZhXq
I also configured the smtp settings on my Mail.rb
delivery_options = { user_name: 'email#server.com.br',
password: 'mypassword',
authentication: :login,
address: 'smtp.mycompany.com.br',
port: 2525,
domain: 'mycompany.com.br',
return_response: true
}

If using gem "aws-ses" you can get the message id with:
response = your_mailer.send(notification, self, *args).deliver_now!
response.message_id # => "010601531c886c55-6c2f30fc-4237-4294-9029-b60b2d44a691-000000#email.amazonses.com"
For me deliver, deliver_now and deliver_now! work in the same way
EXTRA:
If for some chance you are using devise you can get the ID too:
#[app/model/user.rb]
class User < ActiveRecord::Base
...
# Override send_devise_notification stated in devise/REAMDE.txt
def send_devise_notification(notification, *args)
response = devise_mailer.send(notification, self, *args).deliver_now
response.message_id #=> "010601531c8-86c...000000#email.amazonses.com"
end
...
end

Related

How to send an email with mail gem in ruby on rails

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.

Seding emails with Devise gem and Mailgun Api

I want to send automated emails via Mailgun either SMTP or API. The problem is that in tutorials I find they explain how to do that manually e.i creating mailer class etc. For example like that:
def send_simple_message
RestClient.post "https://api:YOUR_API_KEY"\
"#api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
:from => "Excited User <mailgun#YOUR_DOMAIN_NAME>",
:to => "bar#example.com, YOU#YOUR_DOMAIN_NAME",
:subject => "Hello",
:text => "Testing some Mailgun awesomness!"
end
This is from official Mailgun documentation.
But I am using Devise gem which has email sending implemented.
For example I want to send password reset email. When I click forgot password and submit my email from logs I see that my email is tried to be sent, but not sent of course, I need to set up email server.
So the question is where is this code for sending recovery email is written in devise, how to override it? I want it to oveeride so it will use Mailgun API for example.
I have already generated registrations_controller.rb using
rails generate devise:controllers registrations
command. So I suppose I am overriding it here?
Any suggestions?
have you read this tutorial? Looks like you need to setup it in config/environments/development.rb
Something like:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: ENV['GMAIL_DOMAIN'],
authentication: 'plain',
user_name: ENV['GMAIL_USERNAME'],
password: ENV['GMAIL_PASSWORD']
}
Also, you can try to use mail gun gem. Looks like It's really easy to setup it
config.action_mailer.delivery_method = :mailgun
config.action_mailer.mailgun_settings = {
api_key: '<mailgun api key>',
domain: '<mailgun domain>'
}
Hope it helps you.

Devise registration confirmation email delivered 20 minutes late + What delivery method does Devise mailer use?

I have two problems I have been wasting a lot of time on for the past 3 days. I would really appreciate experienced coders helping me out here,.
I have a model User that I send a confirmation email using Devise invitable from the registration controller.
User.invite!({:email => email, ...)
Now I have these settings
In config/environments/development.rb
config.action_mailer.delivery_method = :smtp
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.smtp_settings = {
address: 'mail.myapp.com',
port: 26,
domain: 'myapp.com',
user_name: 'notifications#myapp.com',
password: 'xxxx',
authentication: 'plain',
enable_starttls_auto: true,
openssl_verify_mode: 'none'
}
and config/devise.rb
config.mailer_sender = 'notifications#myapp.com'
This mail is delivered exactly 20 minutes later after the debug output says "Sent email to ..."
Now I also use a custom NotificationsMailer else where in the code, where I send notification emails other than the devise based registration process. I have the same settings as above entered in config/initializers/setup_mail.rb and in this case, the mails are delivered instantaneously.Why this delay?
Question/doubt #2
I figured if I could intercept the delivery process of sending the mail, I would be able to check the Mail::Message object through logging and later develop an asynchronous way of handling using Sidekiq.
So in config/application.rb
ActionMailer::Base.add_delivery_method :queued, :QueuedDelivery
In config/environments/development.rb
config.action_mailer.delivery_method = :queued
In lib/queued_delivery.rb
class QueuedDelivery
def deliver!(mail)
logger.debug "mail object string rep: #{mail.to_s}"
logger.debug "mail object settings: #{mail.deliver_method.settings}"
end
end
In spite of changing the delivery method, it's weird Devise still sends the mail which arrives 20 minutes late as usual and I cant see the debug level logging that I put above.
How is Devise sending these emails?! I checked devise_mail() in the devise gem, and it uses this code finally mail headers_for(action, opts). What is going on here?

sender email is being overridden by smtp settings user_name

i am writing a ruby script to send email using 'mail' gem.
and my smtp settings on my local machine:
mailer_options:
address: smtp.gmail.com
port: 465
domain: gmail.com
user_name: example#gmail.com
password: example_password
authentication: :login
enable_starttls_auto: true
ssl: true
i am trying to send the like this :-----
Mail.deliver do
to 'receiver#gmail.com'
from 'sender#gmail.com'
subject 'Test Mail'
text_part do
body 'Hello World!!!!!'
end
end
the mail is send successfully but when i open the email i see sender email id as
example#gmail.com instead of sender#gmail.com, why it is so i am not able to figure out.
thanks for any comment and answers.
This is often done by your SMTP server and is beyond your control. You could try using a different SMTP provider like Sendgrid if Google isn't working out for you.
Correct answers above, it's not your code, it's the Gmail SMTP servers that do this. I work for SendGrid and if you wanted to change this over to using SendGrid (or any other provider for that matter) then you can do it really easily. Our free plan can send 400 emails a day and is fine for local development.
Your code would change as follows:
mailer_options:
address: smtp.sendgrid.net
port: 587
domain: yourdomain.com
username: your_username
password: your_password
authentication: plain
enable_starttls_auto: true
You don't need to have SSL set at this stage. From here you can use your original Mail.deliver method.
You'll find you can now send from the sender#yourdomain.com address, or whichever address you specify in the from attribute.
There's further Ruby & SendGrid details in the SendGrid documentation.
google does not allow sender email masking. This is done by GMAIL's server. Not by your rails code!!. It always uses email address of gmail account you are using as "from_email".
Your best alternative might be "Mandrill" (12000 emails free / month). They allow email routing in the way you want.
Please set sender default name instead of 'sender#gmail.com' at below :
class UserMailer < ActionMailer::Base
default from: 'sender#gmail.com'
def welcome_email(user)
#user = user
#url = 'http://example.com/login'
mail(to: #user.email, subject: 'Welcome to My Awesome Site')
end
end

No email alerts in mailcatcher, but able to send emails to valid account?

I am running a Rails app and using Mailcather gem as an SMTP service. It was said that it can catch all outgoing emails, however I've done making correct settings in config/environments/development.rb, testing it to send email, but no email catched in either 127.0.0.1:1080 and localhost:1080. But the email was sent and received tho. I've tried all of the possible configurations. Here is my config/development.rb
config/development.rb
Ror::Application.configure do
# Mailer
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => '127.0.0.1:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "127.0.0.1", :port => 1025 }
end
Here is my user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "aaaaaaaaa#gmail.com"
def registration_confirmation(user)
#user = user
mail(:to => user.email, :subject => "Registered")
end
end
I used the registration_confirmation() method to check whether user is registered or not via email. But none of of the email popped up in mailcatcher. I did install it under rvm. I did test install it both with wrapper and without wrapper but the result still the same. Bottom line is, it is able to send emails outside, but can't receive email inside. Or any notifications. Did I miss something? Any advice or corrections would be appreciated. Thanks
You call the mail method like this:
UserMailer.registration_confirmation(user).deliver

Resources