LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to specify recipients in a file and send mail using "mail -t" option (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-specify-recipients-in-a-file-and-send-mail-using-mail-t-option-4175562780/)

mkolici 12-31-2015 04:04 AM

How to specify recipients in a file and send mail using "mail -t" option
 
Hi,
I want to specify the recipients and the subject to a file and mail program can read this info from this file.
From man pages I read that "-t" option is used:

"The message to be sent is expected to contain a message header with 'To:', 'Cc:', or 'Bcc:' fields giving its recipients.
Recipients specified on the command line are ignored."

I specify the recipients in the file as below:

[root@suitecrm ~]# cat example

From: me@myhost.com
To: user@example.com

Hi, this is my message, and I'm sending it to you!
.
[root@suitecrm ~]#


and I give the command:
[root@suitecrm ~]# mail -s test -q example -t <<<EOF

but it does not read the recipients:

[root@suitecrm ~]# mail -s test -q example -t <<<EOF
No recipients specified
"/root/dead.letter" 14/286
[root@suitecrm ~]#

Please help me how to configure the recipients in the file.

Thank you

jpollard 12-31-2015 11:17 AM

What works is giving it a body.

I think the error is coming from an EOF occurring too soon in the data stream, causing mail to not parse the data. It works if you give a blank line before the EOF.

try using:
Code:

echo "" | mail -s test -q example -t
This adds a blank line to the data (which appears to get stripped off in parsing). You can also do this in a script with a here stream following.

PS: it took several tests to figure out what the issue was. Mail is handled in stanzas - the addressing is a block, then the body is a sequence of blocks... What I THINK is being misinterpreted is that the stanza gets separated by a blank line. Once the file from the -q is loaded it doesn't interpret the EOF of that file as the end of the stanza containing the body - and an immediate EOF causes a parsing failure. This does allow for things like standard headers and body, then appending a custom body to the message (as in a here document, or adding a report file as the rest of the body through redirecting stdin).

mkolici 01-04-2016 04:24 AM

Hi jpollard,
It works perfectly. Thank you for your understandable explanation.

Regards
Mikel


All times are GMT -5. The time now is 06:43 PM.