Send email in ruby


#1

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: removed_email_address@domain.invalid
attachment: “1.txt”

END_OF_MESSAGE

smtp = Net::SMTP.start(“IpAdd” , portno)
smtp.send_message msgstr, 'fromAddress, ‘toAddress’


#2

Vetrivel V. wrote:

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: removed_email_address@domain.invalid
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: removed_email_address@domain.invalid

#{mess_body}
END_OF_MESSAGE

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


#3

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.


#4

Damjan R. wrote:

Scott Lillibridge wrote:

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.


#5

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…


#6

Loga G. wrote:

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…

Awaiting reply…


#7

Scott Lillibridge wrote:

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


#8

Loga G. wrote:

Loga G. wrote:

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…

Awaiting reply…

Hi
Expecting Reply …


#9

Loga G. wrote:

I have installed ruport.

Awaiting reply…

Awaiting reply…

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.


#10

Mohit S. wrote:

Loga G. wrote:

I have installed ruport.

Awaiting reply…

Awaiting reply…

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.

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


#11

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


#12

Loga G. wrote:

Mohit S. wrote:

Loga G. wrote:

I have installed ruport.

Awaiting reply…

Awaiting reply…

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.

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


#13

From: Loga G. [mailto:removed_email_address@domain.invalid]

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

gem install ruport-utils

then on your code…,

require ‘ruport/util’