PHPMailer表单不发送附件

时间:2017-03-10 01:32:50

标签: php forms phpmailer attachment

我尝试使用PHPMailer实现联系表单,但我无法通过上传字段发送附件。联系表格确实有效,所有其他字段都已发送。

我跟着this tutorial没有运气。

还尝试了许多不同的PHP脚本,例如thisthisthis等。

我目前使用的代码似乎是最成功的代码:

<?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('name@myemail.com'); //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上使用共享主机。

2 个答案:

答案 0 :(得分:0)

您试图将该文件添加为新的目标地址而不是附件。

请参阅下面的建议修订。

charCodeAt()

答案 1 :(得分:0)

您必须在此表格标签中添加enctype。

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

试试此表单标记