【问题标题】:Not able to send html mail from rails 4.0.8无法从 rails 4.0.8 发送 html 邮件
【发布时间】:2015-02-05 12:13:13
【问题描述】:

我已经开始从 rails 4.0.8 发送邮件,但我无法发送 html 邮件,我为我的邮件程序创建了 html 视图,但仍以文本/纯文本执行。我的邮件程序方法如下(customer_mailer.rb)

  def X customer, message, cust_mail, email_subject
    subject = email_subject
    mail_ids=cust_mail.to_s.split(",")
    mail :subject => subject || "Your Invoice", :to => mail_ids, :body => message

  end

我的 html 视图 (X.html.erb)

<html>
<body bgcolor="#D6A1A1">

<table width="100%" bgcolor="#D6A1A1" cellpadding="10" cellspacing="0">

</table>

</body>
</html>

我能够显示静态 html 页面以及如何将消息从方法 X 传递给 x.html.erb。当我删除以下行 ':body => message' 时它开始工作

【问题讨论】:

  • 就像你在 development.rb config.action_mailer.perform_deliveries = true 中指定的检查一样,默认情况下它是假的
  • 我的 development.rb 文件中没有那行代码,我在我的文件中添加了那行代码并尝试过,但我仍然面临同样的问题
  • 您遇到了什么错误?
  • 我没有收到任何错误,而是作为文本/纯文本执行,我需要制作 html 邮件
  • 您能添加更多代码吗?您是如何调用该方法的,您的配置方式是否准确?

标签: ruby-on-rails ruby email ruby-on-rails-4 html-email


【解决方案1】:

改变

mail :subject => subject || "Your Invoice", :to => mail_ids, :body => message

mail :subject => subject || "Your Invoice", :to => mail_ids, :body => message.html_part

这个

这些方法只会猜测并找到 text/plain 或 text/html 内容类型的多部分消息中的第一部分。 CloudMailin 还通过 params[:plain] 和 params[:html] 提供了这些便利方法。值得记住的是,消息永远不能保证包含纯文本或 html 部分。可能值得使用以下内容来确定:

plain_part = message.multipart? ? (message.text_part ? message.text_part.body.decoded : nil) : message.body.decoded
html_part = message.html_part ? message.html_part.body.decoded : nil

【讨论】:

  • 不,我使用了“customer.welcome_mailer(params, current_user, customer_params)”
  • 是的,我可以正常接收邮件,但文本/纯格式,我需要 html 格式
  • 只需将其粘贴到您的客户/邮件模型方法中 - 这将解决您的问题 content_type "text/html"
  • 得到这个错误捕获异常:NoMethodError: undefined method `html_part' for #<0xb3e10470>
猜你喜欢
  • 1970-01-01
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2011-08-11
  • 1970-01-01
相关资源
最近更新 更多