白色邮件,如果没有附件

时间:2016-12-06 17:01:40

标签: php email

我有一个部分工作的电子邮件代码(不附带phpmailer的东西),它发送带有附件的电子邮件,但是一旦我发送没有附件的文件,它确实发送但是带有白色,没有文字。

如果我只发送短信,为什么要发送白色电子邮件:

这是查询中的所有表单:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    // output data of each row
        echo "
        <form method='post' name='contact_form'
    action='/bedankt-verzenden?id=". $id ."' enctype='multipart/form-data'>
        Naar wie verzend u het voorstel:<br />
        <input style='width: 100%;' type='text' name='name' value='" . $row["firstname"] . "'><br/><br />
        Email:<br />
        <input style='width: 100%;' type='text' name='email' value='Jouw email'><br /><br />
        Onderwerp van het bericht:<br />
        <input style='width: 100%;' type='text' name='title' value='Offerte voor uw ". $row["stijlkeuken"] ." keuken'><br /><br />
        Bericht:<br />
        <textarea style='width: 100%; height: 200px;' name='message'></textarea><br /><br /><br />
        Bestand uploaden:<br />
        <input type='file' name='File_upload[]' size='40' multiple='multiple' ><br /><br />
        <input style='width: 100%; display: none;' type='text' name='email2' value='". $row["email"] ."'>
        <input type='submit' value='Verzenden' class='clicky-submitty' id='clicky-submitty2'>
        </form>";

}

然后这是处理程序(已编辑),也在查询中:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    $recipient_email    = $_POST['email2']; //recepient
    $from_email         = $_POST['email']; //from email using site domain.
    $subject            = $_POST['title']; //email subject line

    $sender_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
    $sender_message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); //capture message
    $attachments = $_FILES['File_upload'];

    $file_count = count($attachments['name']); //count total files attached
    $boundary = md5(time());
    $eol = "\r\n";

if(empty($_FILES['File_upload']))
{

    //header
        $headers = 'From: info@keukenaanbod.nl' . $eol; 
        $headers .= 'Reply-To: '. $sender_email . $eol;
        $headers .= 'Cc: ' . $sender_email . $eol;
        $headers .= "Message-ID: <" . time() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">" . $eol; 
        $headers .= "X-Mailer: PHP v" . phpversion() . $eol;
        $headers .= 'MIME-Version: 1.0' . $eol;
        $headers .= "Content-type:text/html; charset=UTF-8 boundary=\"" . $boundary . "\"";


                $body = "--". $boundary . $eol;
                $body .= "Content-type:text/plain; charset=iso-8859-1" . $eol;
                $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
                $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;

    }else{  

        //header
        $headers = 'From: info@keukenaanbod.nl' . $eol; 
        $headers .= 'Reply-To: '. $sender_email . $eol;
        $headers .= 'Cc: ' . $sender_email . $eol;
        $headers .= "Message-ID: <" . time() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">" . $eol; 
        $headers .= "X-Mailer: PHP v" . phpversion() . $eol;
        $headers .= 'MIME-Version: 1.0' . $eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\""; 

        //attachments
        for ($x = 0; $x < $file_count; $x++){      
            if(!empty($attachments['name'][$x])){

                //get file info
                $file_name = $attachments['name'][$x];
                $file_size = $attachments['size'][$x];
                $file_type = $attachments['type'][$x];

                //read file
                $handle = fopen($attachments['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)

                $body = "--". $boundary . $eol;
                $body .= "Content-type:text/plain; charset=iso-8859-1" . $eol;
                $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
                $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;
                $body .= "--" . $boundary . $eol;
                $body .= "Content-Type: " . $file_type. "; name=\"" . $file_name . "\"" . $eol;
                $body .= "Content-Transfer-Encoding: base64" . $eol; 
                $body .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"" . $eol . $eol;
                $body .= $encoded_content . $eol . $eol;
            }
        }
    }


     $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {      
        header('Location: /verzenden-gelukt?id='. $id .'');
    }else{
        die('Email kon helaas niet verzonden worden, u dient direct uit te zoeken wat er gaande is!');  
    }
}

2 个答案:

答案 0 :(得分:1)

首先,你可以做一个非常直接的重构,让你的代码更加干燥。如果你在我的回答中注意到我将所有的标题和正文都移出了if和else。这允许您一起删除所有其他内容。

而不是检查名称不是空的,你应该选择PHP将抛出的内置错误。

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){

    $recipient_email    = $_POST['email2']; //recepient
    $from_email         = $_POST['email']; //from email using site domain.
    $subject            = $_POST['title']; //email subject line

    $sender_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING);         //capture sender email
    $sender_message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); //capture message

    $boundary = md5(time());
    $eol = "\r\n";

    //header
    $headers = 'From: info@keukenaanbod.nl' . $eol; 
    $headers .= 'Reply-To: '. $sender_email . $eol;
    $headers .= 'Cc: ' . $sender_email . $eol;
    $headers .= "Message-ID: <" . time() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">" . $eol; 
    $headers .= "X-Mailer: PHP v" . phpversion() . $eol;
    $headers .= 'MIME-Version: 1.0' . $eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\""; 

    $body = "--". $boundary . $eol;
    $body .= "Content-type:text/plain; charset=iso-8859-1" . $eol;
    $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
    $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;

    if (isset($_FILES['File_upload'])){//Use isset on the file to verify it was uploaded
        //Check each files error
        foreach ($_FILES["File_upload"]["error"] as $key => $error){
            if ($error == UPLOAD_ERR_OK){//If the error was ok then do your thang
                //Use the $key get file info
                $file_name = $_FILES["File_upload"]['name'][$key];
                $file_size = $_FILES["File_upload"]['size'][$key];
                $file_type = $_FILES["File_upload"]['type'][$key];

                $handle = fopen($_FILES["File_upload"]['tmp_name'][$key], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
                $body .= "--" . $boundary . $eol;
                $body .= "Content-Type: " . $file_type. "; name=\"" . $file_name . "\"" . $eol;
                $body .= "Content-Transfer-Encoding: base64" . $eol; 
                $body .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"" . $eol . $eol;
                $body .= $encoded_content . $eol . $eol;
            }
        }    
    }


    $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if($sentMail){//output success or failure messages
        header('Location: /verzenden-gelukt?id='. $id .'');
    } else {
        die('Email kon helaas niet verzonden worden, u dient direct uit te zoeken wat er gaande is!');  
    }
}

