如何设置发送带有邮件gem的HTML电子邮件? [英] How to setup send HTML email with mail gem?

查看:204
本文介绍了如何设置发送带有邮件gem的HTML电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mail gem发送电子邮件。这是我的代码:

I am sending email using the Mail gem. Here's my code:

require 'mail'
require 'net/smtp'

Mail.defaults do

delivery_method :smtp, { :address              => "smtp.arrakis.es",
                       :port                 => 587,
                       :domain               => 'webmail.arrakis.com',
                       :user_name            => 'myname@domain.com',
                       :password             => 'pass',
                       :authentication       => 'plain',
                       :enable_starttls_auto => true  }

end

Mail::ContentTypeField.new("text/html") #this doesnt work

msgstr= File.read('text2.txt')

list.each do |entity|
    begin
        Mail.deliver do
            from    'myname@domain.com'
            to      "#{entity}"
            subject 'a good subject'
            body   msgstr
        end
    rescue => e
    end

end
end

I不知道如何设置内容类型,以便例如将电子邮件格式设置为html。虽然我实际上只是希望能够像电子邮件客户端一样定义粗体文本:粗体。有人知道我需要指定哪种内容类型才能实现此目的,以及如何通过邮件实现它吗?

I don't know how to set up the content type, so that I can format my email as html for example. Though I actually just wish to be able to define bold text like my email client does: bold text. Does anybody know which content-type I need to specify in order to achieve this, and how to implement it with mail?

仅需注意,上面的代码适用于发送纯文本电子邮件。

Just a note, the code above works fine for sending plain text email.

推荐答案

From the documentation


写作和发送多部分/替代(html和文本)电子邮件

邮件做出了一些基本假设,并使做为
的普通事情变得尽可能简单。 ...(从邮件库中索要很多东西)

Mail makes some basic assumptions and makes doing the common thing as simple as possible.... (asking a lot from a mail library)

mail = Mail.deliver do
  to      'nicolas@test.lindsaar.net.au'
  from    'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
  subject 'First multipart email sent with Mail'

  text_part do
    body 'This is plain text'
  end

  html_part do
    content_type 'text/html; charset=UTF-8'
    body '<h1>This is HTML</h1>'
  end
end


这篇关于如何设置发送带有邮件gem的HTML电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