【问题标题】:php contact form with attachment doesnt send without file带有附件的php联系表格不会在没有文件的情况下发送
【发布时间】:2017-10-08 17:32:52
【问题描述】:

对不起,我对 PHP 很陌生,可能很容易:
我收到了一个带附件的联系表,但是当我尝试在没有附件的情况下发送它时,它不会发送,但会出现我的错误消息。我不知道如何才能发送带附件和不带附件。这是我的基本代码

if(isset($_POST) && !empty($_POST)) {

// catch spam bots which never loaded contact form
if (!isset($_POST["p3"]) || $_POST["p3"] != "sometext") {  
    header("Location: contactform.php");
    exit;  
}  

// check whether the POST method was used
if ("POST" != getenv("REQUEST_METHOD")) {  
    header("Location: contactform.php");
    exit;  
} 

// check for user-agent and http-referer
if ("" == getenv("HTTP_USER_AGENT") || "" == getenv("HTTP_REFERER")) {
    header("Location: contactform.php");
    exit;  
} 

//  trick the spam bot into identifying itself using a honeypot
if (!empty($_POST["email"])) {
    exit;  
}

//if there is an attachment
if(!empty($_FILES['attachment']['name'])) {
    //store some varables
    $file_name = $_FILES['attachment']['name'];
    $temp_name = $_FILES['attachment']['tmp_name'];
    $file_type = $_FILES['attachment']['type'];

    //get the extension of the file
    $base = basename($file_name);
    $extension = substr($base, strlen($base)-4, strlen($base));

    // only thes file types will be allowed
    $allowed_extensions = array(".doc","docx",".pdf",".zip",".png","jpeg",".jpg",".gif",".txt","docm",".odt","xlsx","xlsm",".csv",".xml",".ods","tiff",".rtf","");

    // check that this file type is allowed
    if(in_array($extension,$allowed_extensions)){

        //mail essentials
        $from = $_POST['mail'];
        $to = "abcabacagagagag@gmx.de";
        $subject = "Anfrage über Website";
        $message = $_POST['quote'];

        // things you need
        $file = $temp_name;
        $content = chunk_split(base64_encode(file_get_contents($file)));
        $uid = md5(uniqid(time()));

        // standard mail headers
        $header = "From: ".$from."\r\n";
        $header .= "Reply-To: ".$to."\r\n";
        $header .= "MIME-Version: 1.0\r\n";

        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        $message .= "This is a multi-part message in MIME format.\r\n";

        // plain text part
        $message .= "--".$uid."\r\n";
        $message .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
        $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $message .= $message."\r\n\r\n";

        // file attachment
        $message .= "--".$uid."\r\n";
        $message .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
        $message .= "Content-Transfer-Encoding: base64\r\n";
        $message .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
        $message .= $content."\r\n\r\n";

        $msg = "";

        //send the mail 
        if (mail($to, $subject, $message, $header)) {
            $msg = "Ich habe Ihre Mail erhalten und melde mich in Kürze!";
        } else {
            $msg = "Nachricht konnte nicht gesendet werden. Bitte senden Sie mir Ihre Anfrage an bm-translations@email.de";
        }
    } else {
        $msg = "Dieser Dateityp kann über das Formular nicht gesendet werden. Bitte senden Sie mir Ihre Anfrage an bm-translations@email.de";
    }
} 

// if no file, send anyway (does not work) 
//elseif (empty($_FILES['attachment']['name']) || !(is_uploaded_file($_FILES['attachment']['name']))) {
//}
}

现在我尝试了此代码以允许不带附件发送,但不幸的是它不起作用。

  // if no file, send anyway
  elseif (empty($_FILES['attachment']['name']) ||(is_uploaded_file($_FILES['attachment']['name']))) {
  }

出了什么问题还是有更简单的方法?请在否决之前告诉我您还需要什么,因为可能不清楚。非常感谢!

【问题讨论】:

  • 如果上面的有附件,那么为什么不在另一种情况下简单地使用else(而不是elseif)呢?无论如何,您没有提供足够的信息让我们真正了解问题所在。尝试制作minimal reproducible example
  • 请发布完整代码
  • 谢谢,完整代码已贴出

标签: php sendmail contact-form


【解决方案1】:

我对您的代码进行了一些更改。 (主要是移动了一下。)

  • 我创建了一个变量 hasAttachment,顾名思义,如果帖子包含附件,则它包含一个布尔值。
  • 其余的更改是从您构建附件的 if 中提取邮件部分。
  • if (isset($_POST) && !empty($_POST)) {
        // catch spam bots which never loaded contact form
        if (!isset($_POST["p3"]) || $_POST["p3"] != "sometext") {
            header("Location: contactform.php");
            exit;
        }
    
        // check whether the POST method was used
        if ("POST" != getenv("REQUEST_METHOD")) {
            header("Location: contactform.php");
            exit;
        }
    
        // check for user-agent and http-referer
        if ("" == getenv("HTTP_USER_AGENT") || "" == getenv("HTTP_REFERER")) {
            header("Location: contactform.php");
            exit;
        }
    
        //  trick the spam bot into identifying itself using a honeypot
        if (!empty($_POST["email"])) {
            exit;
        }
    
        $hasAttachment = !empty($_FILES['attachment']['name']);
        $uid = md5(uniqid(time()));
    
        // mail essentials
        $from = $_POST['mail'];
        $to = "abcabacagagagag@gmx.de";
        $subject = "Anfrage über Website";
        $message = $_POST['quote'];
    
        // standard mail headers
        $header = "From: " . $from . "\r\n";
        $header.= "Reply-To: " . $to . "\r\n";
        $header.= "MIME-Version: 1.0\r\n";
        $header.= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"\r\n\r\n";
        $message.= "This is a multi-part message in MIME format.\r\n";
    
        // plain text part
        $message.= "--" . $uid . "\r\n";
        $message.= "Content-Type:text/plain; charset=iso-8859-1\r\n";
        $message.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        $message.= $message . "\r\n\r\n";
    
        // if there is an attachment
        if ($hasAttachment) {
            // store some varables
            $file_name = $_FILES['attachment']['name'];
            $temp_name = $_FILES['attachment']['tmp_name'];
            $file_type = $_FILES['attachment']['type'];
    
            // get the extension of the file
            $base = basename($file_name);
            $extension = substr($base, strlen($base) - 4, strlen($base));
    
            // only thes file types will be allowed
            $allowed_extensions = array(
                ".doc", "docx", ".pdf", ".zip", ".png", "jpeg", ".jpg", ".gif", ".txt", "docm", ".odt", "xlsx", "xlsm", ".csv", ".xml", ".ods", "tiff", ".rtf", ""
            );
    
            // check that this file type is allowed
            if (in_array($extension, $allowed_extensions)) {
    
                // things you need
                $file = $temp_name;
                $content = chunk_split(base64_encode(file_get_contents($file)));
    
                // file attachment
                $message.= "--" . $uid . "\r\n";
                $message.= "Content-Type: " . $file_type . "; name=\"" . $file_name . "\"\r\n";
                $message.= "Content-Transfer-Encoding: base64\r\n";
                $message.= "Content-Disposition: attachment; filename=\"" . $file_name . "\"\r\n";
                $message.= $content . "\r\n\r\n";
            }
        }
    
        $msg = "";
    
        // send the mail
        if (mail($to, $subject, $message, $header)) {
            $msg = "Ich habe Ihre Mail erhalten und melde mich in Kürze!";
        } else {
            $msg = "Nachricht konnte nicht gesendet werden. Bitte senden Sie mir Ihre Anfrage an bm-translations@email.de";
        }
    }
    

    【讨论】:

    • 非常感谢,没有出现错误消息。也谢谢你的描述!但是当我通过 xampp 尝试它时,它不会发送邮件。可能是因为设置。如果它真的发送邮件,你是否测试过它?
    • 没问题,没有我没有测试mail方法,因为这是PHP本身的一个函数。我想这是一个配置问题。我没有使用 xampp 的经验,但在快速谷歌搜索后我发现:how to configure xampp to send mail from localhost。希望对您有所帮助!
    【解决方案2】:
    <?php
    if(isset($_POST) && !empty($_POST)) {
    
    // catch spam bots which never loaded contact form
    if (!isset($_POST["p3"]) || $_POST["p3"] != "sometext") {  
        header("Location: contactform.php");
        exit;  
    }  
    
    // check whether the POST method was used
    if ("POST" != getenv("REQUEST_METHOD")) {  
        header("Location: contactform.php");
        exit;  
    } 
    
    // check for user-agent and http-referer
    if ("" == getenv("HTTP_USER_AGENT") || "" == getenv("HTTP_REFERER")) {
        header("Location: contactform.php");
        exit;  
    } 
    
    //  trick the spam bot into identifying itself using a honeypot
    if (!empty($_POST["email"])) {
        exit;  
    }
     //setting up the mail
    $from = $_POST['mail'];
    $to = "abcabacagagagag@gmx.de";
    $subject = "Anfrage über Website";
    $message = $_POST['quote'];
    $uid = md5(uniqid(time()));
    // standard mail headers
    $header = "From: ".$from."\r\n";
    $header .= "Reply-To: ".$to."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $message .= "This is a multi-part message in MIME format.\r\n";
    
    // plain text part
    $message .= "--".$uid."\r\n";
    $message .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
    $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $message .= $message."\r\n\r\n";
    $msg = "";
    
    //if there is an attachment
    if(!empty($_FILES['attachment']['name'])) {
        //store some varables
        $file_name = $_FILES['attachment']['name'];
        $temp_name = $_FILES['attachment']['tmp_name'];
        $file_type = $_FILES['attachment']['type'];
    
    //get the extension of the file
    $base = basename($file_name);
    $extension = substr($base, strlen($base)-4, strlen($base));
    
    // only thes file types will be allowed
    $allowed_extensions = array(".doc","docx",".pdf",".zip",".png","jpeg",".jpg",".gif",".txt","docm",".odt","xlsx","xlsm",".csv",".xml",".ods","tiff",".rtf","");
    
    // check that this file type is allowed
    if(in_array($extension,$allowed_extensions)){
    
       // file attachment
         // handling the content of the mail 
        $file = $temp_name;
        $content = chunk_split(base64_encode(file_get_contents($file)));
        $message .= "--".$uid."\r\n";
        $message .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
        $message .= "Content-Transfer-Encoding: base64\r\n";
        $message .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
        $message .= $content."\r\n\r\n";
    
        //send the mail 
        if (mail($to, $subject, $message, $header)) {
            $msg = "Ich habe Ihre Mail erhalten und melde mich in Kürze!";
        } else {
            $msg = "Nachricht konnte nicht gesendet werden. Bitte senden Sie mir Ihre Anfrage an bm-translations@email.de";
        }
    } else {
        $msg = "Dieser Dateityp kann über das Formular nicht gesendet werden. Bitte senden Sie mir Ihre Anfrage an bm-translations@email.de";
    }
    }
    
    else  mail($to, $subject, $message, $header);
    
    ?>
    

    我想在您的代码中为您指出几点

    1- 使用 elseif 不是正确的选项,你应该简单地使用 else,因为条件与你之前写的完全相反,更多信息请点击 here

    2-您在最后一部分发送的消息没有像您在我编写的代码中看到的那样声明我基本上将主要消息与附件分开并在代码开头声明它然后最后发送它。

    【讨论】:

      猜你喜欢
      • 2019-08-04
      • 2013-11-11
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多