send email in ruby

V

Vetrivel Vetrivel

How to attach the filename in ruby. I have attach 1.txt as a file name .
The 1.txt
file has the following contents.
This is a test message.
But it doesn't work for me.

require 'net/smtp'

msgstr = <<END_OF_MESSAGE
From: FromAddress
To: ToAddress
Subject: test message
Message-Id: <[email protected]>
attachment: "1.txt"


END_OF_MESSAGE

smtp = Net::SMTP.start("IpAdd" , portno)
smtp.send_message msgstr, 'fromAddress, 'toAddress'
 
D

David Wright

Vetrivel said:
How to attach the filename in ruby. I have attach 1.txt as a file name .
The 1.txt
file has the following contents.
This is a test message.
But it doesn't work for me.

require 'net/smtp'

msgstr = <<END_OF_MESSAGE
From: FromAddress
To: ToAddress
Subject: test message
Message-Id: <[email protected]>
attachment: "1.txt"


END_OF_MESSAGE

smtp = Net::SMTP.start("IpAdd" , portno)
smtp.send_message msgstr, 'fromAddress, 'toAddress'

I haven't personally used this module but looking at the docs,
http://www.ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html I don't
see an attachment option.

You could however do this. (untested)

mess_body= File.readlines('1.txt')

msgstr = <<END_OF_MESSAGE
From: FromAddress
To: ToAddress
Subject: test message
Message-Id: <[email protected]>

#{mess_body}
END_OF_MESSAGE

(did I win that 10,000 pound UK lotto yet?)
 
D

Damjan Rems

Scott said:
I would suggest looking at the MailFactory gem (
http://rubyforge.org/projects/mailfactory/). It has a simple way to
construct the mail message, with attachments, that you can then send
using
net/smtp.

You can also try with Ruport:

r = Ruport::Report.new
r.add_mailer :default,
:host => "my.smtp.host",
:address => "my@adress"

r.send_to('send.to@mail') do |mail|
mail.subject = "Subject"
Dir["*.tif"].each { | cName | mail.attach(cName) }
mail.text = "Body text"
end

Works for me.

by
TheR
 
L

Loga Ganesan

Damjan said:
Scott said:
I would suggest looking at the MailFactory gem (
http://rubyforge.org/projects/mailfactory/). It has a simple way to
construct the mail message, with attachments, that you can then send
using
net/smtp.

You can also try with Ruport:

r = Ruport::Report.new
r.add_mailer :default,
:host => "my.smtp.host",
:address => "my@adress"

r.send_to('send.to@mail') do |mail|
mail.subject = "Subject"
Dir["*.tif"].each { | cName | mail.attach(cName) }
mail.text = "Body text"
end

Works for me.

by
TheR

Hi
In the above code, what are the libraries I need to use in require.

It says me the following error:
uninitialized constant Ruport::Report (NameError)

I have installed ruport.
 
L

Loga Ganesan

Hi
In the above code, what are the libraries I need to use in require.

It says me the following error:
uninitialized constant Ruport::Report (NameError)

I have installed ruport.


Awaiting reply................
 
M

Mohit Sindhwani

Loga said:
Hi
Expecting Reply .........
Obviously, this is not working out for you. I haven't seen your
original code but it seems like your need to require ruport.

Cheers,
Mohit.
3/25/2009 | 2:45 PM.
 
L

Loga Ganesan

Mohit said:
Obviously, this is not working out for you. I haven't seen your
original code but it seems like your need to require ruport.

Cheers,
Mohit.
3/25/2009 | 2:45 PM.


This is the code that I worked out:

#! /usr/bin/ruby

require 'ruport'

r = Ruport::Report.new
r.add_mailer :default,
:host => "my.smtp.host",
:address => "my@adress"

r.send_to('send.to@mail') do |mail|
mail.subject = "Subject"
Dir["*.tif"].each { | cName | mail.attach(cName) }
mail.text = "Body text"
end
 
D

Damjan Rems

Loga said:
Mohit said:
Obviously, this is not working out for you. I haven't seen your
original code but it seems like your need to require ruport.

Cheers,
Mohit.
3/25/2009 | 2:45 PM.


This is the code that I worked out:

#! /usr/bin/ruby

require 'ruport'

r = Ruport::Report.new
r.add_mailer :default,
:host => "my.smtp.host",
:address => "my@adress"

r.send_to('send.to@mail') do |mail|
mail.subject = "Subject"
Dir["*.tif"].each { | cName | mail.attach(cName) }
mail.text = "Body text"
end

Yep. Thats it.

Sorry 4 not beeing online.

by
TheR
 
L

Loga Ganesan

Ya, the above code says the following error:

uninitialized constant Ruport::Report (NameError)

I think the module Report was not included. But though I used 'include
Ruport::Report' , it says the error
 
P

Peña, Botp

RnJvbTogTG9nYSBHYW5lc2FuIFttYWlsdG86bG9nYW5hdGhhbl9ncHRAeWFob28uY28uaW5dIA0K
IyBZYSwgdGhlIGFib3ZlIGNvZGUgc2F5cyB0aGUgZm9sbG93aW5nIGVycm9yOg0KIyAgdW5pbml0
aWFsaXplZCBjb25zdGFudCBSdXBvcnQ6OlJlcG9ydCAoTmFtZUVycm9yKQ0KIyBJIHRoaW5rIHRo
ZSBtb2R1bGUgUmVwb3J0IHdhcyBub3QgaW5jbHVkZWQuIEJ1dCB0aG91Z2ggSSB1c2VkICdpbmNs
dWRlDQojIFJ1cG9ydDo6UmVwb3J0JyAsIGl0IHNheXMgdGhlIGVycm9yDQoNCmdlbSBpbnN0YWxs
IHJ1cG9ydC11dGlscw0KDQp0aGVuIG9uIHlvdXIgY29kZS4uLA0KDQpyZXF1aXJlICdydXBvcnQv
dXRpbCcNCg==
 

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

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top