Best way to send mail in ruby

Discussion in 'Ruby' started by Bauduin Raphael, Jan 3, 2004.

  1. 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
     
    Bauduin Raphael, Jan 3, 2004
    #1
    1. Advertising

  2. Bauduin Raphael () 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
     
    Reimer Behrends, Jan 3, 2004
    #2
    1. Advertising

  3. classes repository? (was : Best way to send mail in ruby)

    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 () 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
     
    Bauduin Raphael, Jan 3, 2004
    #3

  4. >
    > 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
     
    culley harrelson, Jan 4, 2004
    #4
  5. 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"] = ""

    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
     
    Bermejo, Rodrigo, Jan 4, 2004
    #5
  6. Bauduin Raphael

    martinus Guest

    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
     
    martinus, Dec 22, 2004
    #6
  7. 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.
     
    trans. (T. Onoma), Dec 22, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    3
    Views:
    1,960
    Larry Lard
    Nov 23, 2005
  2. =?Utf-8?B?Unlhbg==?=
    Replies:
    2
    Views:
    3,925
    Steven Cheng[MSFT]
    Feb 28, 2006
  3. refv8
    Replies:
    2
    Views:
    27,120
    ittower
    Oct 13, 2006
  4. Mike P
    Replies:
    1
    Views:
    579
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 1, 2007
  5. Ramon
    Replies:
    2
    Views:
    1,031
    Ramon
    Oct 25, 2007
Loading...

Share This Page