Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Best way to send mail in ruby

Reply
Thread Tools

Best way to send mail in ruby

 
 
Bauduin Raphael
Guest
Posts: n/a
 
      01-03-2004
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
 
Reply With Quote
 
 
 
 
Reimer Behrends
Guest
Posts: n/a
 
      01-03-2004
Bauduin Raphael ((E-Mail Removed)) 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
 
Reply With Quote
 
 
 
 
Bauduin Raphael
Guest
Posts: n/a
 
      01-03-2004
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 Behrends wrote:
> Bauduin Raphael ((E-Mail Removed)) 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

 
Reply With Quote
 
culley harrelson
Guest
Posts: n/a
 
      01-04-2004

>
> 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




 
Reply With Quote
 
Bermejo, Rodrigo
Guest
Posts: n/a
 
      01-04-2004
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 Removed)"

o["pass"] = "neverguess"

o["server"] = "smtp server"

o["subject"] = "TEST MESSAGE"

mail=SendMail.new(o)

mail.body="Hi buddy"

mail.send

^

end



Bauduin Raphael wrote:

> 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






 
Reply With Quote
 
martinus
Guest
Posts: n/a
 
      12-22-2004
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

 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      12-22-2004
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clean way to send HTML e-mail with image using default mail client? Ramon Java 2 10-25-2007 02:02 AM
Syntax for System.Net.Mail send mail Mike P ASP .Net 1 03-01-2007 06:58 PM
Send a mail with System.Net.Mail.SmtpClient refv8 ASP .Net 2 10-13-2006 07:28 PM
Send mail using System.Web.Mail fails w/ m.BodyFormat=MailFormat.H =?Utf-8?B?Unlhbg==?= ASP .Net 2 02-28-2006 01:50 AM
Mail.SmtpMail.Send won't send to one of my addresses Nathan Sokalski ASP .Net 3 11-23-2005 09:25 PM



Advertisments