net/smtp

D

Dave Lilley

Okay I am officially stumped!!!

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


[ No Subject ] From (e-mail address removed)



To: (e-mail address removed)

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:
"(e-mail address removed)" <[email protected]>
...
View Contact
To:
To: (e-mail address removed)

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
 
B

Brian Candler

Dave said:
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:
To: nobody@localhost\n\n\n
Hello world"
=> "From: nobody@localhost\n\nTo: nobody@localhost\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("nobody@localhost","candlerb@localhost","Test
message", "hello world!")
 
B

Brian Candler

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"
 
D

Dave Lilley

Brian said:
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: "(e-mail address removed)" <[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.
 
B

Brian Candler

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)
 
D

Dave Lilley

Brian said:
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
 

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

Similar Threads

net/smtp 3
net/smtp Multiple Email recipients Issue 2
net/smtp question 6
Help with NET::SMTP 13
Net::SMTP errors 3
net/smtp question 0
SMTP SMS Reminder Script 5
Simple SMTP server in ruby 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top