3

Trying to create a script where I can send an email with attachments. Everything works well except that when I don't add a file in the email I can still see an attachment with 0B and no name.

if(isset($_POST["my_send"])){


    $email_to = $_POST['my_email_to']; //required

    $email_from = "myemail@example.co.uk"; // required

    $subject = $_POST['my_subject']; // not required

    $comments = $_POST['write_my_email']; // required

    $email_message .= stripcslashes("$comments");

    $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
    $filename = $_FILES['file']['name'];

// create email headers

$headers = 'From: '.$email_from."\r\n".
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"_1_$boundary\"\r\n";

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

$message="This is a multi-part message in MIME format.

--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

$email_message

--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--";


mail($email_to, $subject, $message, $headers);  


        echo "<h5>Thanks, your email was sent successfully!</h5>";

}

what am I doing wrong? Any advice?

SNos
  • 3,430
  • 5
  • 42
  • 92

3 Answers3

2

Just exclude this when there is no attachment:

--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--_1_$boundary--

For example, only appending it to $message when the filename is not empty:

$message = "This is a multi-part message in MIME format.
    --_1_$boundary
    Content-Type: multipart/alternative; boundary=\"_2_$boundary\"

    --_2_$boundary
    Content-Type: text/plain; charset=\"iso-8859-1\"
    Content-Transfer-Encoding: 7bit

    $email_message";

if (!empty($filename))
{
   $message .= " --_1_$boundary
        Content-Type: application/octet-stream; name=\"$filename\" 
        Content-Transfer-Encoding: base64 
        Content-Disposition: attachment 

        $attachment
        --_1_$boundary--";
}
edwardmp
  • 6,339
  • 5
  • 50
  • 77
0
     $headers = array();
     $headers [] = 'MIME-Version: 1.0';
     $headers [] = 'Content-type: text/html; charset=utf-8';
     $headers [] = 'Reply-To: ' . $From;
     $headers [] = 'X-Mailer: PHP/' . phpversion();
     $headers [] = "Return-Path: " . $From;
     $headers [] = "Errors-To: " . $From;

     $header = implode("\r\n", $headers);
     $fifth = "-f $From";
     if (mail($empfaenger, $betreff, $Mailbody, $header, $fifth)) {};

I think the last header should not end with '\r\n' This code is by someone who posted it in the manual. check the manual, you find good code there.

PoohBear
  • 39
  • 6
0

If you want to send emails with attachmenst I recommend you to use PHP-Mailer library. It will make your life a lot easier.

All you need to do is include the class and instantiate the PHPMailer Object

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//And you will need to set properties for the object
?>

Checkout this PHP-Mailer Github Link. They have also given a simple example on how to implement

Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • Is a external library really necessary when it's perfectly possible using PHP's built in `mail()` function? – edwardmp Jun 13 '15 at 18:32
  • mail() function is reaaallyy for just simple stuff. Mail libraries hide a lot of low level stuff from the user, and provides simplistic way to make HTML emails, embedded images in mail, etc. – Abhinav Jun 14 '15 at 06:03