Wednesday 14 May 2014

Sending Emails with attachment using SMTP Server

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
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>");
 } 
?>

Try it and have fun.. have a great day :-)

Monday 12 May 2014

Generating PDF in PHP using mPDF

When you work on large inventory projects in php, there may araise situation where you have to generate pdf dynamically for reports and all. So here Iam explaining about a simple php to PDF library named mpdf.
you can download it from here.
Now please download the latest version of the mpdf library and place it in your root folder of your project.
Here with the help of mpdf  you can generate pdf according to various styles using css. You can include css also using this library.

Now i will provide a sample code for generating report using mpdf.


<?php

// fetching data from database.. You can use your own methods
include("db.php");
$id=$_GET['id'];
$quo=new Quotation();
$orders=$quo->getuserquotations($id);
$orderdet=$quo->getdetails($id);
$prd=new Product();/
$amount=0;

//we have to save our design in this $html variable
$html = '
<h1>Report Detail</h1>
<p style="color:green"> Description here </p>
</br>';
$html.='<p><strong>Attn </strong>: Mr/Ms &nbsp;';
$html.=$orderdet[0]['name'];
$html.='&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Email</strong>:&nbsp;';
$html.=$orderdet[0]['email'];
$html.='</p>';
$html.='<p><strong>Mobile</strong>:  &nbsp;';
$html.=$orderdet[0]['mobile'];
$html.='&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;<strong>Tel</strong>:&nbsp;';
$html.=$orderdet[0]['phone'];
$html.='</p>';
$html.='<p><strong>Fax</strong>:  &nbsp;';
$html.=$orderdet[0]['fax'];
$html.='&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;<strong>Date</strong>:&nbsp;';
$html.=$orderdet[0]['date_booked'];
$html.='</p>';

$html.='<table border="1"><tbody>
<tr> <td><strong>Sr No. </strong></td><td><strong>Product Image </strong></td><td><strong>Description </strong></td><td><strong>QTY </strong></td> <td><strong>Rate </strong></td> <td><strong>Amount </strong></td> </tr>';
for($i=0;$i<count($orders);$i++){
$productid= $orders[$i]['prod_id']; 
     $proddet=$prd->getdetails($productid);

     $tt= $orders[$i]['qty']*$orders[$i]['price']; 
     $amount=$amount+$tt;
$html.='<tr><td>';
$html.=$i+1;
$html.='</td><td><img src="../upload/';
$html.=$proddet[0]['product_img'];
$html.='" width="150px"></td><td>';
$html.=$proddet[0]['description'];
$html.='</td><td>';
$html.=$orders[$i]['qty'];
$html.='</td><td>';
$html.=$orders[$i]['price'];
$html.='</td><td>';
$html.=$tt;
$html.='</td></tr>';
}

$html.='<tr><td></td> <td></td> <td></td> <td></td> <td>Total:</td> <td><span style="color:red">'.$amount.'</span>/-</td> </tr>';


$html.='
</tbody></table>
<p>&nbsp;</p>
';
// $html variabl ends here...

//now its time to include our library and generate report
include("mpdf.php");

$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 

$mpdf->SetDisplayMode('fullpage');

$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list

// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text
$mpdf->SetJS('this.print();'); // will print the pdf
$mpdf->WriteHTML($html,2);
$ext="pdf";
$savename="quote-".substr(md5(rand(1111,9999)),0,8).".".$ext;
$mpdf->Output($savename,'I'); // will display pdf to you in browser
exit;
?>
If you use

$mpdf->Output($savename,'D');

it will download the pdf to your computer.

If you use
$mpdf->Output($savename,'F');

it will download pdf to your local folder..you can set the path $savename variable

If you use
$mpdf->Output($savename,'I');
it will display pdf in your browset window itself..