ruby-on-rails - 如何使用 ActionMailer 发送 html 电子邮件?

标签 ruby-on-rails ruby actionmailer

使用 rails 2.3.14

我的电子邮箱: 数据.html.erb:

<% @data.each do |error| %>
    <table>
        <% error.each_pair do |k, v| %>
            <tr>
                <td>
                    <%= k %>
                </td>
                <td>
                    <%= ap v %>
                </td>
            </tr>   
        <% end %>
    </table>
    <br />
<% end %>

我可以收到电子邮件,但当我收到时,它显示为纯文本,而不是呈现 HTML。

Action Mailer 配置:

config.action_mailer.raise_delivery_errors = true

# Send emails during development
config.action_mailer.perform_deliveries = true
ActionMailer::Base.delivery_method = :sendmail

更新:必须在我的交付方式中指定内容类型:

 def data(data, sent_at = Time.now)
    Time.zone = 'Eastern Time (US & Canada)'

    content_type 'text/html'
    subject     "[#{RAILS_ENV}] An error has occurred - #{Time.now.to_s("%B %d, %Y")}"
    recipients  "bugs@mydomain.com"
    from        AppConfig['from_email']
    sent_on     sent_at
    @body   =   {:data => data}
  end

最佳答案

来自 d11wtq 的 comment :

content_type 指定为 text/html:

ActionMailer::Base.mail(
  content_type: "text/html",
  from: "sender@example.com",
  to: "your_email@example.com",
  subject: "This is a test",
  body: "<h2>HTML Email Test</h2><p>This is a <b>test</b>.</p>"
).deliver_now

关于ruby-on-rails - 如何使用 ActionMailer 发送 html 电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10299503/

相关文章:

ruby-on-rails - Phusion 乘客无法以 root 身份运行 - 警告 : potential privilege escalation vulnerability

ruby-on-rails - 如果引发异常,rails 是否会缓存任何内容?

ruby-on-rails - ArgumentError : An SMTP To address is required to send a message. 设置消息 smtp_envelope_to、to、cc 或 bcc 地址

sql - 表格之间的 Ruby 调节

发送电子邮件时 jquery 设置边距不起作用

ruby-on-rails - Heroku/devise - 缺少要链接的主机!请提供:host parameter or set default_url_options[:host]

ruby-on-rails - 如何在 Rails 中指定与自定义主键的多态关联

ruby-on-rails - Rails 3.1,jQuery : Append data to a form before sending it out

ruby - 如果数组包含其他数组,如何检查 Ruby?

ruby-on-rails - Rails 4 - 带有命名空间的 STI