...try toying with MIME::Lite.
While the above may be true for the casual email admin, there are times that you need the functionally of working with sendmail directly. The standard config file may be set up to discourage spammers. Many email providers have multiple configurations and multiple queues for email activity. (Note: How about a different config file for timing considerations for sending a 'text' message to a cell phone). So while the above code is nice, it doesn't account for the interaction required to talk to sendmail directly. I prefer the following for directly calling sendmail from Unix/Linux:
my $CR = "\r\n"; ## This should be what your sendmail accepts.
## sendmail will use the correct $CR for the rec
+ipient(s)
open ( MAIL, ">", $out_email )|| die " Not open $out_email $!\n";
## Mail Header
print MAIL "To: $to$CR";
print MAIL "From: $from$CR";
print MAIL "Subject: $subject$CR";
print MAIL "MIME-Version: 1.0$CR";
print MAIL "Content-Type: text/html; charset=utf-8;$CR";
print MAIL "$CR"; ## "end of headers"
## Mail Body
print MAIL "<html><body><br />$CR";
print MAIL "This is a test mail$CR";
print MAIL "<br /></body></html>$CR";
print MAIL "$CR"; ## "end of body"
close(MAIL);
my $conf = "-C /etc/mail/alt_sendmail.cf"; ## Optional if the stand
+ard is okay!
my $smail = "/usr/sbin/sendmail $conf -bm -v -t < $out_mail 1>>/dev/nu
+ll 2>>/dev/null /&";
system($smail);
I have had to add the "-v" and "1>>/dev/null 2>>/dev/null" for some Linux distributions, so you may not need them, but they don't hurt either. Sendmail is very capable of having hundreds of email sessions running at the same time, so I add the "/&" so that I get control back and can continue processing in perl. Another good reason to do it this way is you can print the $smail and run the command from the command line. If it doesn't work, remove the "1>>/dev/null 2>>/dev/null" and you can see the conversation with sendmail (and any errors).
For future readers of this post, that may need help working with email and perl, an excellent O'Reilly book is "Programming Internet Email" by David Wood. His examples are in perl and the information is a great basic explanation of internet email. I do not have any affiliation with the author or the publisher but when I was looking for a book on programming email, I was discouraged by the horrible reviews for this book. One review stated the examples were written in Visual Basic, so I bought a used book. When I received the book, I was pleasantly surprised to see the extensive use of perl throughout the book.
Good Luck!
"Well done is better than well said." - Benjamin Franklin
|