Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: [email protected]
    password: example_password
    authentication: :login
    enable_starttls_auto: true
    ssl: true

i am trying to send the like this :-----

Mail.deliver do

  to  '[email protected]'
  from    '[email protected]'
  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 [email protected] instead of [email protected], why it is so i am not able to figure out.

thanks for any comment and answers.

like image 260
Sachin Singh Avatar asked Mar 22 '23 15:03

Sachin Singh


2 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.

like image 126
tadman Avatar answered Apr 01 '23 05:04

tadman


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 [email protected] address, or whichever address you specify in the from attribute.

There's further Ruby & SendGrid details in the SendGrid documentation.

like image 26
Martyn Davies Avatar answered Apr 01 '23 04:04

Martyn Davies