Can I send email from different addresses using same ActionMailer

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 07:37:19

问题


I'm using ActionMailer for my Rails 2.3.9 application.

When I send an email using:

  deliver_user_invite

config:

  def user_invite(subject, content)
    subject subject
    from "User Invite <invite@mydomain.com>"
    recipients "invites@mydomain.com"
    sent_on Time.now
    content_type "text/html"
    body :content => content
  end

with SMTP configuration

  config.action_mailer.smtp_settings = {
      :enable_starttls_auto => true,
      :address        => 'smtp.gmail.com',
      :port           => 587,
      :domain         => 'mydomain.com',
      :authentication => :plain,
      :user_name      => 'user@mydomain.com',
      :password       => 'password'    
  }

However when the email is sent, the sender email shows as user@mydomain.com instead of invite@mydomain.com.

Can I have different SMTP configuration for different email addresses? or Is there a way to set the sender email address from ActionMailer config?


回答1:


This is a Gmail SMTP limitation. It will always make the sender of the e-mail be the username/login you use for your smtp settings, and will ignore your from address.

A possible workaround might be to change the smtp settings dynamically when you need to send as someone else.

Edit: You might be able to go into the settings for your own Gmail account and use the "Add another email address you own" option to allow your account to send through those e-mail addresses. I haven't tested it, but it might work. (See http://www.mobileread.com/forums/showpost.php?p=21093&postcount=1).



来源:https://stackoverflow.com/questions/5058743/can-i-send-email-from-different-addresses-using-same-actionmailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!