简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM