How do I set up sending HTML emails using a mailbox?

I am sending letters using a mailbox. Here is 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 do not know how to configure the content type to, for example, format email as html. Although in fact I just want to define bold text, for example, my email client: bold text . Does anyone know what type of content I need to specify in order to achieve this and how to implement it with mail?

Just notice, the code above is great for sending text emails.

+4
source share
2 answers

From the documentation

Writing and sending emails with multiple / alternative (html and text)

Mail makes some basic assumptions and makes the usual thing as simple as possible .... (ask a lot from the 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 
+11
source

@Simone Carletti's answer is essentially correct, but I struggled with this and didn’t want part of the text to be written by email and a separate part of HTML. If you just want all the email to be HTML, something like this will work:

 mail = Mail.deliver do to ' nicolas@test.lindsaar.net.au ' from 'Mikel Lindsaar < mikel@test.lindsaar.net.au >' subject 'First email sent with Mail' content_type 'text/html; charset=UTF-8' body '<h1>This is HTML</h1>' end 

Perhaps I skipped this, I did not see anything in the Mail gem documentation describing how to do this, which I think will be more common than creating a multi-page message. The documentation seems to cover only text messages and multi-page messages.

+3
source

All Articles