will35010 Posted August 10, 2009 Share Posted August 10, 2009 I'm creating an online card script for a non-profit hospital. I have the html part complete where the user picks the card and types their info. It then sends it to this script as post data. If the user picks print, I want it to email the person at the facility for printing and delivery. If the user picks email, I want it to email the patient using the form data email info. On both emails, I want it to attach the image the user has chosen. I'm trying to attach an image from a file to an email based on the post data received. For example if the user picks the baby card ($baby), it would attach the jpeg from ../cardtemplates/baby.jpg and email that as an attachment. I'm pretty sure I can get the email part working, but I'm lost on how to attach an image from a file. I googled and everything I found seemed very complex or the wrong file types. Any help would be greatly appreciated. <?php /** * @author Will Morris * @copyright 2009 */ //assign varible names to POST data //baby cards $baby = $_POST['baby']; $baby2 = $_POST['baby2']; $baby3 = $_POST['baby3']; $babyangel = $_POST['baby-angel']; $babyflower = $_POST['baby-in-flower']; $daughter1 = $_POST['daughter1']; $newkid1 = $_POST['new-kid1']; $twins2 = $_POST['twins-2']; //get well cards $getwell = $_POST['getwell']; $getwellsoon = $_POST['getwellsoon-1']; $recovery2 = $_POST['recovery-2']; $footprints = $_POST['footprints']; //thinking of you card $thinkingofyou = $_POST['thinking-of-you2']; //print or email $print - $_POST['print']; $email = $_POST['email']; //form info $sendername = $_POST['yourname']; $fromemail = $_POST['fromemail']; $toemail = $_POST['toemail']; $fname = $_POST['fname']; $lname = $_POST['lname']; $room = $_POST['room']; $message = $_POST['message']; //attach image to email if (isset($baby)) { } elseif (isset($baby2)) { } if (isset($print)) { //email info for print $to = '[email protected]' . ', '; $to = '[email protected]'; $subject = 'Patient E-Card'; $message = 'hello'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); } else { $to = '$toemail'; $subject = 'Patient E-Card'; $message = 'hello'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); } ?> Link to comment https://forums.phpfreaks.com/topic/169653-solved-php-mail-help/ Share on other sites More sharing options...
The Little Guy Posted August 10, 2009 Share Posted August 10, 2009 Something like this: READ THE COMMENTS <?php $random_hash = md5(date('r', time())); $mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n"; // Additional headers $headers .= 'To: '.$row['personal'].' <'.$row['from'].'>' . "\r\n"; $headers .= 'From: '.$_SESSION['first'].' '.$_SESSION['last'].' <'.$row['to'].'>' . "\r\n"; // File Info $message = $_POST['message']."\n\n". "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Next line is the name ofo your file: $name = 'fileName.jpg'; // Following line is the location of your file: $fileatt = "../attach/".$name; $fileatt_type = "application/octet-stream"; // File Type $fileatt_name = $name; // Filename that will be used for the file as the attachment $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $data = chunk_split(base64_encode($data)); $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}\n"; unset($data); unset($file); unset($fileatt); unset($fileatt_type); unset($fileatt_name); mail($_POST['to'],$_POST['subject'],$message,$headers); ?> Link to comment https://forums.phpfreaks.com/topic/169653-solved-php-mail-help/#findComment-895012 Share on other sites More sharing options...
will35010 Posted August 10, 2009 Author Share Posted August 10, 2009 How do I make it work with my script? Something like this: READ THE COMMENTS <?php $random_hash = md5(date('r', time())); $mime_boundary = "==Multipart_Boundary_x{$random_hash}x"; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-Type: multipart/mixed; boundary="'.$mime_boundary.'"' . "\r\n"; // Additional headers $headers .= 'To: '.$row['personal'].' <'.$row['from'].'>' . "\r\n"; $headers .= 'From: '.$_SESSION['first'].' '.$_SESSION['last'].' <'.$row['to'].'>' . "\r\n"; // File Info $message = $_POST['message']."\n\n". "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Next line is the name ofo your file: $name = 'fileName.jpg'; // Following line is the location of your file: $fileatt = "../attach/".$name; $fileatt_type = "application/octet-stream"; // File Type $fileatt_name = $name; // Filename that will be used for the file as the attachment $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $data = chunk_split(base64_encode($data)); $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}\n"; unset($data); unset($file); unset($fileatt); unset($fileatt_type); unset($fileatt_name); mail($_POST['to'],$_POST['subject'],$message,$headers); ?> Link to comment https://forums.phpfreaks.com/topic/169653-solved-php-mail-help/#findComment-895040 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.