Ruby, Ruby on Rails

Tuesday, December 11, 2007

Sending E-mail with Ruby Net::SMTP

All you need to know is here:
http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/classes/Net/SMTP.html

def send_email(from, from_alias, to, to_alias, subject, message)

msg = <<EOF
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}

#{message}
EOF

Net::SMTP.start('localhost') do |smtp|
smtp.send_message(msg, from, to)
end

end

Or if your mail server is on another machine:

Net::SMTP.start(mail_server, port, helo_domain) do |smtp|
smtp.send_message msg, from, to
end

For example:

smtp.send_message('your.smtp.server', 25, 'mail.from.domain')

1 comment:

jonty said...

Thanks for this - the docs are not entirely clear on what goes where - this is the third time i've tried to learn this!!

About Me

My photo
Developer (Ruby on Rails, iOS), musician/composer, Buddhist, HSP, Vegan, Aspie.