Saturday, February 27, 2010

Sending Email using PERL Net::SMTP module

I recently bumped into a problem with my old CGI script that sends emails from LINUX/UNIX server in PERL platform.



I later found out that the server I rented/shared is no longer supporting emails using the sendmail –t command. I found a solution to it by using the PERL module of Net::SMTP.


Here are the codes that saved my day:



use Net::SMTP;

$smtp = Net::SMTP->new("smtp.yourdomain.com");

$smtp->mail("username\@ yourdomain.com");
$smtp->to($touser);
$smtp->data();
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("From: $fromuser\n");
$smtp->datasend("To: $touser\n");
$smtp->datasend("Subject: $subject\n\n");
$smtp->datasend("$messagebody");
$smtp->dataend();
$smtp->quit();



Make sure you know your smtp address before using these codes. I found many other similar codes online but none is fully working. These codes are fully tested with no problem. Let me know if find any problem with these codes.



P.S.: Please replace your own variables for $touser, $fromuser, $subject, $messagebody in PERL.

No comments:

Post a Comment