Professional Web Applications Themes

Best way to send mail in ruby - Ruby

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

  1. #1

    Default Best way to send mail in ruby

    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 Guest

  2. #2

    Default Re: Best way to send mail in ruby

    Bauduin Raphael (com) wrote:
    [...] 

    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 Guest

  3. #3

    Default 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: 
    >
    >
    > 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[/ref]
    Bauduin Guest

  4. #4

    Default Re: Best way to send mail in ruby

     

    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 Guest

  5. #5

    Default Re: Best way to send mail in ruby

    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"] = "ge.com"

    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:
     





    Bermejo, Guest

Similar Threads

  1. How to send an E-mail with DIR MX & PHP
    By Marco Antonio León Dávila in forum Macromedia Director Lingo
    Replies: 1
    Last Post: November 27th, 02:40 PM
  2. Replies: 1
    Last Post: July 31st, 08:51 PM
  3. Can't send/receive Mail (Apple Mail)
    By Shin in forum Mac Applications & Software
    Replies: 1
    Last Post: July 22nd, 08:03 PM
  4. Receiving mail but not able to send mail!
    By Govardhanan in forum Windows Networking
    Replies: 0
    Last Post: July 22nd, 01:39 PM
  5. Send Same E-mail To Multiple E-mail Addresses?
    By Thorsten Matzner in forum Windows XP/2000/ME
    Replies: 3
    Last Post: July 6th, 01:02 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139