2017-03-10 5 views
-2

PHPMailer를 사용하여 문의 양식을 구현하려고하는데 첨부 파일 업로드 필드를 보낼 수 없습니다. 연락처 양식이 작동하고 다른 모든 필드가 전송됩니다.PHPMailer 양식이 첨부 파일을 보내지 않음

나는 행운이없는 this tutorial을 따랐다.

this, thisthis과 같은 다양한 PHP 스크립트도 시도해 보았습니다.

가장 성공적으로 사용 될 것으로 보인다 내가 가진 현재 코드

이 하나입니다

<?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 위 코드를 업데이트 해 주셔서 감사합니다.하지만 첨부 파일을 아직받지 못했습니다. 대신 형식은 다음 오류를 반환합니다. "알림 : 정의되지 않은 색인 : 55 번째 줄에 /home/user/public_html/...l/form-contact.php의 inputBriefing" – 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"> 

는이 폼 태그를 시도