Rails ActionMailer – format sender and recipient name/email address

Rails ActionMailer – format sender and recipient name/email address

Add Comment


  • 13 Answer(s)

       The name and email validate or escape the name and email, can invalid From header on simply concatenating strings.

    require 'mail'
    address = Mail::Address.new email # ex: "[email protected]"
    address.display_name = name.dup # ex: "John Doe"
    # Set the From or Reply-To header to the following:
    address.format # returns "John Doe <[email protected]>"
    

    Answered on January 21, 2019.
    Add Comment

    recipients = “\”#{user.name}\” ”
    @from = “\”MyCompany\” “

    Answered on January 23, 2019.
    Add Comment

    @recipients = “\”#{user.name}\” ”
    @from = “\”MyCompany\” “

    Answered on January 23, 2019.
    Add Comment

    In rails3 I place the following in each environment. i.e. production.rb

    ActionMailer::Base.default :from => “Company Name ”
    Placing quotations around the company name did not work for me in Rails3.within Rails 2.3.3 a bug within the ActionMailer was introduced. You can see the ticket over here Ticket #2340. It’s resolved in 2-3-stable and master so it will be fixed in 3.x and 2.3.6.

    For fixing the problem within 2.3.* you can use the code provided within the ticket comments:

    module ActionMailer
    class Base
    def perform_delivery_smtp(mail)
    destinations = mail.destinations
    mail.ready_to_send
    sender = (mail[‘return-path’] && mail[‘return-path’].spec) || Array(mail.from).first

    smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
    smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
    smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
    smtp_settings[:authentication]) do |smtp|
    smtp.sendmail(mail.encoded, sender, destinations)
    end
    end
    end
    end

    Answered on January 27, 2019.
    Add Comment
    @recipients   = "#{user.email}"
    @from         = "[email protected]"
    @subject      = "Hi"
    @content_type = "text/html"
    Answered on January 28, 2019.
    Add Comment

    recipients = “\”#{user.name}\” ”
    @from = “\”MyUniversity\” “

    Answered on February 2, 2019.
    Add Comment
    def self.named_email(name,email) "\"#{name}\" <#{email}>" end
    default :from => named_email(user.name, user.email)
    Answered on February 2, 2019.
    Add Comment

    Is there a way to specify email AND name for sender and recipient info when using ActionMailer

    Typically you’d do:

    @recipients = “#{user.email}”
    @from = “[email protected]
    @subject = “Hi”
    @content_type = “text/html”
    But, I want to specify name as well– MyCompany , John Doe .

    Answered on February 5, 2019.
    Add Comment
    module ActionMailer
      class Base
        def perform_delivery_smtp(mail)
          destinations = mail.destinations
          mail.ready_to_send
          sender = (mail['return-path'] && mail['return-path'].spec) || Array(mail.from).first
    
          smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
          smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
          smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
                     smtp_settings[:authentication]) do |smtp|
            smtp.sendmail(mail.encoded, sender, destinations)
          end
        end
      end
    end
    Answered on February 5, 2019.
    Add Comment

    If you are taking user input for name and email, then unless you very carefully validate or escape the name and email, you can end up with an invalid From header by simply concatenating strings. Here is a safe way:

    require 'mail'
    address = Mail::Address.new email # ex: "[email protected]"
    address.display_name = name.dup   # ex: "John Doe"
    # Set the From or Reply-To header to the following:
    address.format # returns "John Doe <[email protected]>"
    Answered on February 9, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.