【问题标题】:pony doesn't send email to gmail address?小马不向gmail地址发送电子邮件?
【发布时间】:2011-07-27 18:59:26
【问题描述】:

我有一个要求提供人员姓名和电子邮件地址的报名表。我将该电子邮件地址保存到会话中,以便在提交表单后访问它。然后我使用 Pony 向提交表单的人发送一封感谢/通知电子邮件。但是,虽然它可以毫无问题地发送到 MobileMe 地址,但不会发送到 gmail 地址。我用来发送的线路是:

Pony.mail(:to => "#{@email}", :from => 'from@email.com', :subject => "Thanks for entering!", 
:body => "Thank you!")

@email 变量在处理程序中定义并从会话中获取值。

有什么想法吗?

【问题讨论】:

    标签: ruby sinatra pony


    【解决方案1】:

    这是我使用的辅助方法,它使用 Pony 在我的 Mac 上开发时使用 sendmail 发送电子邮件,或者在生产时使用 Heroku 上的 sendgrid 发送电子邮件。这工作可靠,我所有的测试电子邮件都会发送到我的各种 gmail 地址。

    您的问题可能是您的from 地址无效,而 Google 将其标记为垃圾邮件。另外我注意到您没有设置Content-Type 标头,在我的情况下通常是text/html

    def send_email(a_to_address, a_from_address , a_subject, a_type, a_message)
      begin
        case settings.environment
        when :development                          # assumed to be on your local machine
          Pony.mail :to => a_to_address, :via =>:sendmail,
            :from => a_from_address, :subject => a_subject,
            :headers => { 'Content-Type' => a_type }, :body => a_message
        when :production                         # assumed to be Heroku
          Pony.mail :to => a_to_address, :from => a_from_address, :subject => a_subject,
            :headers => { 'Content-Type' => a_type }, :body => a_message, :via => :smtp,
            :via_options => {
              :address => 'smtp.sendgrid.net',
              :port => 25,
              :authentication => :plain,
              :user_name => ENV['SENDGRID_USERNAME'],
              :password => ENV['SENDGRID_PASSWORD'],
              :domain => ENV['SENDGRID_DOMAIN'] }
        when :test
          # don't send any email but log a message instead.
          logger.debug "TESTING: Email would now be sent to #{to} from #{from} with subject #{subject}."
        end
      rescue StandardError => error
        logger.error "Error sending email: #{error.message}"
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2021-08-11
      • 2012-06-26
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 2013-12-28
      相关资源
      最近更新 更多