Problem in sending email with attachment

the mail() function

Moderators: macek, egami, gesf

sathiyaseelan8
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Feb 23, 2011 5:55 pm

Problem in sending email with attachment

Postby sathiyaseelan8 » Thu Apr 28, 2011 9:26 pm

i am developing a function where i must able to send an email with PDF attachment.

now i can send email with attachment to a person. but the receipient could not be able to open the attachment from the email. The adobe reader giving error when the receipient open the attachment. The error is (it was sent as an email attachment and wasn't correctly decoded).

this is my code. can anyone help me... thanks in advance..

<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');

include("Include/database.php");

$SAFNo = $_GET['id'];

$Sql1 = "SELECT OldSAFNo, Client, RequestMode, RequestedBy, IssueDate, RecordedBy, Issue FROM safissue WHERE OldSAFNo = '$SAFNo'";

$Result1 = mysql_query($Sql1);


$Row1 = mysql_fetch_array($Result1);
$NewSAFNo = $Row1['OldSAFNo'];
$Client = $Row1['Client'];
$RequestedBy = $Row1['RequestedBy'];
$RequestMode = $Row1['RequestMode'];
$RecordedBy = $Row1['RecordedBy'];
$ReportedDate = $Row1['IssueDate'];
$Issue = $Row1['Issue'];

//changing date format
list($y, $m, $d) = explode("-", $ReportedDate);
$date = date('d-m-Y');

//get client name, tel and fax
$SqlClient = "SELECT ClientName1, Telephone1, Fax FROM client WHERE ClientCode = '$Client'";
$ReaultClient = mysql_query($SqlClient);
$RowClient = mysql_fetch_array($ReaultClient);

$NewClient = $RowClient['ClientName1'];
$NewTel = $RowClient['Telephone1'];
$NewFax = $RowClient['Fax'];

//get request mode
$SqlMode = "SELECT Mode FROM requestmode WHERE ID = '$RequestMode'";
$ResultMode = mysql_query($SqlMode);
$RowMode = mysql_fetch_array($ResultMode);

$NewRequestMode = $RowMode['Mode'];

//get reported by
$SqlReport = "SELECT ContactName FROM clientcontact WHERE ContactPersonID = '$RequestedBy'";
$ResultReport = mysql_query($SqlReport);
$RowReport = mysql_fetch_array($ResultReport);

$NewRequestedBy = $RowReport['ContactName'];

//get recorded by
$SqlRecorded = "SELECT Name FROM employee WHERE EmployeeID = '$RecordedBy'";
$ResultRecorded = mysql_query($SqlRecorded);
$RowRecorded = mysql_fetch_array($ResultRecorded);

$NewRecordedBy = $RowRecorded['Name'];

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
//$pdf->SetCreator(PDF_CREATOR);
//$pdf->SetAuthor('Nicola Asuni');
//$pdf->SetTitle('TCPDF Example 002');
//$pdf->SetSubject('TCPDF Tutorial');
//$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set font
$pdf->SetFont('times', 'BI', 50);

// add a page
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 20);

// set some text to print
//$txt = "sathiya";

// print a block of text using Write()
//$pdf->Write($h=0, $ClientName, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);

// output the HTML content
//ob_end_clean();

