Sending mail from php

Today I am going to post a simple method to send mail from php code.To this we must use SMTP [Simple Mail Transfer protocol]which uses default port that is 25.Before sending mail,SMTP server name must be included in php.ini .To make our job easier I will use three php files.
They are
01: SMTPconfig.php
02: SMTPclass.php
03: index.php

01: SMTPconfig.php

//Server Address
$SmtpServer=”gmail.com”;
$SmtpPort=”25″; //default
$SmtpUser=”yourgmailid@gmail.com”;
$SmtpPass=”yourgmailpassword”;

Here,SMTP server setup is completed.

02: SMTPclass.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;

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

function SendMail ()
{
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
{
fputs ($SMTPIN, “EHLO “.$HTTP_HOST.”rn”);
$talk[“hello”] = fgets ( $SMTPIN, 1024 );
fputs($SMTPIN, “auth loginrn”);
$talk[“res”]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpUser.”rn”);
$talk[“user”]=fgets($SMTPIN,1024);
fputs($SMTPIN, $this->SmtpPass.”rn”);
$talk[“pass”]=fgets($SMTPIN,256);
fputs ($SMTPIN, “MAIL FROM: from.”>rn”);
$talk[“From”] = fgets ( $SMTPIN, 1024 );
fputs ($SMTPIN, “RCPT TO: to.”>rn”);
$talk[“To”] = fgets ($SMTPIN, 1024);
fputs($SMTPIN, “DATArn”);
$talk[“data”]=fgets( $SMTPIN,1024 );
fputs($SMTPIN, “To: to.”>rnFrom: from.”>rnSubject:”.$this->subject.”rnrnrn”.$this->body.”rn.rn”);
$talk[“send”]=fgets($SMTPIN,256);
//CLOSE CONNECTION AND EXIT …
fputs ($SMTPIN, “QUITrn”);
fclose($SMTPIN);
//
}
return $talk;
}
}
/////////////////////////////////

03: index.php

include(‘SMTPconfig.php’);
include(‘SMTPClass.php’);
if($_SERVER[“REQUEST_METHOD”] == “POST”)
{
$to = “to_mailid”;
$from = “from_mailid”;
$subject = “subject”;
$body = “message”;
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
$SMTPChat = $SMTPMail->SendMail();
}

Thanks for visiting this…
For more info Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *