2

Pertinent code:

msg = "Subject: Reset password instructions\n\nHello " + @request_payload["email"] + "!\n\n" +
      "A new account has been created for you at <a href=\"presentation-layer.dev\">presentation-layer.dev<a>." + 
      "Please go to that page and click \"forgot password\" to set your password."
      smtp = Net::SMTP.new 'smtp.gmail.com', 587
      smtp.enable_starttls
      smtp.start('domain', "email", 'password', :login) do
        smtp.send_message(msg, 'sender', "recip")
      end

The resulting email just has the raw text in it. How do I get the server to evaluate the HTML tags?

bkaiser
  • 647
  • 8
  • 22
  • Servers (Mail Transfer Agents == MTAs) don't evaluate HTML tags. Mail user agents (MUAs) do when they're displaying the content to the user. – the Tin Man Aug 01 '13 at 03:38

2 Answers2

5

To do what you want, you should generate a MIME document. If you really want to do it right, create a multipart MIME document so you have both the TEXT and rich-text parts.

You can do it from Net::SMTP, but you have to add the necessary MIME header and part dividers to the document. See "Sending Email using Ruby - SMTP" for an example how.

It's easier to use the Mail gem, which supports both, especially if you're including multiple parts or adding attachments. From the documentation:

You can also create MIME emails. There are helper methods for making a multipart/alternate email for text/plain and text/html (the most common pair) and you can manually create any other type of MIME email.

And farther down in the document in "Writing and sending a multipart/alternative (html and text) email":

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Thanks for the help. How can I configure this code to use google smtp server rather than a server running locally? i notice it says you can send via sendmail - should i just do `Net::SMTP.start(server info here)`? – bkaiser Aug 01 '13 at 13:55
  • Have a look at the answer here http://stackoverflow.com/questions/12884711/how-to-send-email-via-smtp-with-rubys-mail-gem It gave me the code I needed to send the email from a gmail account – TimSmith-Aardwolf May 24 '16 at 15:44
0

Sounds like you need to set the 'Content-Type' header:

Content-Type: text/html;
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124