-3

I am fairly new to php, and am unfamiliar with email headers. This is a function based on some code I came across / updated to use a file object. The result is a 'File Sent Successfully gets echoed, but the email never comes. I can only assume this has something to do with the email headers that this thing is generating. Hopefully somebody see's what's wrong here:

function mail_attachment ($from , $to, $subject, $message, $attachment){ 
    $fileatt = $attachment["tmp_name"];
    $fileatt_type = "application/octet-stream";
    $fileatt_name = $attachment["name"]; 
    $email_from = $from;
    $subject = "New Attachment Message"; 
    $email_subject = $subject;
    $email_to = $to;
    $headers = "From: ".$email_from;   
    $file = fopen($fileatt,'rb'); 
    $data = fread($file,filesize($fileatt)); 
    fclose($file);
    $semi_rand = md5(time()); 
    $mime_boundary = "Multipart_Boundary_x{$semi_rand}x"; 
    $headers .= "\nMIME-Version: 1.0\n" . 
                "Content-Type: multipart/mixed;\n" . 
                "boundary=\"{$mime_boundary}\"";
    $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . 
                "Content-Type:text/html; charset=\"iso-8859-1\"\n" . 
                "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; 
    $data = chunk_split(base64_encode($data)); 
    $email_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";   
    $ok = mail($email_to, $email_subject, $email_message, $headers);   
    if($ok) { echo "File Sent Successfully.";} 
    else { die("Sorry but the email could not be sent. Please go back and try again!"); } 
}
Jeff Lauder
  • 1,247
  • 8
  • 14
  • Stealing code then asking SO to fix it for you? – Madbreaks Jan 08 '13 at 20:14
  • You should use [PHP Mailer](http://phpmailer.worxware.com/) – SeanWM Jan 08 '13 at 20:15
  • @Sean Why? `mail` works just fine – Madbreaks Jan 08 '13 at 20:15
  • it's heavily modified, i'm not dumb, i just don't really know what's wrong with the headers. – Jeff Lauder Jan 08 '13 at 20:15
  • It's your decision to use cumbersome code like that. So please debug it yourself. Everybody else ought to use [PHPMailer or SwiftMailer](http://stackoverflow.com/questions/303783/phpmailer-vs-swiftmailer). – mario Jan 08 '13 at 20:15
  • @Madbreaks because it simplifies mail attachments. – SeanWM Jan 08 '13 at 20:16
  • You said you are "unfamiliar with email headers". Did you consider familiarizing yourself with them? – Madbreaks Jan 08 '13 at 20:16
  • I'm also not that familiar with php as I usually program in c#. I just didn't know if I was doing something blatantly incorrect, or if it was the headers. I wasn't trying to leach off you guys or anything. – Jeff Lauder Jan 08 '13 at 20:19
  • using php's mail() for complicated stuff (and even uncomplicated simple "hello world") emails just leads to premature baldness and jackets that close up in the rear. it should never be used in a production system. – Marc B Jan 08 '13 at 20:29

1 Answers1

0

Use phpMailer()

<?php 
require_once('phpmailer.php');

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport

try {
    $mail->AddReplyTo('email@example.com', 'First Last');
    $mail->AddAddress('John@example.com', 'John Doe');

    $mail->SetFrom('email@example.com', 'First Last');
    $mail->Subject  =  "Subject Line";
    $mail->AltBody    = "Alternate Text"; // optional, comment out and test

    $mail->WordWrap     =   50; // set word wrap    
    $mail->Body = "This is the body of the email";
    $mail->IsHTML(true); // send as HTML

    // Single or Multiple File Attachments
    $mail->AddAttachment('../path-to-file.pdf', 'File-Name.pdf');
    $mail->Send(); // Try to send email
    //echo "Message Sent OK<p></p>\n";
    } catch (phpmailerException $e) {
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
    } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
}
// end try


?>
adamdehaven
  • 5,890
  • 10
  • 61
  • 84