$html = '<table width="550" border="0" cellpadding="1" cellspacing="1">
<tr>
<td width="420">&nbsp;</td>
<td width="110"><font face="helvetica" size="11"><b>Form No<br>
Revision</b></font></td>
<td width="80"><font face="helvetica" size="11"><b>: FM-34<br>
: 0</b></font></td>
</tr>
</table>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="160"><img src="images/ParadigmLogo.jpg" alt="PARADIGM SYSTEM BERHAD" width="150" height="90" /></td>
<td width="470"><table width="470" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="455" align="center"><font face="helvetica" size="11"><b>PARADIGM SYSTEM BERHAD</b></font><br>
<font face="helvetica" size="11">SUITE A605, PHILEO DAMANSARA II, 15 JALAN 16/11,<br>46350 PETALING JAYA, SELANGOR<br>TEL. : 03-79571121 FAX : 03-79571131
</font></td>
</tr>
</table></td>
</tr>
</table><br>
<table width="600" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><div align="center"><font face="helvetica" size="11"><b>SAF ACKNOWLEDGEMENT</b></font></div></td>
</tr>
</table>
<table border="0">
<tr>
<td width="400"></td></tr></table>
<table width="585" border="1" cellspacing="0" cellpadding="2">
<tr>
<td width="125"><font face="helvetica" size="11"><b>ORGANISATION</b></font></td>
<td width="195"><font face="helvetica" size="11"><label>'.$NewClient.'</label></font></td>
<td width="140"><font face="helvetica" size="11"><b>REQUEST MODE</b></font></td>
<td width="140"><font face="helvetica" size="11"><label>'.$NewRequestMode.'</label></font></td>
</tr>
<tr>
<td><font face="helvetica" size="11"><b>TEL NO.</b></font></td>
<td><font face="helvetica" size="11"><label>'.$NewTel.'</label></font></td>
<td><font face="helvetica" size="11"><b>FAX NO.</b></font></td>
<td><font face="helvetica" size="11"><label>'.$NewFax.'</label></font></td>
</tr>
<tr>
<td><font face="helvetica" size="11"><b>REQUESTED BY</b></font></td>
<td><font face="helvetica" size="11"><label>'.$NewRequestedBy.'</label></font></td>
<td><font face="helvetica" size="11"><b>DATE REQUESTED</b></font></td>
<td><font face="helvetica" size="11"><label>'.$d.'-'.$m.'-'.$y.'</label></font></td>
</tr>
<tr>
<td><font face="helvetica" size="11"><b>FROM</b></font></td>
<td><font face="helvetica" size="11"><label>'.$NewRecordedBy.'</label></font></td>
<td><font face="helvetica" size="11"><b>DATE SENT ACK</b></font></td>
<td><font face="helvetica" size="11"><label>'.$date.'</label></font></td>
</tr>
<tr>
<td><font face="helvetica" size="11"><b>Issue</b></font></td>
<td colspan="3"><font face="helvetica" size="11"><label>'.$Issue.'</label></font></td>
</tr>
</table><br>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="442" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="290"><font face="helvetica" size="11"><b>SAF Report No</b></font></td>
<td width="270"><font face="helvetica" size="11"><b>:
<label>'.$SAFNo.'</label></b></font></td>
</tr>
<tr>
<td><font face="helvetica" size="11">Thank you for your SAF dated</font></td>
<td><font face="helvetica" size="11">:
<label>'.$d.'-'.$m.'-'.$y.'</label></font></td>
</tr>
<tr>
<td><font face="helvetica" size="11">Date Received</font></td>
<td><font face="helvetica" size="11">:
<label>'.$d.'-'.$m.'-'.$y.'</label></font></td>
</tr>
<tr>
<td colspan="2"><font face="helvetica" size="11">Your SAF has been recorded in our SAF log,</font></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><font face="helvetica" size="11">Please quote the SAF Report No for future correspondence.</font></td>
</tr>
</table>
</td>
</tr>
</table>';

$Output1 = '<table width="610" border="0">
<tr>
<td width="610">'.$html.'</td>
</tr></table>';

$pdf->writeHTML($Output1, true, 0, true, 0, '');

// reset pointer to the last page

$pdf->lastPage();

// ---------------------------------------------------------
// email stuff (change data below)

$to = "receipient@yahoo.com";
$from = "sender@yahoo.com";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";

// a random hash will be necessary to send mixed content

$separator = md5(time());

// carriage return type (we use a PHP end of line constant)

$eol = PHP_EOL;

//attachment name

$filename = "example.pdf";


//Close and output PDF document
ob_end_clean();
$pdf->Output('L1.pdf', 'I');


$attachment = chunk_split(base64_encode($pdfdoc));

// main header (multipart mandatory)

$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message

$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;

// attachment

$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";



// send message

mail($to, $subject, $message, $headers);

//============================================================+
// END OF FILE
//============================================================+
?>

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Problem in sending email with attachment

Postby johnj » Thu Apr 28, 2011 11:15 pm

Save the pdf file locally and then try to open it. Are you able to do that?


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest