Now I want to write how to send an email using SMTP. Before I continue how to send the email, let me introduce you what is SMTP. Simple Mail Transfer Protocol (SMTP) is an Internet standard for electronic mail (e-mail) transmission across Internet Protocol (IP) networks. SMTP is specified for outgoing mail transport and uses TCP port 25.
While electronic mail servers and other mail transfer agents use SMTP to send and receive mail messages, user-level client mail applications typically only use SMTP for sending messages to a mail server for relaying. For receiving messages, client applications usually use either the Post Office Protocol (POP) or the Internet Message Access Protocol (IMAP) to access their mail box accounts on a mail server. [From Wikipedia]
I have try to use send email using PHPMailer in Yii Framework. But for now I will not explain how to send SMTP email in Yii Framework. Because I think just a little Indonesian people using this framework. If you want to try how to send SMTP email in Yii Framework, please see my post Send email using PHP Mailer and Yii Framework.
Lets try this code without a framework. Before you continued, please make sure you have the PHPMailer files. You can download from this link. For the authentication and mail server I will use Google Mail (GMail). If you have mail server, you can try this code.
<?php
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$body = "Test SMTP Mail using PHPMailer";
$body = eregi_replace("[\]", '', $body);
$mail->IsSMTP(); // telling to send this mail using SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets Google Mail as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername@gmail.com"; // Your(GMail) username
$mail->Password = "yourpassword"; // Your(GMail) password
$mail->SetFrom("name@yourdomain.com", "First Last");
$mail->AddReplyTo("name@yourdomain.com","First Last");
$mail->Subject = "Test SMTP Mail using PHPMailer";
$mail->MsgHTML($body);
$address = "recipient@otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}
?>
I hope, I can help you with this code.