Friday, January 2, 2015

PHPMailer and Hostinger Malaysia (SMTP server)

Peace be upon you and Hi!
Final year project (FYP) or Projek Sarjana Muda (PSM) are individual project based on research or developing a prototype software for client during final year of study to fulfil the undergraduate requirement. Enough of FYP/PSM introduction.

Well, the most famous software that has been developed are from web-based application and email was the famous communication between system and users, maybe some stuff like send notice or notification.

So lets just keep it simple here, I want to share my readers how to use PHPMailer class and Hostinger SMTP server to sent emails. Please remember, you can use any SMTP server either paid or free. This is just a complete tutorial applied for Hostinger web hosting, because I know, not all of student can afford to rent email server. Hopefully this tutorial will help you to add email functionality in your system.

Step 1 – PHPMailer class

==========================
PHPmailer are complete package of class files that allow web based application to send email and attachment, besides using mail() function through PHP. Now what you need to to do are just download PHPMailer from the official Github account. Download the Zip files:
https://github.com/Synchro/PHPMailer/archive/master.zip

Step 2 – Hostinger Hosting

============================
Now you need a SMTP server to allow you send email to targeted recipient. I choose Hostinger hosting that allow you to create 2 email account under free hosting plan. Now, just register your account directly from this link :
http://api.hostinger.my/redir/5613780


Now create a new hosting account and a new email account from control panel account from host list. Remember to take notes about you new username, password, SMTP server and port details.

Step 3 – Sending email

=======================
Extract PHPmailer into your system folder, and create a new PHP file and wrote this code within the same folder as PHPMailer

<?php
include_once("PHPMailerAutoload.php");
$mail                = new PHPMailer();
$mail->IsSMTP(); 
$mail->SMTPDebug  = false; 
$mail->SMTPAuth   = true;     
$mail->Host       = 'mx1.hostinger.my'; // SMTP host
$mail->Port       = 2525; // SMTP open port
$mail->Username   = '[email protected]'; // SMTP username
$mail->Password   = 'MyPassWord';
$mail->SetFrom('[email protected]', ' MY NAME'); 
$mail->AddReplyTo('[email protected]','REPLY NAME'); 
$email = "[email protected]"; // Recipient email
$mail->AddAddress($email);
$mail->Subject    = 'Hostinger'; // Email subject

$mail->AltBody    = 'To view the message, please use an HTML compatible email viewer!'; 
$mail->IsHTML(false);
$mail->Body = 'Hack the planet r0x!';


if(!$mail->Send()) {
  echo "Mailer Error: ". $mail->ErrorInfo;
} else {
  echo "Message sent!";
}    
// From : www.Kriptonium.com   
?>

Please modify SMTP host, port, username, password, subject, body and recipient email based on your configuration. Now execute the file from web server and check your recipient inbox. Do they get a new email from you? If yes, congratulation to yourself. From this concept try to apply it onto your project. That all from me.Please have a great day and Thank you :)

6 comments:

  1. Hostinger does not display the SMTP port if you are on free hosting sadly.

    ReplyDelete
    Replies
    1. I am using free account and able to use the SMTP service.

      Delete
    2. Is it still working to you? Because I cannot configure the SMTP server (free hosting).

      Delete
    3. Is it still working to you? Because I cannot not figure out.

      Delete
  2. Where do I put the PHPMailer files please? In the root folder of my application? Do I unzip them to that location?

    Thanks!

    ReplyDelete
    Replies
    1. Hi there, you can put it everywhere that you want. Here, I give some example with this directory :

      var/
      ----/web/
      --------/public/
      -----------------/Phpmailer/

      You email script are located at : /var/web/public/myscript.php
      and phpmailer files located at : /var/web/public/Phpmailer/

      so myscript.php need to get the path just like this include_once("./Phpmailer/PHPMailerAutoload.php");

      All configuration are depending where you locate the files, as long it valid.

      Delete