# Sendmail, semicolons and new lines

**URL:** <https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924>\
**Category:** Ruby\
**Created:** [May 2, 2010, 8:58am UTC](https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924 "2010-05-02T08:58:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 2, 2010, 8:58am UTC](https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924/1 "2010-05-02T08:58:30Z")

</div>

I know the tile doesn’t sound very Ruby-related but please bear with me!  
In a Ruby script I make a system call to ‘sendmail’ to send text from a  
file I’ve created from a Ruby string (I use sendmail because I don’t  
have and cannot easily get ‘mail’ or other gems added to this machine).

All works fine until I try to to include semi-colons at the end of each  
lne of text, ie “…;\n”, at which point sendmail considers these as  
comments, ignores the “\n” newline cahracters, and everything gets  
printed on one line. Looking at sendmails manual this seems to be  
expeced behaviour but the odd thing is that if I manually write a test  
file, with ‘nano’, then the mail is sent, with line breaks as desired,  
no problem. I also tried a file created by another script (bash I  
think) and that was also OK. It is almost as if “\n” is not working in  
exactly the same way as a ‘return’ when writing a text file manually. I  
also tried “…;\r” instead of “…;\n”, with no effect.

Other wordings which had no effect were “…; \n” and "…;\n ".  
Strangley, “…;\n\n” _did_ work, but of course generated an extra blank  
line, which I don’t want!

Any help or advice appreciated.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 2, 2010, 10:41am UTC](https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924/2 "2010-05-02T10:41:40Z")

</div>

Toby R. wrote:

> I know the tile doesn’t sound very Ruby-related but please bear with me!  
> In a Ruby script I make a system call to ‘sendmail’ to send text from a  
> file I’ve created from a Ruby string (I use sendmail because I don’t  
> have and cannot easily get ‘mail’ or other gems added to this machine).
> 
> All works fine until I try to to include semi-colons at the end of each  
> lne of text, ie “…;\n”, at which point sendmail considers these as  
> comments, ignores the “\n” newline cahracters, and everything gets  
> printed on one line. Looking at sendmails manual this seems to be  
> expeced behaviour

This is complete nonsense. Sendmail doesn’t handle semicolons in mail  
bodies any differently from any other characters.

If you post your actual ruby code, we can tell you what’s wrong with it,  
but it’s certainly nothing to do with sendmail.

Probably the best way to do it is to start sendmail using IO.popen and  
then write the message body to the pipe.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 2, 2010, 11:40am UTC](https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924/3 "2010-05-02T11:40:49Z")

</div>

On 02.05.2010 08:58, Toby R. wrote:

> I know the tile doesn’t sound very Ruby-related but please bear with me!  
> In a Ruby script I make a system call to ‘sendmail’ to send text from a  
> file I’ve created from a Ruby string (I use sendmail because I don’t  
> have and cannot easily get ‘mail’ or other gems added to this machine).

There is net/smtp in the standard library.  
[http://ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html)

> All works fine until I try to to include semi-colons at the end of each  
> lne of text, ie “…;\n”, at which point sendmail considers these as  
> comments, ignores the “\n” newline cahracters, and everything gets  
> printed on one line.

What do you mean by “gets printed”? Last time I checked sendmail was an  
MTA, and MTA’s do not print emails.

> line, which I don’t want!
> 
> Any help or advice appreciated.

How exactly do you invoke sendmail (command line arguments) and how do  
you interact with it (writing to stdin, using IO redirection…)?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 2, 2010, 2:35pm UTC](https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924/4 "2010-05-02T14:35:19Z")

</div>

Toby R. wrote:

> I focused my attention on the file I’m  
> feeding into Sendmail, and whilst I don’t pretend to understand it, I  
> find if I add two newlines ("\n\n") before my list of lines ending in  
> semicolons then it works as desired.

Ah. An E-mail message consists of headers and a body, and they are  
separated by a blank line (see RFC 2822 for the full details)

So if you want to send an E-mail and don’t want to specify any headers,  
you need a blank line at the top. An E-mail like that is invalid, so  
sendmail will add a minimal set of headers for you anyway (e.g. To: is  
the recipient(s) you provided on sendmail command line, From: is the  
user who submitted the message)

It’s better to provide the headers explicitly, and if you use sendmail  
-t then the recipients are taken from the To:/Cc:/Bcc: headers that you  
provide, so you don’t need to provide them on the sendmail command line.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 2, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924/5 "2010-05-02T14:04:50Z")

</div>

Robert K. wrote:

> There is net/smtp in the standard library.  
> [http://ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/net/smtp/rdoc/index.html)

Ah, very helpful and very useful, many thanks Robert. By using

Net::SMTP.start(my\_mail\_server) { |smtp| smtp.send\_message  
IO.read(my\_dodgy\_file), from\_address, to\_address }

… I’m getting the same result as before confirming your and Brian’s  
position that this is not related to Sendmail (so I must have misread  
that ‘sendmail’ man page). I focused my attention on the file I’m  
feeding into Sendmail, and whilst I don’t pretend to understand it, I  
find if I add two newlines (“\n\n”) before my list of lines ending in  
semicolons then it works as desired.

Plus, I now know how to quickly and easily send e-mail from Ruby without  
relying on other programs - nice!

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 4, 2010, 11:57pm UTC](https://www.ruby-forum.com/t/sendmail-semicolons-and-new-lines/187924/6 "2010-05-04T23:57:49Z")

</div>

Many thanks for the explanation Brian - always nice to know ‘why’ as  
well as ‘how’.
