Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Sending an Email with Perl via Sendmail

From John's Wiki

If your system has a sendmail compatible MTA installed you can use it in conjunction with the Perl script below to send an Email using Perl.

#!/usr/bin/env perl
 
$to = 'to@example.com';
$from = 'from@example.com';
$subject = 'Sent with Perl and Sendmail';
$message = 'This is test email sent using Perl and Sendmail.';
 
open(MAIL, "|/usr/sbin/sendmail -t");
 
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;

close(MAIL);
print "Email Sent!\n";

As you can see, you can just fopen the sendmail program as if it were a file and print to it the same way you'd print to a file, then close it the same way you'd close a file to send.