How to send mail with SMTP Authentication?

The Simple Mail Transfer Protocol (SMTP) is the most widely used protocol for sending electronic mail.SMTP authentication is an extension to SMTP that proves your identity to a mail gateway.

Many shared hosting providers don’t allow to use the PHP mail() function for security reasons. Using SMTP Authentication instead of mail function adds layer of security to sendmail. You have to try this class for composing and sending e-mail messages that comes with a wrapper function named smtp_mail().

SMTP server configuration details:

SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUsername);
$this->SmtpPass = base64_encode ($SmtpPassword);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
if ($SmtpPort == “”)
{
$this->PortSMTP = 25;
}
else
{
$this->PortSMTP = $SmtpPort;
}
}
?>

 

Features of SMTP class:

  • Supports AUTH login using TLS or SSL
  • Sends messages a plain text.
  • Validates that server is connected and logged in.
  • Validates that email message has been sent.
  • Provides additional headers if needed.
SmtpServer, $this->PortSMTP))
{
fputs ($INPUTS, “EHLO “.$HTTP_HOST.”\r\n”);
$result["hello"] = fgets ( $INPUTS, 1024 );//Request Auth Login
fputs($INPUTS, “auth login\r\n”);
$result["res"]=fgets($INPUTS,515);//Send username
fputs($INPUTS, $this->SmtpUser.”\r\n”);
$result["user"]=fgets($INPUTS,515);//Send password
fputs($INPUTS, $this->SmtpPass.”\r\n”);
$result["pass"]=fgets($INPUTS,515);//Email From
fputs ($INPUTS, “MAIL FROM: from.”>\r\n”);
$result["From"] = fgets ( $INPUTS, 515 );//Email To
fputs ($INPUTS, “RCPT TO: to.”>\r\n”);
$result["To"] = fgets ($INPUTS, 515);//Email Body
fputs($INPUTS, “DATA\r\n”);
$result["data"]=fgets($INPUTS,515 );//Send Email
fputs($INPUTS, “To: to.”>\r\nFrom: from.”>\r\nSubject:”.$this->subject.”\r\n\r\n\r\n”.$this->body.”\r\n.\r\n”);
$result["send"]=fgets($INPUTS,515);//Disconnect from SMTP
fputs ($INPUTS, “QUIT\r\n”);
fclose($INPUTS);
}
return $result;
}
}
?>

Passing parameters to SMTP class:

$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['sub'];
$body = $_POST['message'];
$SMTP = new SMTPConfig ($from, $to, $subject, $body);
$SMTP->SMTPMail();
?>

Originally posted 2011-08-27 14:39:16.

This entry was posted in PHP, Programming and tagged , , . Bookmark the permalink.

Leave a Reply