答案 1 :(得分:0)

这是解决我的问题的代码,我现在可以发送带有和不带附件的php()中的电子邮件,并给我的电子邮件一个正文!

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    $recipient_email    = $_POST['email2']; //recepient
    $from_email         = $_POST['email']; //from email using site domain.
    $subject            = $_POST['title']; //email subject line

    $sender_email = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
    $sender_message = filter_var($_POST['message'], FILTER_SANITIZE_STRING); //capture message
    $attachments = $_FILES['File_upload'];

    $file_count = count($attachments['name']); //count total files attached
    $boundary = md5(time());
    $eol = "\r\n";

    if (isset($_FILES['File_upload'])){

        //attachments
        for ($x = 0; $x < $file_count; $x++){      
            if(!empty($attachments['name'][$x])){

                //get file info
                $file_name = $attachments['name'][$x];
                $file_size = $attachments['size'][$x];
                $file_type = $attachments['type'][$x];

                //read file
                $handle = fopen($attachments['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
            }
        }
    }

    //header
        $headers = 'From: info@keukenaanbod.nl' . $eol; 
        $headers .= 'Reply-To: '. $sender_email . $eol;
        $headers .= 'Cc: ' . $sender_email . $eol;
        $headers .= "Message-ID: <" . time() . " TheSystem@" . $_SERVER['SERVER_NAME'] . ">" . $eol; 
        $headers .= "X-Mailer: PHP v" . phpversion() . $eol;
        $headers .= 'MIME-Version: 1.0' . $eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary . "\""; 

        $body = "--". $boundary . $eol;
        $body .= "Content-type:text/plain; charset=iso-8859-1" . $eol;
        $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
        $body .= "Er is gereageerd op jouw keuken aanvraag, reageer op deze mail om in contact te komen:". $eol . $sender_message . $eol . $eol;
            if (isset($_FILES['File_upload'])){
        $body .= "--" . $boundary . $eol;
        $body .= "Content-Type: " . $file_type. "; name=\"" . $file_name . "\"" . $eol;
        $body .= "Content-Transfer-Encoding: base64" . $eol; 
        $body .= "Content-Disposition: attachment; filename=\"" . $file_name . "\"" . $eol . $eol;
        $body .= $encoded_content . $eol . $eol;
            }


     $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {      
        header('Location: /verzenden-gelukt?id='. $id .'');
    }else{
        die('Email kon helaas niet verzonden worden, u dient direct uit te zoeken wat er gaande is!');  
    }
}