0

I am attempting to send an attachment via email using PHP. I have been following some online tutorial pages and my mail() function is returning true. However, the email is not sending. I have been examining my headers and body and can't pinpoint what I am doing wrong.

$from_email = 'admin@felixxiao.com'; //sender email
$recipient_email = 'admin@felixxiao.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body

//get file details we need
$file_tmp_name    = $_FILES['data']['tmp_name'];
$file_name        = $_FILES['data']['name'];
$file_size        = $_FILES['data']['size'];
$file_type        = $_FILES['data']['type'];
$file_error       = $_FILES['data']['error'];

$user_email = filter_var("admin@felixxiao.com", FILTER_SANITIZE_EMAIL);

if($file_error>0)
{
    echo 'upload error';
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));


    $boundary = md5("sanwebe"); 
    //header
    $headers = "MIME-Version: 1.0\r\n"; 
    $headers .= "From: ".$from_email."\r\n"; 
    $headers .= "Reply-To: ".$user_email."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

    echo $headers;
    //plain text 
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
    $body .= chunk_split(base64_encode($message)); 

    //attachment
    $body .= "--$boundary\r\n";
    $body .="Content-Type: ".$file_type."; name=".$file_name."\r\n";
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
    $body .="Content-Transfer-Encoding: base64\r\n";
    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 

    echo $body;
    $body .= $encoded_content; 

$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{       
    echo 'Thank you for your email';
}else{
    echo 'Could not send mail! Please check your PHP mail configuration.';  
}

When I print out my $headers and $body, this is what prints out:

MIME-Version: 1.0
From: admin@felixxiao.com 
Reply-To: admin@felixxiao.com
Content-Type: multipart/mixed; boundary = 8de2a431c506316063ec3a4044192e46

--8de2a431c506316063ec3a4044192e46
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: base64

VGhpcyBpcyBib2R5IG9mIHRoZSBtZXNzYWdl
--8de2a431c506316063ec3a4044192e46
Content-Type: application/pdf; name=blob
Content-Disposition: attachment; filename=blob
Content-Transfer-Encoding: base64
X-Attachment-Id: 32091

Thank you for your email
felix_xiao
  • 1,608
  • 5
  • 21
  • 29

1 Answers1

1

The @ before the @mail call suppresses the error (see this). (It sets the error to 0). Remove the @ and try again.

user3791775
  • 426
  • 3
  • 5