skip to Main Content

I have a php script that sends out emails with attachments once people have submitted some information. I receive these emails in my gmail inbox with no problem. However, when I use my personal email address or my work email address the email is never delivered. Is this a problem with my script (below) or some setting I have on the server? I think it might be a problem with the headers, but everytime I change the headers, they break the email and everything appears in the message body. Does anyone know how to fix this?
The server is the clients managed linux server with a plesk control panel so I don’t have access to the php ini file.

//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Email with Attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with rn
$mime_boundary = "<<<--==+X[".md5(time())."]";

$path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
$fileContent =  chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));


$headers .= "From: [email protected] <[email protected]>rn";  

$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: multipart/mixed;rn";
$headers .= " boundary="".$mime_boundary.""";




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

$message .= "rn";
$message .= "--".$mime_boundary."rn";

$message .= "Content-Type: text/plain; charset="iso-8859-1"rn";
$message .= "Content-Transfer-Encoding: 7bitrn";
$message .= "rn";
$message .= "Email content and what not: rn";
$message .= "This is the file you asked for! rn";
$message .= "--".$mime_boundary."" . "rn";

$message .= "Content-Type: application/octet-stream;rn";
$message .= " name="CTF-brochure.pdf"" . "rn";
$message .= "Content-Transfer-Encoding: base64 rn";
$message .= "Content-Disposition: attachment;rn";
$message .= " filename="CTF_brochure.pdf"rn";
$message .= "rn";
$message .= $fileContent;
$message .= "rn";
$message .= "--".$mime_boundary."rn";

//send the email
$mail_sent = mail($to, $subject, $message, $headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";

3

Answers


  1. Check your spam folder on the other accounts. Sometimes, mail can end up in spam if it triggers too many warnings for whatever spam-filtering software the e-mail account provider is using.

    Login or Signup to reply.
  2. Try this multipart alternative version. It accepts both plaintext and html messages and allows the mail client to choose which to display.

    
    //define the receiver of the email
    $to = '[email protected]';
    //define the subject of the email
    $subject = 'Email with Attachment';
    //create 2 boundary strings. They must be unique
    $boundary1 = rand(0,9)."-"
                                .rand(10000000000,9999999999)."-"
                                .rand(10000000000,9999999999)."=:"
                                .rand(10000,99999);
    $boundary2 = rand(0,9)."-".rand(10000000000,9999999999)."-"
                                .rand(10000000000,9999999999)."=:"
                                .rand(10000,99999);
    
    $path = $_SERVER['DOCUMENT_ROOT'].'/two/php/';
    $fileContent =  chunk_split(base64_encode(file_get_contents($path.'CTF_brochure.pdf')));
    
    $txt_message = "Email content and what not: rn";
    $txt_message .= "This is the file you asked for! rn";
    
    $html_message = "Email content and what not: <br />";
    $html_message .= "This is the file you asked for! ";
    
    $headers     =<<<AKAM
    From: [email protected] <[email protected]>
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
        boundary="$boundary1"
    AKAM;
    
    $attachment = <<<ATTA
    --$boundary1
    Content-Type: application/octet-stream;
        name="CTF_brochure.pdf"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
        filename="CTF_brochure.pdf"
    
    $fileContent
    
    ATTA;
    
    $body = <<<AKAM
    This is a multi-part message in MIME format.
    
    --$boundary1
    Content-Type: multipart/alternative;
        boundary="$boundary2"
    
    --$boundary2
    Content-Type: text/plain;
        charset=ISO-8859-1;
    Content-Transfer-Encoding: 7bit
    
    $txt_message
    --$boundary2
    Content-Type: text/html;
        charset=ISO-8859-1;
    Content-Transfer-Encoding: 7bit
    
    $html_message
    
    --$boundary2--
    
    $attachment
    --$boundary1--
    AKAM;
    
    //send the email
    $mail_sent = @mail($to, $subject, $body, $headers);
    //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
    echo $mail_sent ? "Mail sent" : "Mail failed";
    
    Login or Signup to reply.
  3. The mail() function is very useful except for when you had to deal with headers. Most e-mails will be rejected by spam bots without proper headers.

    I would recommending using the PHPMailer class and have that do all the work. Making one yourself is a pain and not recommended.

    https://github.com/PHPMailer/PHPMailer

    This comes with many wonderful features and will save your life of all these problems 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search