前回の質問
Rubyのmailを使ってファイルの添付をすることができました。
使ったコード
ruby
1require "net/smtp" 2require "mail" 3 4mail = Mail.new do 5 from "Your Name <from@example.com>" 6 to "Dest Address <to@example.net>" 7 subject "test mail" 8 body "This is a test mail." 9 10 add_file(filename: "bbb.png", content: File.binread('./bbb.png')) 11 add_file(filename: "sample.rb", content: File.binread('./sample.rb')) 12end 13 14 15a = Net::SMTP.new("smtp.sendgrid.net", 587) 16a.enable_starttls 17a.start('localhost') do |smtp| 18 smtp.authenticate("id","password") 19 smtp.send_message(mail.to_s, "送信元", "送信先") 20end 21
bbb.png sample.rbの2つのファイルを添付させています。以下送られてきたファイル
(手動でやらないと画像を開けない・・・)
ただ、自分が想像していたのは
上記のような感じのものです。
調べていくとHTMLメールというみたいですが
Ruby の mailでHTMLメール作成は可能なのでしょうか?
1Ruby mail HTMLメール
のようなワードで調べているのですが
出てくるのがRailsや関係ないものばかりで。。。
RubyのmailでHTMLメールを送ることはできないのでしょうか?
わからないので参考になるリンクまたは説明をお願いします。
require "net/smtp"
require "mail"
追記
回答でいただいた方法でHTMLメールを作成できました。
ruby
1 2mail = Mail.new do 3 to 'nicolas@test.lindsaar.net.au' 4 from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>' 5 subject 'First multipart email sent with Mail' 6 html_part do 7 content_type 'text/html; charset=UTF-8' 8 body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p> 9 <a href="teratail.com">This is sample links</a> 10 <img src="https://s.yimg.jp/images/ks/official/nt/grd/prf_48.png"> 11 ' 12 end 13end 14 15a = Net::SMTP.new("smtp.sendgrid.net", 587) 16a.enable_starttls 17a.start('localhost') do |smtp| 18 smtp.authenticate("id","password") 19 smtp.send_message(mail.to_s, "送信元", "送信先") 20end
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/16 12:18
2019/08/16 12:20
2019/08/16 12:31
2019/08/16 13:38
2019/08/17 03:09
2019/08/17 07:58