Im attempting to get a MIME email with a PDF attachment and plain text body to be sent via my php script which is triggered from the command line. Odd thing is this will work quite happily when the script is ran from a browser, obviously no output is sent to the browser. When run from the CLI script, it manages to send an email, but the from address is wrong, instead being mydomain@dns1.host.com and the whole email including headers and base64 encoded attachment is shown IN the email body, if that makes sense
<?php
$eol="\n";
# File for Attachment
//$f_name="../../letters/".$letter; // use relative path OR ELSE big headaches. $letter is my file for attaching.
$filename = "6_1146305804.pdf";
$f_name="/home/path/public_html/certificates/".$filename;
//
$handle=fopen($f_name, 'rb');
$f_contents=fread($handle, filesize($f_name));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
$f_type=filetype($f_name);
fclose($handle);
# To Email Address
// $emailaddress = $item[3];
$emailaddress = "customer@hotmail.com";
# Message Subject
$emailsubject = "Free PDF";
# Common Headers
$headers .= 'From: Company Title <myemail@mydomain.co.uk>'.$eol;
$headers .= 'Reply-To: Company Title <myemail@mydomain.co.uk>'.$eol;
$headers .= 'Return-Path: Company Title <myemail@mydomain.co.uk>'.$eol; // these two to set reply address
$headers .= "Message-ID: <".$now." TheSystem@www.mydomain.co.uk>".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$mime_boundary=md5(time());
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol;
$msg = "";
# Text Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "This is a multi-part message in MIME format.".$eol.$eol;
$msg .= "If you are reading this, please update your email-reading-software.".$eol;
$msg .= "+ + Text Only Email + +".$eol;
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.
# SEND THE EMAIL
/*
ini_set(sendmail_from,'myemail@mydomain.co.uk'); // the INI lines are to force the From Address to be used !
mail($item[3], $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
*/
mail($emailaddress, $emailsubject, $msg, $headers);
?>
I commented out the sendmail_from part as i believe that is only for win servers and it seemed to be causing some problems. Is there something I am missing sending mail() form the command line in a PHP script?
It is bieng called by a forward setup from within cPanel;
"|/usr/bin/php -q /home/mydomain/public_html/cert/trim.php"
Is it perhaps something to do with the -q option? Do you need some more information?