codescracker


perl

Perl Send Email



« Previous Tutorial Next Tutorial »


You are free to send e-mail using your perl program. If you are using Linux/Unix platform, you can use sendmail utility inside your perl program to send email. Here is an example to send an email to the given email id.

#!/usr/bin/perl
 
$to = 'abc123@gmail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = 'This is just a test email sent by the Perl Script';
 
open(MAIL, "|/usr/sbin/sendmail -t");
 
# this is email header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";

# this is email body
print MAIL $message;

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

Here is an example, to send an HTML formatted email using the sendmail then you simply need to add Content-type: text/html\n in the header part of the email.

#!/usr/bin/perl
 
$to = 'abc123@gmail.com';
$from = 'webmaster@yourdomain.com';
$subject = 'Test Email';
$message = '<h1>This is just a test to email sent by the Perl Script</h1>';
 
open(MAIL, "|/usr/sbin/sendmail -t");
 
# this is email header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Content-type: text/html\n";

# this is email body
print MAIL $message;

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

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test