You are not logged in.

#1 2014-03-18 10:56:42

archery1234
Member
Registered: 2014-03-18
Posts: 9

sending a batch of mails using mail command

Hello everyone,

I am looking for a hint on how to do the following:

I have a bunch of prepared mails (~300). They're one file each, including header and body. What I need to do now seems relatively simple - I want to send them out. I have been advised to use

mail

to do so, but I can't seem to figure out how to do this. I studied the manpage looking for a way to give mail a file as a argument and found

-q file     Start the message with the contents of the specified file.  May be given in send mode only.

However, it returns

mail -q myfile
Send options without primary recipient specified.

So apparently, the contents of the file are really just used for the mail body, not header and body.

Is there any way to make mail send my file "as is"?

Thank you in adavance!

Offline

#2 2014-03-18 11:17:17

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: sending a batch of mails using mail command

Should be easy enough with a bit of scripting, can you post an example file so we can see the exact format please.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2014-03-18 11:26:00

archery1234
Member
Registered: 2014-03-18
Posts: 9

Re: sending a batch of mails using mail command

Yes, of course. The mails look like this:

Content-Type: multipart/encrypted;
 protocol="application/pgp-encrypted";
 boundary="----------=_1395064062-6308-209"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
X-Mailer: MIME-tools 5.503 (Entity 5.503)
Subject: $SUBJECT
To: $RECEIVER
From: $SENDER
Sender: $SENDER
User-Agent: $MYUSERAGENT

This is a multi-part message in MIME format...

------------=_1395064062-6308-209
Content-Type: application/pgp-encrypted; name="myname.msg"
Content-Disposition: attachment; filename="myname.msg"
Content-Transfer-Encoding: 7bit

Version: 1

------------=_1395064062-6308-209
Content-Type: application/octet-stream; name="msg.asc"
Content-Disposition: inline; filename="msg.asc"
Content-Transfer-Encoding: 7bit

-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.12 (GNU/Linux)

-- crypted content --

-----END PGP MESSAGE-----

------------=_1395064062-6308-209--

Offline

#4 2014-03-18 12:01:01

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: sending a batch of mails using mail command

archery1234 wrote:

I studied the manpage looking for a way to give mail a file as a argument and found

-q file     Start the message with the contents of the specified file.  May be given in send mode only.

That does say "start ... with" so you probably still end up in an editor. It might work better to use input redirection to feed the file to mail.

So apparently, the contents of the file are really just used for the mail body, not header and body.

The file is used as the message text (for the DATA command in SMTP), which includes both header and body. It does not parse this message text to extract a recipient (which, in SMTP, is given separately with the RCPT command). You have to pass the recipient as an argument to mail. According to the man page, it accepts comma-separated lists and it can handle full specifications including both names and addresses, so you can probably extract $RECEIVER in your script and pass this to mail without any further processing (and similarly for $SENDER and $SUBJECT, unless these are the same for all messages).

Or perhaps somebody can suggest a way to make mail or a related tool perform this extraction for you.

Offline

#5 2014-03-19 11:50:50

Painless
Member
Registered: 2006-02-06
Posts: 237

Re: sending a batch of mails using mail command

I think sendmail would be better for pre-generated emails like this, assuming you have it installed.  You would still have to extract $RECEIVER, but you could then (for each file) cat each file and pipe it to sendmail, e.g.

cat mailfile | sendmail "${RECEIVER}"

Otherwise (if you have to use mail/mailx) then as Raynman says, you'll have to extract $SENDER and $SUBJECT as well, but I'm not sure how the sent email will look with extra To/From/Etc fields.

Offline

#6 2014-03-19 15:12:33

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: sending a batch of mails using mail command

Maybe I was also partly thinking of sendmail. I'm actually not that familiar with the mail command and sendmail is probably better for this job, because you don't want mail to make changes to the contents of the file.

But both tools have a -t option (which I overlooked in mail's man page at first) to read the recipient from the headers in the mail, so try

sendmail -t < mailfile

Offline

#7 2014-03-19 20:32:22

Painless
Member
Registered: 2006-02-06
Posts: 237

Re: sending a batch of mails using mail command

I didn't know about the -t flag, that's definitely the better option.  smile

Offline

Board footer

Powered by FluxBB