Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send sender name with email SendGrid - Rails

I am using SendGrid to send emails from my application. I want to send sender name along with sender email e.g. from: 'Maxcreet <[email protected]>'

It is working fine using Letter Opener and MailTrap but SendGrid only show sender email.

Here is my SendGrid functionto send emails.

def deliver!(mail)
  email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address)
  email.subject = mail.subject
  email.add_personalization(build_personalization(mail))
  build_content(mail)
  send!(email)
end

I have checked mail[:from] values using puts it gives following values:

puts mail[:from]                      => Maxcreet <[email protected]>
puts mail[:from].addrs                => Maxcreet <[email protected]>
puts mail[:from].addrs.first          => Maxcreet <[email protected]>
puts mail[:from].addrs.first.address  => [email protected]

Above 3 seems OK for me but when I use any of them in

email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address)

It does not sent my email and even I do not find my email in sendgrid dashboard.

Following this also tried email.fromname but this even did not work.

like image 417
Uzair Nadeem Avatar asked Nov 03 '25 03:11

Uzair Nadeem


1 Answers

SendGrid's ruby API has this option with name name and not fromname.

So the following should solve your problem.

email.from = SendGrid::Email.new(
  email: mail[:from].addrs.first.address, 
  name:  mail[:from].addrs.first.name
)

I concluded this by trying it from this doc.

like image 98
Zia Ul Rehman Mughal Avatar answered Nov 04 '25 16:11

Zia Ul Rehman Mughal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!