Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sendmail problem

Status
Not open for further replies.

safra

Technical User
Joined
Jan 24, 2001
Messages
319
Location
NL
Hi,

I'm trying to make a simple script to send members from the mailing list an email.

When I only send one person an email it works fine but when I try to go through the array of members I receive an Internal Server Error.

This is the part:

foreach$newemail(@emails){
open (MAIL, "| $maillocation -t") || die "cant use $maillocation";
print MAIL "from: $from\n";
print MAIL "To: $newmail\n";
print MAIL "Reply-to: $from\n";
print MAIL "Subject: $subject\n";
MAIL " email message";
close (MAIL);
}

Any ideas whats causing this and what to do about it?

Regards,

Ron
 
Miss Type, sorry! The last line of the print code should be:

print MAIL " email message";

This part is correct in the script itself!

Ron
 
Two rules of thumb I always go by are:
1. use the "-w" switch on the #!/usr/bin/perl line at the top of every script, and
2. use "use strict;"

The "use strict;" will force you to declare all the variables you use, and thus will give you an error if you mistype a variable name - looks to me like your foreach references "$newemail", but your print MAIL "To:" references $newmail(this is missing the "e" in email).

Another thing is that there is NO need to open your pipe to mail for *every* email address - just build a string(such as $email_addresses) containing the list of email addresses separated by commas, and once that string is built, just use that in the "To: $email_addresses" - that way you only have to open mail once, and you only have to send one email - your mail client should take care of sending out multiple emails - one to each "To:" address.

HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Sorry again! You were right about $newmail and $newemail, thanks!

About your suggesting to open the pipe to mail only once.

Am I right that this can only be done if the message to each e-mail address is identical?

Each email will contain user-specific information!

Ron


 
Whats with the;

foreach$newemail(@emails){

line?

shouldn't it be;

foreach $newemail (@emails){
 
You were right about opening the pipe to your mailer - seems that you need to open/close it for each message you send - but if the message is the same going to more than one email address, you can just build a scalar containing all the email addresses separated by commas and do one send.

However, I did look at Mail::Mailer, and Mail::Mailer will allow you to instantiate one mail object that can be used to send more then one message, before you close the object at the end.

I did "perldoc -q mail", and came up with this:
-----------------------------------
=head2 How do I send mail?

Use the C<sendmail> program directly:

open(SENDMAIL, &quot;|/usr/lib/sendmail -oi -t -odq&quot;)
or die &quot;Can't fork for sendmail: $!\n&quot;;
print SENDMAIL <<&quot;EOF&quot;;
From: User Originating Mail <me\@host>
To: Final Destination <you\@otherhost>
Subject: A relevant subject line

Body of the message goes here after the blank line
in as many lines as you like.
EOF
close(SENDMAIL) or warn &quot;sendmail didn't close nicely&quot;;

The B<-oi> option prevents sendmail from interpreting a line consisting
of a single dot as &quot;end of message&quot;. The B<-t> option says to use the
headers to decide who to send the message to, and B<-odq> says to put
the message into the queue. This last option means your message won't
be immediately delivered, so leave it out if you want immediate
delivery.

Or use the CPAN module Mail::Mailer:

use Mail::Mailer;

$mailer = Mail::Mailer->new();
$mailer->open({ From => $from_address,
To => $to_address,
Subject => $subject,
})
or die &quot;Can't open: $!\n&quot;;
print $mailer $body;
$mailer->close();

The Mail::Internet module uses Net::SMTP which is less Unix-centric than
Mail::Mailer, but less reliable. Avoid raw SMTP commands. There
are many reasons to use a mail transport agent like sendmail. These
include queueing, MX records, and security.
---------------------------------------
HTH.
Hardy Merrill
Mission Critical Linux, Inc.
 
Normally I would put a space in there just to make it better for the view. But regarding errors, as far as my experiences go Perl is quite forgiving in this sort of situations.

Ron
 
Hmerrill, you were just ahead of me with your response.

I will read it carefully and try this.

Thanks again!

Ron
 
Shouldn't you have a blank line after your email headers? Also, it may not matter, but you have &quot;from:&quot; instead of &quot;From:&quot; in the header. Still, I don't think those are causing the ISE. They may cause the sendmail not to work, but shouldn't cause an ISE. That's usually either a syntax error, or you're not printing anything back to the browser (especially the &quot;Content-type: text/html\n\n&quot; header. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I tries what hmerrill suggested for only opening the pipe to mail once:

open (MAIL, &quot;| $maillocation -oi -t -odq&quot;) || die &quot;cant use $maillocation&quot;;
foreach$newmail(@emails){
print MAIL &quot;From: $from\n&quot;;
print MAIL &quot;To: $newmail\n&quot;;
print MAIL &quot;Reply-to: $from\n&quot;;
print MAIL &quot;Subject: $subject\n\n&quot;;
print MAIL &quot; email message&quot;;
}
close (MAIL);

I also followed tsdragon's suggestions ('From' instead of 'from' and a blank line after the subject).

Nothing happens. No errors no emails in inbox?

Is there a significant advantage of only opening the pipe to mail once or eachtime for each new email?

Ron


btw. tsdragon the initial code I had is working fine after changing $newemail into $newmail.
 
Someone else had an email problem that I helped with and it was very puzzling. We tried a bunch of things before we finally got it to work, but one thing I think helped is that we told perl to flush it's buffers after every print command. Try putting this line near the top of your program if you don't have it already:
Code:
$| = 1; #flush buffers after every print
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Try using

open (MAIL, &quot;| $maillocation -oi -t -odq > /tmp/test.out 2>&1&quot;)

and then examine the contents of that file when you've finished. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Hi Mike,

What is '> /tmp/test.out 2>&1' supposed to do?

I created the folder tmp and the file test.out. But it remains empty after excuting the script?

Ron
 
Here's another easy way to make sure your email message is getting formatted and output properly. Just change your open statement to:
Code:
open (MAIL, &quot;|cat&quot;);
That command simply takes everything you print to MAIL and returns it to the browser. Make sure you print a content-type header first, so the browser will like the output. text/plain works great for this, because you can see the formatting better. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top