When we do large projects in php, we have to send emails to users and admins to give some notification to them. In Php projects most of the people use the default mail function .It is very easy to use and will work Perfectly. But the problem is that the mail may go to spam or junk folder. So many users may not notice these mails. So Inorder to get rid of it, we have to use SMTP mails. So it will not go to junk folders.
In this Post Iam explaining how to send SMTP mails from your Server. It is very simple and easy to integrate.
For doing this, first you have to make sure that PEAR mail package is enabled on your server. If not please install it. It is free to intsall. If you didn't know how to manage extensions in server just ask server service provider to do it for you.
Now download PEAR mail script from here
Now place it your folder where the mail script resides.
Place this script to the sending portion of your script.
<?php
<?php
require_once "Mail.php"; //includes all mail library for smtp mails
include 'Mail/mime.php' ; // includes mime library for sending multimedia mails with attachment
include('Mail/mail.php'); // adds the enhanced send function
$from = "<".$frommail.">";
$to = "<".$toemail.">";
$body = $description;
$host = "hostname";
$port = "25";
$username = $usernamea;
$password =$password;
$crlf = "\r\n";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($description);
$ds = DIRECTORY_SEPARATOR; //1
//attaching images/docs in mail
if (!empty($_FILES) && isset($_FILES)) {
for($k=0;$k<count($_FILES);$k++){
$name=$_FILES['attachment']['name'][$k];
$size=$_FILES['attachment']['size'][$k];
$tmpname=$_FILES['attachment']['tmp_name'][$k];
$uploader->uploadmultiFile($name,$size,$tmpname);// my own function call just leave it.. its image storing code
$image = $uploader->getUploadName();
$tarfil='../upload/'.$image;
$mime->addAttachment($tarfil, 'text/plain'); // adding attachment to mail
}
}
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$body = $mime->get();
$hdrs = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $hdrs, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
?>
include 'Mail/mime.php' ; // includes mime library for sending multimedia mails with attachment
include('Mail/mail.php'); // adds the enhanced send function
$from = "<".$frommail.">";
$to = "<".$toemail.">";
$body = $description;
$host = "hostname";
$port = "25";
$username = $usernamea;
$password =$password;
$crlf = "\r\n";
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($description);
$ds = DIRECTORY_SEPARATOR; //1
//attaching images/docs in mail
if (!empty($_FILES) && isset($_FILES)) {
for($k=0;$k<count($_FILES);$k++){
$name=$_FILES['attachment']['name'][$k];
$size=$_FILES['attachment']['size'][$k];
$tmpname=$_FILES['attachment']['tmp_name'][$k];
$uploader->uploadmultiFile($name,$size,$tmpname);// my own function call just leave it.. its image storing code
$image = $uploader->getUploadName();
$tarfil='../upload/'.$image;
$mime->addAttachment($tarfil, 'text/plain'); // adding attachment to mail
}
}
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$body = $mime->get();
$hdrs = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $hdrs, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
}
?>
Try it and have fun.. have a great day :-)