简体   繁体   中英

How do you send the same email to multiple recipients with Sendmail using Perl?

I have the following PERL script, but I can't seem to get it to send to more than one email at once. How do I send to multiple emails, preferably as separate emails?

 open(SENDMAIL, "|/usr/lib/sendmail -oi -t") || die "Cannot open sendmail output"; print SENDMAIL <<"ENDENDEND"; From: <test\\@test.com> To: <test1\\@test1.com> Subject: report spam MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="DeathToSpamDeathToSpamDeathToSpam" This is a multi-part message in MIME format. --DeathToSpamDeathToSpamDeathToSpam Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit --DeathToSpamDeathToSpamDeathToSpam Content-Type: message/rfc822 Content-Disposition: attachment ENDENDEND while (<STDIN>) { print SENDMAIL ; } print SENDMAIL <<"ENDENDEND"; --DeathToSpamDeathToSpamDeathToSpam-- ENDENDEND close (SENDMAIL); 

Thanks!

Sending a mail by executing the sendmail program can do only a single mail at a time because that's how the sendmail program works: the mail gets piped into sendmail and the mail is done on EOF. This means you need to invoke sendmail again for the next mail.

Another way would be to not execute sendmail for delivery but directly talk to a SMTP server using Net::SMTP or similar modules - this way you could also send multiple mails within a single SMTP connection.

EDIT: as noted in a comment by Andrzej A. Filip one call sendmail with the -bs option so that it works as a minimal SMTP server which expects communication with stdin and stdout. This feature seems to be implemented also in the sendmail wrapper from Postfix which is probably more in use than the original sendmail .
But, I'm not aware of any module which supports this mode of operation so you are on your own to setup the bidirectional communication with maybe IPC::Open2 and then speak the SMTP protocol including all the strange end of mail handling and escaping rules. It would be much easier to just talk SMTP to the mail server on localhost using Net::SMTP which already cares about all the protocol specific stuff and lets you just send the mails.

Adding "Cc: " seems to work in this instance.

For some reason, using Bcc: or adding another To: email does NOT work, but Cc: seems to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM