php email with pdf attachment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coolumanga
Forum Newbie
Posts: 9
Joined: Wed Aug 18, 2010 11:21 pm

php email with pdf attachment

Post by coolumanga »

hi
this is my code

Code: Select all

<?php 
$subject=$_GET['subject'] ; 
$url=$_GET['url'] ; 
$doc=$_GET['doc'] ; 

//echo $url

$url="http://system.firefoxlanka.com/firefox/".$url ;
 ?>
<form id="form1" name="form1" method="get" action="pdf-o-matic.php">
    <table width="719" border="0" bgcolor="#FFFF99">
      <tr>
        <th colspan="2" scope="row"><div align="center">EMAIL</div></th>
      </tr>
      <tr>
        <th width="112" scope="row"><div align="left">From:</div></th>
        <td width="581"><label>
          <input name="from" type="text" class="style3" value="sujeewa@firefoxlanka.com" size="50" />
        </label></td>
      </tr>
      <tr>
        <th scope="row"><div align="left">To:</div></th>
        <td><input name="To1" type="text" class="style3" size="50" /></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><input name="To2" type="text" class="style3" size="50" /></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><input name="To3" type="text" class="style3" size="50" /></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><input name="To4" type="text" class="style3" size="50" /></td>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><input name="To5" type="text" class="style3" size="50" "/></td>
      </tr>
      <tr>
        <th scope="row"><div align="left">Subject : </div></th>
        <td><input name="Subject" type="text" class="style3" size="50" value="<?php print $subject ?>" /></td>
      </tr>
      <tr>
        <th valign="top" scope="row"><div align="left">Message : </div></th>
        <td><textarea name="message" cols="75" rows="10">Please See Attachment</textarea></td>
      </tr>
      <tr>
        <th colspan="2" scope="row"><div align="center">
          <input name="Submit" type="submit" class="style3" value="SEND MAIL" />
          &nbsp;&nbsp;</div></th>
      </tr>
      <tr>
        <th scope="row">&nbsp;</th>
        <td><input type="hidden" name="url" value="<?php print $url ?>"/></td>
      </tr>
	  <tr>
        <th scope="row">&nbsp;</th>
        <td><input type="hidden" name="doc" value="<?php print $doc ?>"/></td>


Action file

?
// header("Content-Type: application/pdf");
// header("Content-Disposition: inline; filename=\"pdf-o-matic.pdf\"");

echo " <br> <br> <br>";
echo "
	
<link rel='stylesheet' href='../menu/menu.css'>


<script language='JavaScript' src='../menu/menu.js'></script>
<script language='JavaScript' src='../menu/menu_items.js'></script>
<script language='JavaScript' src='../menu/menu_tpl.js'></script>
<script language='JavaScript'>
	new menu (MENU_ITEMS, MENU_TPL);
</script>


";


 function
   htmldoc_header($title = "")        // I - Title string
   {
     print("<html><head><title></title></head><body>\n");
   }

   function
   html_footer()
   {
     print("</body></html>\n");
   }

//include_once "../phplib/htmldoc.php";

// Globals...
global $_POST;                           // Form variables
global $_SERVER;                        // Server variables
global $PHP_SELF;                       // Local URL for this page

//
// 'show_form()' - Show a form for converting a URL to PDF.
//

function
show_form()
{
  global $PHP_SELF;

 

  print("<input type='hidden' name='FORMAT' value='.pdf'>"
       ."</center></form>\n");

 
}




function                                // O - 1 if the URL is bad, 0 otherwise
bad_url($url)                           // I - URL to check
{
  // See if the URL starts with http: or https:...
  if (strncmp($url, "http://", 7) != 0 &&
      strncmp($url, "https://", 8) != 0)
  {
    return (1);
  }

  // Check for bad characters in the URL...
  $len = strlen($url);
  for ($i = 0; $i < $len; $i ++) {
    if (!strchr("~_*()/:%?+-&@;=,$.0123456789"
               ."abcdefghijklmnopqrstuvwxyz"
               ."ABCDEFGHIJKLMNOPQRSTUVWXYZ", $url[$i]))
    {
      return (1);
    }
  }

  return (0);
}

            
//
// 'topdf()' - Convert the named file/URL to PDF.
//

function
topdf($filename,                        // I - File/URL to convert
      $options = "")                    // I - Additional command-line options
{
  flush();

 

ob_start();
 passthru("ulimit -m 16384 -t 5; htmldoc --no-localfiles --no-compression -t pdf14 --quiet "
          ."--jpeg --webpage $options '$filename'");

//$pdfdoc=ob_POST_contents();

//$pdfdoc=ob_get_contents();

//ob_end_clean();

$pdfdoc = $pdf->Output("", "S");

echo $pdfdoc;


$from     = $_GET["from"];
$To1     = $_GET["To1"];
$To2     = $_GET["To2"];
$To3     = $_GET["To3"];
$To4     = $_GET["To4"];
$To5     = $_GET["To5"];
$subject     = $_GET["Subject"];
$url     = $_GET["url"];
$message = $_GET["message"];
$doc     = $_GET["doc"];

$to = $To1 ;

if (strlen(trim($To2))>0)
$to=$to.",".$To2;

if (strlen(trim($To3))>0)
$to=$to.",".$To3;

if (strlen(trim($To4))>0)
$to=$to.",".$To4;

if (strlen(trim($To5))>0)
$to=$to.",".$To5;



      $separator = md5(time());
      $eol = PHP_EOL;
      $attachment = chunk_split(base64_encode($pdfdoc));
     // $attachment = $pdfdoc;

      $filename = $doc.".pdf";
    
      //echo $filename ;
     // main header (multipart mandatory)
      $headers = "From: ".$from.$eol;
	  $headers .= "Reply-To: ".$from.$eol;
	  $headers .= "Return-Path:wmjsbandara@yahoo.com ".$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, "", $headers);
      mail("wmjsbandara@gmail.com", $subject, "", $headers);
	
         echo "From ".$to ."<br>" ;
         echo "To ".$to ."<br>" ;
	     echo  "Subject ".$subject ."<br>";
	     echo  "<H2>Successfully Sent  </H2><br>";
         
/*	echo substr($doc,0,2);
	if(substr($doc,0,2)=="PO"){
	  echo echo "<p><a href='/firefox/PO_Imp_B_Head_list.php'>SC/PC -> S/C System ->SC Input </a></p>";
	}
	else
	{
		 echo "<p><a href='/firefox/Sc_Head_list.php'>SC/PC -> S/C System ->SC Input </a></p>
<p><a href='/firefox/PC_Head_list.php'>SC/PC -> S/C System -> PC Input </a> </p>" ; 
    }
*/
}


//
// MAIN ENTRY - see if we have form data...
//

if ($argc && $argv[0] == "source")
{
  echo " Show source...";
  htmldoc_header("xx");

?>


<?php

  print("<pre>\"pdf-o-matic.php\":\n");

  highlight_file("pdf-o-matic.php");

  print("</pre>\n");

  html_footer();
}
else if (array_key_exists("url", $_GET))
{
//  echo " Yes, see if the URL is a valid URL and not a filename (security...)";
  
  $url     = $_GET["url"];
  $options = "";
 



  if (array_key_exists("HTTPS", $_SERVER) && $_SERVER["HTTPS"] == "on")
    $self = "https://$_SERVER[HTTP_HOST]$PHP_SELF";
  else
    $self = "http://$_SERVER[HTTP_HOST]$PHP_SELF";

  if (bad_url($url))
  {
    // Write the standard header...
    htmldoc_header("xxx");

    // Show a form for the URL...
    show_form();

    // Show an error message...
   // print("<p><b>Error!</b></p>\n"
   //      ."<p>\"$url\" is not a valid URL. Unable to process!</p>\n");

    // Show the standard footer...
    html_footer();
  }
  else if (array_key_exists("HTTP_REFERER", $_SERVER) &&
           $self != $_SERVER["HTTP_REFERER"])
  {
    // Write the standard header...
    htmldoc_header("xxx");

    // Show an error message - no remote linking!
  //  print("<h1>Error 10</h1>\n"
 //        ."<p><b>Error!</b></p>\n"
  //       ."<p> linked from a remote site "
  //       ."($_SERVER[HTTP_REFERRER]), which is not allowed.</p>\n");

    // Show the standard footer...
    html_footer();
  }
  else
echo "<BR><BR><BR><BR><BR><BR>URL ". $url."<BR>" ;
    topdf($url, "--header t.D --footer ./. --size letter --left 0.5in");
}
else
{
  // Write the standard header...
  htmldoc_header("PDF-o-matic");

  // Show a form for the URL...
  show_form();

  // Show the standard footer...
  html_footer();
}
// echo "<BR><BR><BR><BR><BR><BR>URL ". $url."<BR>" ;
// topdf($url, "--header t.D --footer ./. --size letter --left 0.5in");

?>
In hear i will receive the email also successfully ...but pdf file not attachement... it will say that pdf file

adobe reader says it couldn't open it because it is not a supported file type or the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded).

Please solve the problem plzzzzzzzzzzz
Last edited by Benjamin on Fri Aug 20, 2010 2:12 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php email with pdf attachment

Post by Jonah Bron »

Please wrap your code in [php][/php] tags. Can you narrow it down to where you think the problem is?
coolumanga
Forum Newbie
Posts: 9
Joined: Wed Aug 18, 2010 11:21 pm

Re: php email with pdf attachment

Post by coolumanga »

hmm yeah problem hear my email sending succesfully but pdf hearders attachment area having the problem
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

Re: php email with pdf attachment

Post by internet-solution »

Are you sure the PDF file is created successfully?

pdf-o-matic.php code in your post says no remote linking is allowed (which you are doing). Just by commenting out this bit of code, will not work! It will only suppress error message.

Code: Select all

// Show an error message - no remote linking!
  //  print("<h1>Error 10</h1>\n"
 //        ."<p><b>Error!</b></p>\n"
  //       ."<p> linked from a remote site "
  //       ."($_SERVER[HTTP_REFERRER]), which is not allowed.</p>\n");
coolumanga
Forum Newbie
Posts: 9
Joined: Wed Aug 18, 2010 11:21 pm

Re: php email with pdf attachment

Post by coolumanga »

Jonah Bron wrote:Please wrap your code in tags. Can you narrow it down to where you think the problem is?

i think it will not attaching the file to the email

preparing attachment area having the problem ................. plz give me a solution
Post Reply