Help with Sending Multiple Emails!

T

Terry

Hi,

I am new to Perl. I ran into this problem while trying to send out 2
messages from a script. Nearly half the time, the second message didn't
get sent, while the first message always get sent. Here is what I did:

# send the 1st message
open (M, "| /usr/sbin/sendmail -t");

print M "To: $address_01\n";
print M "From: $address_from\n";
...
# content of 1st message

close (M);


# send the 2nd message
open (M, "| /usr/sbin/sendmail -t");

print M "To: $address_02\n";
print M "From: $address_from\n";
...
# content of 2nd message

close (M);


I wonder if this is the right way to implement this. Is there a way to
check to see if the messages have been sent successfully?

Thanks in advance for your help!

Terry
 
P

Paul Lalli

Terry said:
Hi,

I am new to Perl. I ran into this problem while trying to send out 2
messages from a script. Nearly half the time, the second message didn't
get sent, while the first message always get sent. Here is what I did:

# send the 1st message
open (M, "| /usr/sbin/sendmail -t");

Always check the return value of open.
open (M, "| /usr/bin/sendmail -t) or die "Cannot open pipe to sendmail:
$!";
print M "To: $address_01\n";
print M "From: $address_from\n";
...
# content of 1st message

close (M);


# send the 2nd message
open (M, "| /usr/sbin/sendmail -t");

See above.
print M "To: $address_02\n";
print M "From: $address_from\n";
...
# content of 2nd message

close (M);


I wonder if this is the right way to implement this.

It's *a* way. It's probably not the best way. You'd be far better off
using one of the several available email modules available on the CPAN.
Is there a way to
check to see if the messages have been sent successfully?

Not with this method. All you can check is that the pipe to sendmail
was opened correctly, and that the pipe was closed correctly. What
sendmail does is out of your control. That is one of the reasons I
reccomend the emailing modules.

Paul Lalli
 
R

Reto Hersiczky

Terry,
you had to close and re-open the file handle to Sendmail for each next
message.
Otherwise I'd expect the first e-mail recipient would receive all the output
as a
long thread ;)

A performance issue for the necessary closing after each message should not
be
worth to be considered if you add the "-odq" option (option delivered
queued )
to the Sendmail command:
open (M, "| /usr/sbin/sendmail -odq -t");

This will give your script the control flow back very quickly and Sendmail
will use just one SMTP session if multiple recipients had the same MX host.

hth
--retoh :)
 
R

Reto Hersiczky

I agree with the previous reply, checkout these CPAN modules first:

Net::SMTP
or
MIME::Lite
(I prefer *this*, see an example on
http://www.infocopter.com/perl/send-htmlmail-multi.htm)

To check if the mail has been accepted, you should hack smth like this:

If the mail could be sent or not, had to be checked with the mail log,
usually located at /var/log/maillog

--retoh :)
 
C

chris-usenet

Terry said:
I am new to Perl. I ran into this problem while trying to send out 2
messages from a script. [..]
# send the 1st message
open (M, "| /usr/sbin/sendmail -t");
print M "To: $address_01\n";
print M "From: $address_from\n";

These variables aren't from a CGI by any chance, are they? Have you
sanitised $address_01 and $address_from? If not, consider what happens
if either of the variables contains a newline followed by a very long
"Bcc: spam1@address1 spam2@address2..."

Chris
 
P

P.R.Brady

Terry said:
Hi,

I am new to Perl. I ran into this problem while trying to send out 2
messages from a script. Nearly half the time, the second message didn't
get sent, while the first message always get sent. Here is what I did:

# send the 1st message
open (M, "| /usr/sbin/sendmail -t");

print M "To: $address_01\n";
print M "From: $address_from\n";
...
# content of 1st message

close (M);


# send the 2nd message
open (M, "| /usr/sbin/sendmail -t");

print M "To: $address_02\n";
print M "From: $address_from\n";
...
# content of 2nd message

close (M);


I wonder if this is the right way to implement this. Is there a way to
check to see if the messages have been sent successfully?

Thanks in advance for your help!

Terry

Terry,
that's a refreshing back to basics approach!
Alternatively, the module SendMail as described at
http://www.tneoh.zoneit.com/perl/SendMail/
is easy to use. Remember to 'reset' after each email.
The site gives a download link but I installed it with PPM.

I hope you are not writing a spam engine!

Regards
Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top