How to Send Mail using SMTP and PHP?

how to use SMTP send mail script

This article is all about "send Mail using SMTP and PHP".
so now you  can send your email SMTP authentication smtp and php script. each mail needed server authentication, So you have to buy mail server.



First you have to make a php file and add this code in this file and file name should be SMTPClass.php


<?php


class SMTPClient
{

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
{

$this->SmtpServer = $SmtpServer;
$this->SmtpUser = base64_encode ($SmtpUser);
$this->SmtpPass = base64_encode ($SmtpPass);
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;
$this->newLine = "\r\n";

if ($SmtpPort == "") {
$this->PortSMTP = 25;
} else {
$this->PortSMTP = $SmtpPort;
}
}

function SendMail (){
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) {
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n");
$talk["hello"] = fgets ( $SMTPIN, 1024 );

fputs($SMTPIN, "auth login\r\n");
$talk["res"]=fgets($SMTPIN,1024);

fputs($SMTPIN, $this->SmtpUser."\r\n");
$talk["user"]=fgets($SMTPIN,1024);

fputs($SMTPIN, $this->SmtpPass."\r\n");
$talk["pass"]=fgets($SMTPIN,256);

fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n");
$talk["From"] = fgets ( $SMTPIN, 1024 );

fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n");
$talk["To"] = fgets ($SMTPIN, 1024);

fputs ($SMTPIN, "RCPT TO: \r\n");
$talk["Bcc"] = fgets ($SMTPIN, 1024);

fputs($SMTPIN, "DATA\r\n");
$talk["data"]=fgets( $SMTPIN,1024 );

//Construct Headers
$headers = "MIME-Version: 1.0" . $this->newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $this->newLine;
$headers .= "From: <".$this->from.">". $this->newLine;
$headers .= "To: <".$this->to.">". $this->newLine;
$headers .= "Subject: ".$this->subject. $this->newLine;

fputs($SMTPIN, $headers."\r\n\r\n".$this->body."\r\n.\r\n");

$talk["send"]=fgets($SMTPIN,256);
//CLOSE CONNECTION AND EXIT ...
fputs ($SMTPIN, "QUIT\r\n");
fclose($SMTPIN);
//
}
return $talk;
}
}?>



Now make a other php file name could be anything according to your need.
and paste this code which is below  this line


<?php
include("SMTPClass.php")

function smtpmail_send($to,$subject,$body)
{
$SmtpServer="smtpout.site.net";
$SmtpPort="80";
$SmtpUser="email@domain.com";
$SmtpPass="password";
//$to = " to@domain.com ";
$from = " email@domain.com ";
//$subject = "subject";
//echo $SmtpServer.'--'.$SmtpPort.'--'.$SmtpUser.'--'.$SmtpPass.'--'.$from.'--'.$to.'--'.$subject.'--'.$body;die;
$SMTPMail = new SMTPClient($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
return "sent";
}
?>

happy coading

Comments

  1. After 2 hours of searching, and trying, this little script answered my prayers.... had the script already but this now extends it to send HTML....with headers....

    Thank you .....

    ReplyDelete

Post a Comment