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, ...).

10 comments:

rize wordpress said...

Our PHP Developers enables clients to effectively store and display content on their site.

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

china tours said...

Such a nice post, We loved it a great deal!You can learn more: China tour packages | China travel packages | China Travel Agency

Learn Chinese said...

AllMandarin is the only company that can give you an excellent alternative to learn Mandarin Chinese that is not only better, but also more efficient in terms of cost, flexibility and learning methods.I am a Chinese teacher,you can learn more about Free Chinese language info: Learn mandarin online | Learn mandarin Chinese | Mandarin Teacher

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

learning Chinese online said...

The best place to learn Chinese online is in China. However, we understand that it isn't always possible to move here to learn Chinese language. The next best thing is to study with our experienced teachers in a virtual classroom. Online students enjoy the same excellent way of mandarin online lessons and custom designed courseware that we provide for our face to face clients.

ForwardChoicer said...

Hi

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in Chennai | Online PHP Training

PHP Training in Chennai | Online PHP Training

Girish Desai 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.

Girish Desai said...

Thanks for sharing this and its very useful to the back end developers. The explanation given is really comprehensive and informative .