sender email is being overridden by smtp settings user_name - ruby-on-rails

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

Related

Sending emails with pure Ruby

I have problems with sending emails with pure Ruby. Here is how my script looks like:
Mail.defaults do
delivery_method(
:smtp,
address: 'smtp.sendgrid.net',
port: 587,
user_name: 'sendgrid_username',
password: 'sendgrid_password',
authentication: :login,
enable_starttls_auto: false,
openssl_verify_mode: "none"
)
end
Mail.deliver do
from 'Test Email'
to 'user#example.com'
subject 'Here is the image you wanted'
body "Test Email"
end
This script raise the following error:
/Users/mateuszurbanski/.rubies/ruby-2.7.2/lib/ruby/2.7.0/net/smtp.rb:975:in `check_auth_response': 535 Authentication failed: Bad username / password (Net::SMTPAuthenticationError)
I'm using credentials from one of my Ruby on Rails project and they are fine. Any ideas?
Sendgrid recently transitioned to api keys and will reject plain auth, see details here.
Your old credentials while still being valid may not be accepted.
Generate a new api key in sendgrid and use it in place of password. Username will be apikey
Instead of using your SendGrid username and password in this config, use the following,
user_name: 'apikey',
password: '<sendgrid_api_key>',
This works for me when defining the smtp settings for ActionMailer, so it should work here as well.

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, admin_mailer & config

I'm using devise with my rails 4 app.
I'm trying to setup figaro before I let the site go live, but for now, I'm managing my test email account with my passwords in production.rb. It won't be this way for long, but I have hit a problem with my setup.
I have two admin_mailer emails that get sent when someone registers. One is an email to me, using one gmail account to let me know that they have registered. The second is an email to the user, welcoming them to the site. It is supposed to be sent from a second email account that I set up.
In my production.rb, I have:
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: ###,
domain: 'gmail.com',
user_name: 'firstemailaddress#gmail.com',
password: '###',
authentication: 'plain',
enable_starttls_auto: true }
{
address: 'smtp.gmail.com',
port: ###,
domain: 'gmail.com',
user_name: 'secondemailaddress#gmail.com',
password: '####',
authentication: 'plain',
enable_starttls_auto: true }
In my admin_mailer, I set the sender for the emails as one or the other of those user names.
However, when I run the code, both emails are sent from the first email address.
Does anyone know how to set up to mailers so that emails send from different addresses?
Thank you
I think each mailer is supposed to be associated with one email account. You could create two separate mailers, and define the smtp settings within each individual mailer rather than in production.rb.
For example, app/mailers/first_mailer.rb :
class FirstMailer < ActionMailer::Base
default from: 'firstemailaddress#gmail.com'
self.delivery_method = :smtp
self.smtp_settings = {
# your gmail smtp settings here
}
def some_email(user)
mail(to: user.email, subject: 'Subject')
end
end
You can send this by calling FirstMailer.some_email(user).deliver.
Similarly, you then create another file app/mailers/second_mailer.rb that uses a different account (and so different settings).
There may be a more DRY way to do this, but I know this works.

Running into SMTP error when trying to send email in RoR app

I've been desperately trying to send an email using the Action Mailer class in RoR, and even though the terminal shows that a message is being sent, it keeps returning with this error:
et::SMTPAuthenticationError: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbtOS
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:968:in `check_auth_response'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:739:in `auth_plain'
from /Users/abhasarya/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/smtp.rb:731:in `authenticate'...
Here's what my mailer class looks like within app/mailers/my_mailer.rb:
class MyMailer < ActionMailer::Base
default from: "from#example.com"
def welcome_email(user)
#user = user
mail(to: user.email,
from: 'example_email#gmail.com',
subject: 'Welcome!')
end
end
And here's what my config/application.rb file looks like:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
#domain: 'gmail.com',
user_name: 'example_email#gmail.com',
password: 'my_password',
authentication: 'plain',
enable_starttls_auto: true
}
config.action_mailer.default_url_options = {
:host => "localhost",
:port => 3000
}
in my config/environments/development.rb file I also have these two lines:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
My problem is that whenever I start the rails console (working in development, have no idea how to run in production), assign a user's email to the user variable then enter the command: MyMailer.welcome_email(user).deliver
the terminal says:
Rendered my_mailer/welcome_email.html.erb (11.7ms)
Rendered my_mailer/welcome_email.text.erb (0.5ms)
Sent mail to abhas.arya#yahoo.com (923.1ms)
Date: Fri, 08 Aug 2014 13:54:38 -0400
From: abhas.aryaa#gmail.com
To: abhas.arya#yahoo.com
Message-ID: <53e50ededebb2_2b0b8082dbf8507c5#Abhass-MacBook-Pro.local.mail>
Subject: Welcome!
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_53e50eded7355_2b0b8082dbf85068f";
charset=UTF-8
Content-Transfer-Encoding: 7bit
but then renders the error. My views are simple enough that I didn't think I needed to include them here. I've desperately tried everything, including unlocking google captcha but still nothing works. I'd appreciate any help, all I want is for an email to get sent to the proper inbox. I'd love you forever if you can solve this issue!!!
The reason that you are seeing this error is that Google has blocked your IP. You will need to enable authorization for this IP.
Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue.
Then again try to send email from the application, it should work. You will need to send email from your app within 10 min of visiting the above link. By visiting above link, Google will grant access to register new apps for 10 min.
I solved by doing the following:
1) Log into the Google Apps Admin portal and enable Less Secure Apps by checking "Allow users to manage their access to less secure apps" under Security preferences.
2) Login into the account that will serve as the mailer and turn "Allow less secure apps" to ON.
Most of the answers available on-line refer to step 2, but you cannot configure at a user level without the Admin turning the feature on.
This one solved the issue for me after some investigation:
Click https://www.google.com/settings/u/0/security/lesssecureapps
Enable access for less secure apps here by logging in with the email address you have provided in smtp configuration.
I resolved my issue by doing this.
config.action_mailer.default :charset => "utf-8"
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.gmail.com',
port: 587,
domain: 'mysite.com',
user_name: myemail#gmail.com,
password: mypassword,
authentication: 'plain',
enable_starttls_auto: true
}
As google will try to block your sign-in if you have turned off access for less secure apps in your accounts settings. Therefore, follow this link and "Turn on" access for less secure apps.

ActionMailer with SMTP - Bad recipient address syntax

My user has email with this format: "-user-#domain.com". Mailgun validation succeeded but Rails couldn't send email to the address. I'm using SMTP with Mandrill.
This is the error message:
/home/johnny/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/net/smtp.rb:948:in `check_response': 401 4.1.3 Bad recipient address syntax (Net::SMTPServerBusy)
Do you have any idea?
Thanks in advance.
Updated:
This sample code (with valid SMTP configuration) would raise the error:
#!/usr/bin/env ruby
require 'mail'
options = {
address: "smtp.mandrillapp.com",
port: 587,
domain: "mydomain.com",
authentication: "login",
user_name: "myemail#mydomain.com",
password: "mypassword",
enable_starttls_auto: false
}
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
from 'valid.email#domain.com'
to "-test-#domain.com"
subject 'Testing sendmail'
body 'Testing sendmail'
end
Even if the starting dash in the email address is valid, most mail servers do not accept such emails due to restrictions for command line arguments.
A quick fix you can try is wrapping the email address with angle brackets:
Mail.deliver do
from 'valid.email#domain.com'
to "<-test-#domain.com>"
subject 'Testing sendmail'
body 'Testing sendmail'
end

Resources