Best way to send mail in ruby

  • Thread starter Bauduin Raphael
  • Start date
B

Bauduin Raphael

Hi,

I have to send a mail from a Ruby script, and wonder what's the best way
to do it.

I saw net/smpt, but I need to write the headers myself (eg if I want to
define a subject I have to write Subject: blah\n )
I saw Rubymail, which anable me to easily construct a mail with headers,
but it doesnt send it (same for Tmail I think).

So, do I have to combine both to send a mail? Isn't there simply
something like

Mail::send_mail(from, to, subject, body) ?

Thanks for your help and advices.

Raph
 
R

Reimer Behrends

Bauduin Raphael ([email protected]) wrote:
[...]
So, do I have to combine both to send a mail? Isn't there simply
something like

Mail::send_mail(from, to, subject, body) ?

Assuming that you have sendmail/exim/postfix or something else with
sendmail-compatible options on your system (which is pretty much
any UNIX MTA), the script below should suffice.

The -oi option prevents a line with a single dot to be interpreted
as the end of the message; the -t option tells the MTA to extract
the To address from the headers; the -oem message tells the mailer
to deliver an error message to the sender if the command fails;
and -odb delivers the message in the background rather than
suspending the calling process.

You may have to change the path to /usr/sbin/sendmail on some
systems.

Reimer Behrends


SENDMAIL = "/usr/lib/sendmail -oi -t -oem -odb"

def sendmail(from, to, subject, body)
File.popen(SENDMAIL, "w") do
| pipe |
pipe.puts "From: #{from}"
pipe.puts "To: #{to}"
pipe.puts "Subject: #{subject}"
pipe.puts
pipe.puts body
end
end
 
B

Bauduin Raphael

Thanks :)

I wonder if there's a central place (liek a ruby classes repository)
where one could look for such things, rather than everybody rewriting
all this for their own need ( I thought about writing similar code,
but thought it was already existing and prefered to use a
well tested solution)

Or am I wrong in thinking that lots of those little code snippets
have been written times and times again by developers for their own
needs?

Wouldn't such a classes repository be helpfull?


Raph

PS: I'm still new to Ruby, and may have a wrong impression... Just
trying to be constructive ;-)


Reimer said:
Bauduin Raphael ([email protected]) wrote:
[...]
So, do I have to combine both to send a mail? Isn't there simply
something like

Mail::send_mail(from, to, subject, body) ?


Assuming that you have sendmail/exim/postfix or something else with
sendmail-compatible options on your system (which is pretty much
any UNIX MTA), the script below should suffice.

The -oi option prevents a line with a single dot to be interpreted
as the end of the message; the -t option tells the MTA to extract
the To address from the headers; the -oem message tells the mailer
to deliver an error message to the sender if the command fails;
and -odb delivers the message in the background rather than
suspending the calling process.

You may have to change the path to /usr/sbin/sendmail on some
systems.

Reimer Behrends


SENDMAIL = "/usr/lib/sendmail -oi -t -oem -odb"

def sendmail(from, to, subject, body)
File.popen(SENDMAIL, "w") do
| pipe |
pipe.puts "From: #{from}"
pipe.puts "To: #{to}"
pipe.puts "Subject: #{subject}"
pipe.puts
pipe.puts body
end
end
 
C

culley harrelson

So, do I have to combine both to send a mail? Isn't there simply
something like

Mail::send_mail(from, to, subject, body) ?

def mail(body, headers)

require 'rmail'
require 'net/smtp'

mail = RMail::Message.new()

headers.each do |key, value|
mail.header.set(key, value)
end

mail.body = body

smtp_server = Net::SMTP.new('localhost')
smtp_server.start(['localhost']) do |smtp|
smtp.sendmail(mail.to_s, headers['From'], headers['To'])
end

end
 
B

Bermejo, Rodrigo

If you have a SMTP server avaliable you can use the script below:

require 'net/smtp'

class SendMail < Net::SMTP



def initialize(options)

super

@user = options["user"]

@from = options["from"]

@to = options["to"].to_a

@pass = options["pass"]

@server = options["server"]

@subject = options["subject"]

end

def body=(mail_body)



# BUILD HEADERS



@body = "From: #{@from} <#{@from}>\n"

@body << "To: #{@to}<#{@to}>\n"

@body << "Subject: #{@subject}\n"

@body << "Date: #{Time.now}\n"

@body << "Importance:high\n"

@body << "MIME-Version:1.0\n"

@body << "\n\n\n"



# MESSAGE BODY



@body << mail_body

end

def send

@to.each do | to |

Net::SMTP.start(@server, 25 , @from , @user , @pass , :login) do |smtp|

smtp.send_message(@body,@from,to)

end

end



end



end

if __FILE__ == $0

print %^USAGE:

o=Hash.new

o["user"] = "userid"

o["from"] = "(e-mail address removed)"

o["pass"] = "neverguess"

o["server"] = "smtp server"

o["subject"] = "TEST MESSAGE"

mail=SendMail.new(o)

mail.body="Hi buddy"

mail.send

^

end
 
M

martinus

Is there a possibility to send a mail to a server that allows only
secure TSL connections? For example, I would like to send mails to a
gmail account, but smtp.gmail.com requires a SETTSL command.

Martin
 
T

trans. (T. Onoma)

On Wednesday 22 December 2004 02:27 pm, martinus wrote:
| Is there a possibility to send a mail to a server that allows only
| secure TSL connections? For example, I would like to send mails to a
| gmail account, but smtp.gmail.com requires a SETTSL command.

Sounds like it would be a nice addition to Net::SMTP.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top