Html email through SMTP?

Anyone have a snippet or link to a snippet for setting up an email
with HTML formatting? Does it have to be a multipart MIME yada yada
with plain-text accompaniment, or can I skip that and just have html
in it and keep it simple?

···

--
Chris
http://clabs.org

If you want it to be an HTML only email, it's trivial.

I tend to use the tmail library because it's simple and works for me.

To use it, do something similar to:

require 'tmail'

mail = TMail::Mail.new
mail.to = to_address
mail.from = from_address
mail.reply_to = reply_address
mail.subject = 'My Email'
mail.date = Time.now
mail.set_content_type = 'text','html'
mail.body = <<EMAIL
<head><title>My Mail</title></head>
<body><h1>Here is my HTML mail!</h1></body>
EMAIL

sm = IO.popen("/usr/sbin/sendmail -i -t","w+")
sm.print mail.encoded
sm.flush

Kirk Haines

···

On Thu, 23 Sep 2004 05:09:02 +0900, Chris Morris wrote

Anyone have a snippet or link to a snippet for setting up an email
with HTML formatting? Does it have to be a multipart MIME yada yada
with plain-text accompaniment, or can I skip that and just have html
in it and keep it simple?

Chris Morris wrote:

Anyone have a snippet or link to a snippet for setting up an email
with HTML formatting? Does it have to be a multipart MIME yada yada
with plain-text accompaniment, or can I skip that and just have html
in it and keep it simple?

This may help

http://rubyforge.org/projects/mime-alt-lite/

James

You _can_ just put HTML in it, with Content-Type: text/html

However, it is polite (and strongly recommended) to send the mail as a
Multipart/Alternative, with the first part being a plaintext representation
of the mail, and the second part the HTML. That means that it can be read by
people whose mailers don't support HTML.

Regards,

Brian.

···

On Thu, Sep 23, 2004 at 05:09:02AM +0900, Chris Morris wrote:

Anyone have a snippet or link to a snippet for setting up an email
with HTML formatting? Does it have to be a multipart MIME yada yada
with plain-text accompaniment, or can I skip that and just have html
in it and keep it simple?

Once you have that TMail::Mail instance built, it's not much harder to
send via an SMTP server, either (which is good if you're on Windows,
where 'sendmail' usually isn't installed :wink:

An example:

···

--
# ...create TMail::Mail object called 'mail', then...

require 'net/smtp'
from_addr = 'me@example.com'
to_addr = 'you@example.com'
mail_server = 'example.com'

Net::SMTP.start(mail_server, 25) do |smtp|
smtp.send_msg(mail.encoded, from_addr, to_addr)
end
--

Lennon
rcoder.net