# Net/smtp

**URL:** <https://www.ruby-forum.com/t/net-smtp/192714>\
**Category:** Ruby\
**Created:** [August 4, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/net-smtp/192714 "2010-08-04T13:10:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [August 4, 2010, 1:10pm UTC](https://www.ruby-forum.com/t/net-smtp/192714/1 "2010-08-04T13:10:24Z")

</div>

Okay I am officially stumped!!!

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

[No Subject] From removed\_email\_address@domain.invalid

```
To: removed_email_address@domain.invalid

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:  
“removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
…  
View Contact  
To:  
To: removed\_email\_address@domain.invalid

```
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](http://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

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [August 4, 2010, 1:37pm UTC](https://www.ruby-forum.com/t/net-smtp/192714/2 "2010-08-04T13:37:54Z")

</div>

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: nobody@localhost\n  
> > 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!”)

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [August 4, 2010, 1:40pm UTC](https://www.ruby-forum.com/t/net-smtp/192714/3 "2010-08-04T13:40:25Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [August 5, 2010, 10:01am UTC](https://www.ruby-forum.com/t/net-smtp/192714/4 "2010-08-05T10:01:53Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [August 5, 2010, 8:57am UTC](https://www.ruby-forum.com/t/net-smtp/192714/5 "2010-08-05T08:57:53Z")

</div>

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: “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
…  
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.

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [August 5, 2010, 1:34pm UTC](https://www.ruby-forum.com/t/net-smtp/192714/6 "2010-08-05T13:34:58Z")

</div>

Dave L. wrote:

> got any pointers on adding in attachments

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

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [August 5, 2010, 11:09am UTC](https://www.ruby-forum.com/t/net-smtp/192714/7 "2010-08-05T11:09:01Z")

</div>

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
