Use Net SMTP To Send Email
Login | Register RSS

The following Perl code is an example of how you can send emails from a CGI script through your SMTP mail server.
(Remember to escape special characters you set in the mail hash)

#!/usr/bin/perl

require 5.001;
$|=0;

use Net::SMTP;
use strict;

print "Content-type: text/html\n\n";

# initalize hash & set SMTP parameters
my %mail = (
  'smtp_host',     "smtp.yourdomain.com",
  'smtp_port',     "25",
  'smtp_username', "xxxUSERNAMExxx",
  'smtp_password', "xxxPASSEWORDxxx",
);

# append to hash the data for the email
$mail{'from'} = "you\@yourdomain.com";
$mail{'to'}   = "somebody\@someplace.com";
$mail{'cc'}   = ""; # leave blank, when not being used.
$mail{'bcc'}  = ""; # leave blank, when not being used.
$mail{'subject'} = "Insert email subject here...";
$mail{'message'} = "Insert email message body here...";

# pass the hash to the function, to send the email
my ($status, $response) = &send_email(%mail);

# show message whether email was sent or not.
if ($status eq "success") {
  print "OK: Email was sent.\n";
}
else {
  print "ERROR: Email could not be sent. ($response)\n";
}

exit;


##### YOU SHOULD NOT NEED TO CHANGE ANYTHING BELOW THIS LINE #####

sub send_email {
  my %mail = @_;

  my ($status, $response);

  ## uncomment the below line, if you have not defined the Net::SMTP module at the top of your script.
  #use Net::SMTP;

  ## initialize Net::SMTP module; passing it the SMTP host name & port #.
  my $smtp = Net::SMTP->new("$mail{'smtp_host'}", Port=> $mail{'smtp_port'}, Debug=>0);

  ## see if SMTP connection failed.
  if ( ! $smtp ) {
    $status = "problem";
    $response = "Connection to SMTP server failed";
    warn("$status - $response");
    return("$status", "$response");
  }

  ## if connection was successful, send login info.

  ## do SASL authentication (plain text password)
  my $ret = $smtp->auth("$mail{'smtp_username'}","$mail{'smtp_password'}");

  if ( ! $ret ) {
    $status = "problem";
    $response = "SMTP authentication failed\n";
    warn("$status - $response");
    return("$status", "$response");
  }

  ## assume SMTP authentication succeeded...
  #print "SMTP authentication succeeded...\n";
 
  ## set the envelope from address
  $smtp->mail("$mail{'from'}");

  ## set the envelope recipient address
  # NOTE: this part can be called multiple times, if multiple people are to the emailed; or simply set all emails in an array & pass the array in)
  $smtp->recipient("$mail{'to'}");

  ## now try to send the email:

  ## initialize email message
  $smtp->data();

  ## insert email data (To, From, Subject)
  $smtp->datasend("From: $mail{'from'}\n");
  $smtp->datasend("To: $mail{'to'}\n");
  if ($mail{'cc'} ne "") {
    $smtp->datasend("Cc: $mail{'cc'}\n");
  }
  if ($mail{'cc'} ne "") {
    $smtp->datasend("Bcc: $mail{'bcc'}\n");
  }
  $smtp->datasend("Subject: $mail{'subject'}\n");
  $smtp->datasend("\n"); # mandatory before starting the email message body

  ## insert email message body
  $smtp->datasend("$mail{'message'}");

  ## indicate end of the email message
  $smtp->dataend();

  ## close the SMTP connection
  $smtp->quit();

  ## now return a response to the calling script/function.
  $status = "success";
  $response = "Email Sent";
  return("$status", "$response");
}

If you like this site or any of its content, please help promote it. Use the social media buttons below to help spread the word. Don't forget to post in the comments section.

  Print   Email