PHP send mail SMTP authentification

this is a very complete php send mail script, with authentification ofcourse.
You can choose the smtp server, use your username and password, and can send a html email.

This script does not use php mail() function, it's all from scrach.





function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{

$smtpServer = "smtpout.europe.secureserver.net";
$port = "25";
$timeout = "30";
$username = "username@mailserver.com";
$password = "mypassword";
$localhost = "localhost"; //hosting server
$newLine = "\r\n";

$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return false;
} else {
$logArray['connection'] = "Connected: $smtpResponse";
}

//Request Auth Login

fputs($smtpConnect,"AUTH LOGIN" . $newLine);

$smtpResponse = fgets($smtpConnect, 515);


//Send username

fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);

//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);

//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);

//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);

//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);

//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);

//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
//Add these two lines if you have any problem
//$headers .= "To: $nameto <$to>" . $newLine;

//$headers .= "From: $namefrom <$from>" . $newLine;


fputs($smtpConnect, "To: $nameto <$to>\nFrom: $namefrom <$from>\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);

// Say Bye to SMTP
fputs($smtpConnect, "QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);

return true;
}

?>

That's it, just copy this mail SMTP authentification function and change configuration variables values to your own (smtpserver, port, username, password, ...).

14 comments:

Diablo 3 Gold said...

Very cool. It's my job to take pleasure in the interview. Thanks for having one of these website Buy RS Gold Cheap Diablo 3 Items

League of Legends boost said...

Incredible weblog! Good, obvious as well as well crafted. Many thanks with regard to discussing!

Buy elo boost


FUT 14 Coins

S. B. Patil Institute of Management said...

Thanks for sharing this and its very useful post, it was so interesting to read & appreciate your work for blog post which is very useful.

Unknown said...

Thanks for sharing this information. Its really useful information about PHP. PHP training in Bangalore

Zara Porter said...

By hiring a company that has good years of experience, you can make your business become famous in a less amount of time.
Website Designing Agency in Bangalore | Website Development Agency in Bangalore

Unknown said...
This comment has been removed by the author.
BTree Systems, Software (IT) Training Institute said...

aws training in chennai
Python training in Chennai

BtreeSystem Training said...

iot training in chennai - Iot Training in Chennai - Hurray start learning the IOT course in Chennai now.
DevOps training in chennai - join the DeVops and learn the course from Aws with DeVops training in Chennai.
blue prism training in Chennai - Best Blue prism training in Chennai.
uipath training in Chennai - ui path Training in Chennai.
microsoft azure training in chennai - Best azure training in Chennai is providing the course with microsoft azure certification and training in Chennai.

BTree Systems said...
This comment has been removed by the author.
BtreeSystem Training said...

Linkedin
Facebook
Youtube
Tweet

BtreeSystem Training said...

Aws vs google cloud blog btree systems chennai - Since its beginning, the cloud environment has gotten a complex, steadily extending a bunch of suppliers, advancements, items, and administrations.

Matt Reeves said...

Mindblowing blog very useful thanks
AWS Training in Velachery
AWS Training in Chennai

scopex said...

Low cost crm for small business

Joe said...

Work Time Management Software
Opportunities Management Software
Purchase Management Software