Net/smtp

Okay I am officially stumped!!!

output from my current code looks like this (from my client inbox, yahoo
and gmail.

[ No Subject ] From [email protected]

To: [email protected]

Subject: test

Date: Wed Aug 04 22:37:47 +1200 2010

Message-Id: Wed Aug 04 22:37:47 +1200 2010

This is a test   <<< Just want to see this

I want it to look as if the email was sent by a client email program
(note i use Linux for development but don’t know if this will be the end
OS, I’d like it to be).

code I currently use is (and if I change it to a class I get NO
CHANGE!!) …

require ‘net/smtp’

module Sendemail

def send_email(from, to, subject, message) # edited

msg = "From: #{from}\n
To: #{to}\n
Subject: #{subject}\n
Date: #{Time.now}\n
Message-Id: #{Time.now}\n\n\n

#{message}"


Net::SMTP.start('smtp.isp.com',25) do |smtp|
  smtp.send_message(msg, from, to)
end

end
end

And if i use the parameter message instead of msg I still don’t get teh
subject to appear, so this fixes 1 problem but not the other.

Okay USE PONY well this is the output - NO DIFFERENCE.

[ No Subject ]

From:
[email protected][email protected]

View Contact
To:
To: [email protected]

Subject: test

Date: Wed Aug 04 22:52:25 +1200 2010

Message-Id: Wed Aug 04 22:52:25 +1200 2010


  This is a test

this is the code used for pony as you can see not a lot of difference to
my hand crafted code (but I did hope).

require ‘pony’

module Sendemail
def send_email(from, to, subject, msg)
Pony.mail(:to => “#{to}”, :from => “#{from}”, :subject =>
“#{subject}”, :body => “#{msg}”, :via => :smtp, :via_options => {
:address => ‘smtp.isp.com’,
:port => ‘25’})
end
end

Now I’ve tried using authentication and password but this changes
nothing!!!

changing both bits of code to a class does nothing! using esmtp does
nothing too.

you’re help would be apprec

taking the NET::SMTP piece of code into my calling program changes
nothing!!!.

how can I simply get to appear what you would see if you used you’re
client email program? namely from

Dave L. wrote:

Okay I am officially stumped!!!

It’s because of the double newlines: you are adding \n as well as having
the newline because the string is broken onto another line.

You can see this in irb:

msg = “From: [email protected]\n
To: [email protected]\n\n\n
Hello world”
=> “From: [email protected]\n\nTo: [email protected]\n\n\n\nHello world”

Personally I’d use a heredoc:

require ‘net/smtp’

module Sendemail

def send_email(from, to, subject, message) # edited

msg = <<EOM

From: #{from}
To: #{to}
Subject: #{subject}
Date: #{Time.now}

#{message}
EOM
Net::SMTP.start(‘127.0.0.1’,25) do |smtp|
smtp.send_message(msg, from, to)
end

end
module_function :send_email

end

Sendemail.send_email(“[email protected]”,“[email protected]”,“Test
message”, “hello world!”)

Also note that #{Time.now} is not an RFC2822-compliant string. Try this:

irb(main):001:0> require ‘time’
=> true
irb(main):002:0> Time.now.rfc2822
=> “Wed, 04 Aug 2010 12:40:02 +0100”

You must not put spaces before the Subject: header. Align your text with
the left-hand column.

Using a heredoc with minus sign, you are allowed to indent the
terminating symbol:

msg = <<-EOS
Subject: foo
etc
EOS

But you still need to align the text itself to the left-hand edge. If
you don’t think that’s pretty, then you could use gsub to remove the
leading spaces.

(Incidentally, you said you were using a heredoc, but the code you
posted wasn’t)

Brian C. wrote:

Also note that #{Time.now} is not an RFC2822-compliant string. Try this:

irb(main):001:0> require ‘time’
=> true
irb(main):002:0> Time.now.rfc2822
=> “Wed, 04 Aug 2010 12:40:02 +0100”

many thanks for you’re assistance Brian,

I’ve removed the ‘\n’ from the code and the date stuff so I am 1 step
closer to what I want.

BUT I don’t have any subject line!!

it’s still this…

[ No Subject ]

From: “[email protected][email protected]

View Contact
To:

  test message...

I only put the date stuff in from looking at my original program.

Any further advice? module is now this (using heredoc now too).

require ‘net/smtp’

module Sendemail # edited

def send_email(from, to, subject, message) # edited

msg = "From: #{from}
To: #{to}
Subject: #{subject}

#{message}"


Net::SMTP.start('smtp.isp.com',25) do |smtp|
  smtp.send_message(msg, from, to)
end

end
end

Will keep on trying other things and googling.

Dave L. wrote:

got any pointers on adding in attachments

Not really - probably worth looking at gems like mail or tmail.

Brian C. wrote:

You must not put spaces before the Subject: header. Align your text with
the left-hand column.

Using a heredoc with minus sign, you are allowed to indent the
terminating symbol:

msg = <<-EOS
Subject: foo
etc
EOS

But you still need to align the text itself to the left-hand edge. If
you don’t think that’s pretty, then you could use gsub to remove the
leading spaces.

(Incidentally, you said you were using a heredoc, but the code you
posted wasn’t)

You are a brilliant!!!

I’ve spent so much time on this (and another problem) now I’ve got both
sorted.
I was going to use pony and I may just do so as I can see my next task
with this will be to put up attachments (like PDFS) to these out going
emails.

In the past I’d tried to use heredocs and got errors along the lines of
no end marker found and i’d done this

text = <<TO_END
some text put here
TO_END
or was it like this…

 text = << TO_END
   some text here
   nicely indented
 TO_END

still many many thanks.

got any pointers on adding in attachments - being cheeky.

better still can you point to a place with very good samples?
I’m not a great coder by any test.

rgds,

Dave.
only to have

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs