2017-03-10 44 views
-2

我試圖使用PHPMailer實現一個聯繫表單,但是我無法從發送的上傳字段中創建附件。聯繫表單可以工作,所有其他字段都會發送。PHPMailer表單不發送附件

我跟着this tutorial沒有運氣。

還嘗試了許多不同的PHP腳本,如this,thisthis其中之一。

當前的代碼我有,這似乎是最成功地使用是這個:

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', '1'); 
require_once 'phpmailer/PHPMailerAutoload.php'; 

// Attack #1 preventor - Spam Honeypot 

    if ($_POST["address"] != "") { 
     echo "SPAM HONEYPOT"; 
     exit; 
    } 

    // Attack #2 preventor - Email header injection hack preventor 

    foreach($_POST as $value) { 
     if(stripos($value, 'Content-Type:') !== FALSE) { 
      echo "There was a problem with the information you entered."; 
      exit; 
     } 
    } 

if (isset($_POST['inputNome']) && isset($_POST['inputEmail']) && isset($_POST['inputMensagem'])) { 

    //check if any of the inputs are empty 
    if (empty($_POST['inputNome']) || empty($_POST['inputEmail']) || empty($_POST['inputMensagem'])) { 
     $data = array('success' => false, 'message' => 'Preencha todos os campos requeridos.'); 
     echo ($data); 
     exit; 

    } 

    //create an instance of PHPMailer 
    $mail = new PHPMailer(); 
     // Set up SMTP 
    $mail->IsSMTP();    // Sets up a SMTP connection 
    $mail->SMTPAuth = true;   // Connection with the SMTP does require authorization  
    $mail->SMTPSecure = "ssl";  // Connect using a TLS connection 
    $mail->Host = "************"; //Gmail SMTP server address 
    $mail->Port = 465; //Gmail SMTP port 
    $mail->Encoding = '7bit'; 

    // Authentication 
    $mail->Username = "*************"; // Your full Gmail address 
    $mail->Password = "*********"; // Your Gmail password 

    $mail->CharSet = 'UTF-8'; 

    // Compose 
    $mail->SetFrom($_POST['inputEmail'], $_POST['inputNome']); 
    $mail->AddReplyTo($_POST['inputEmail'], $_POST['inputNome']); 
    $mail->Subject = "My Company - " . $_POST['inputAssunto'];  // Subject (which isn't required) 
    $mail->AddAddress('[email protected]'); //recipient 

    //Add attachment 
    $mail->addAttachment($_FILES['inputBriefing']); 
     if (isset($_FILES['inputBriefing']) && 
      $_FILES['inputBriefing']['error'] == UPLOAD_ERR_OK) { 
      $mail->addAddress($_FILES['inputBriefing']['tmp_name'], 
           $_FILES['inputBriefing']['name']); 
    } 
    $mail->Body = "Nome: " . $_POST['inputNome'] . "\r\nEmail: " . $_POST['inputEmail'] . "\r\nTelefone: " .$_POST['inputTelefone'] . "\r\nAssunto: " . $_POST['inputAssunto'] . "\r\nMensagem: " . stripslashes($_POST['inputMensagem']); 


    if(!$mail->send()) 
{ 
    echo 'An error has occurred.'; 
    exit; 
} 

echo 'Message successfully sent'; 
} 

?> 

而且形式:

<form id="contact" method="post" action="<?php echo get_template_directory_uri(); ?>/form-contact.php"> 
    <div class="form-group"> 
     <label for="inputNome">Nome Completo *</label> 
     <input type="text" id="inputNome" name="inputNome" required class="form-control" placeholder="Nome Completo"> 
    </div> 
    <div class="form-group"> 
     <label for="inputEmail">E-mail *</label> 
     <input type="email" id="inputEmail" name="inputEmail" required class="form-control" placeholder="Digite seu e-mail"> 
    </div> 
    <div class="form-group"> 
     <label for="inputTelefone">Telefone</label> 
     <input type="tel" id="inputTelefone" name="inputTelefone" class="form-control" placeholder="Telefone"> 
    </div> 
    <div class="form-group"> 
     <label for="inputAssunto">Assunto</label> 
     <select class="form-control" id="inputAssunto" name="inputAssunto" > 
      <option disabled selected value> -- Selecione -- </option> 
      <option value="Orçamento">Orçamento</option> 
      <option value="Hospedagem">Hospedagem</option> 
      <option value="Dúvidas">Dúvidas</option> 
      <option value="Informações">Informações</option> 
      <option value="Outro">Outro</option> 
     </select> 
    </div> 
    <div class="form-group"> 
     <label for="inputBriefing">Briefing</label> 
     <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> 
     <input type="file" id="inputBriefing" name="inputBriefing"> 
     <p class="help-block">Se preferir adicione seus arquivos (.pdf, .docx ou .zip)</p> 
    </div> 
    <!--Hpam Sponypot --> 
    <div class="form-group" style="visibility: hidden">  
     <label for="address">Address</label> 
     <input type="text" name="address" id ="address"> 
     <p> Humans, do not fill out this form! </p> 
    </div> 
    <div class="form-group"> 
     <label for="inputMensagem">Mensagem *</label> 
     <textarea class="form-control" rows="3" id="inputMensagem" name="inputMensagem" required placeholder="Escreva sua mensagem"></textarea> 
    </div> 
    <p><small>* Campos obrigatórios.</small></p> 
    <button type="submit" class="btn btn-info">Enviar</button> 
</form> 

我使用的是HostGator的共享主機。

+0

請[閱讀手冊](http://php.net/manual/en/features.file-upload.post-method.php)你是不是引用文件在您嘗試添加附件 – RiggsFolly

+0

@RiggsFolly謝謝我更新了我的代碼上面,但我仍然沒有得到附件。相反,表單返回以下錯誤:「注意:未定義的索引:inputBriefing in /home/user/public_html/...l/form-contact.php on line 55」 – mdeotti

+0

@RiggsFolly正如我所說的問題,我是跟着這個教程,這就是我認爲這是做的:http://www.codesynthesis.co.uk/tutorials/attach-a-file-to-an-email-in-php-using-phpmailer。 – mdeotti

回答

0

您試圖將文件添加爲新的目標地址,而不是附件。

請參閱下面的建議修正案。

//Add attachment 
// $_FILES['inputBriefing'] is an array not the file 
// adding the file as an attachment before checking for errors is a bad idea also 
//$mail->addAttachment($_FILES['inputBriefing']); 
if (isset($_FILES['inputBriefing']) && $_FILES['inputBriefing']['error'] == UPLOAD_ERR_OK) { 

    // this is attempting to add an address to send the email to 
    //$mail->addAddress($_FILES['inputBriefing']['tmp_name'],$_FILES['inputBriefing']['name']); 
    $mail->addAttachment($_FILES['inputBriefing']['tmp_name'], 
          $_FILES['inputBriefing']['name']); 
} 
0

您必須在這裏添加enctype形式標籤。

<form id="contact" method="post" action="<?php echo get_template_directory_uri(); ?>/form-contact.php" enctype="multipart/form-data"> 

試試這個表單標籤