Home
 

Customer Support

Search for keywords:

Browse by category:

How do I use sendmail with perl?

You can either specify who the mail will be sent to as you print to the MAIL filehandle like so:
#!/usr/bin/perl

unless(open (MAIL, "|/usr/sbin/sendmail -t")) {
print "error.\n";
warn "Error starting sendmail: $!";
}
else{
print MAIL "From: me\@mydom.com\n";
print MAIL "To: test\@testdom.com\n";
print MAIL "Subject: test subject\n\n";
print MAIL "test mail message";
close(MAIL) || warn "Error closing mail: $!";
print "Mail sent.\n";
}
or you can specify who the mail will be sent to as you invoke sendmail:
#!/usr/bin/perl

unless(open (MAIL, "|/usr/sbin/sendmail recipient\@someplace.com")) {
print "error.\n";
warn "Error starting sendmail: $!";
}
else{
print MAIL "From: me\@mydom.com\n";
print MAIL "Subject: test subject\n\n";
print MAIL "test mail message";
close(MAIL) || warn "Error closing mail: $!";
print "Mail sent\n";
}

User-Contributed Notes

add a note
perl -at- sedition.com
30-Sep-2002 00:10
In addition to the sample code modwest shows there are *many* perl
modules that make assembling and sending mail a snap. 

MIME::Lite is great for attachments (sending images or attached files)
and Mail::Mailer is a terrific pick for simple plain text or HTML mail. 
Here's a sample:


#!/usr/bin/perl
use warnings;
use strict;
#----------------------------------------------
use Mail::Mailer qw(sendmail);
#----------------------------------------------
my $mail = Mail::Mailer->new('sendmail')
    or die "Couldn't create a mail object!\n";

my $myself = $ENV{USER} . '@' . $ENV{HOST};

my $mail_headers =  {
    'Content-Type' => 'text/plain',
    To => [ 'tuna@fish.net', $myself ],
    From => $ENV{USER} . '@sedition.com',
    Subject => "Testing, 1, 2, 5 -- 3, Sire!",
};

$mail->open( $mail_headers );

$mail->print("This message has no body!\n",
             "Except this of course...");

$mail->close();

exit 0;
#----------------------------------------------

add a note

Related Questions:


How do I use cgi scripts?

My formmail.pl script from Matt's Script Archive doesn't work.

Why does Sbox say "no such file or directory" but the files are there?

What Perl modules are installed?

Do you offer CGI? What scripts can I install?

I can't get the python socket module to work?

Why do I get: Sbox error file is world writable?

What is the path to perl or some other program?

Sendmail does not work or does not accept my optional arguments.

Importing the Python cgi module doesn't work.

How do I use MySQL with Python?

What version of Python do you support and what modules are available?

Why do I get: Sbox Error exec of [some script] failed (Exec format error)?

Why do I get: Sbox error file not world executable?

Why do I get: Sbox error the directory containing [some script] is world writable?

Why do I get: Sbox Error exec of [some script] failed (Permission denied)

Why do I get: Sbox error: cannot run suid scripts

Why do I get: Sbox Error exec of [some script] failed (Resource temporarily unavailable)

Browse Categories:

Getting Started, FTP, Telnet/SSH, Moving Domains, E-mail, Traffic Reports, Mailing Lists, Apache, PHP, CGI, Other Server-Side Scripting, MySQL Database, Imaging Libraries, Other Software, Billing & Terms, Control Panel, E-commerce, Pre-Sales


Tiny Modwest Logo         Copyright 2000-2008 by Modwest, Inc.          About Us    |    Blog    |    Jobs    |    Web Design    |    Contact Us