【问题标题】:PHPMailer form not sending attachmentsPHPMailer表单不发送附件
【发布时间】:2017-03-10 01:32:49
【问题描述】:

我正在尝试使用 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 上使用共享主机。

【问题讨论】:

  • read the manual您在尝试添加附件时没有引用该文件
  • @RiggsFolly 谢谢我更新了上面的代码,但我仍然没有收到附件。相反,表单返回以下错误:“注意:未定义的索引:inputBriefing in /home/user/public_html/...l/form-contact.php on line 55”
  • @RiggsFolly 正如我在问题上所说的那样,我正在关注本教程,这就是我认为这样做的目的:codesynthesis.co.uk/tutorials/…
  • @RiggsFolly 再次感谢您在没有检查我的消息来源和嘲笑我而不是提供任何有效帮助的情况下标记我。
  • 我确实检查了你的来源

标签: php forms phpmailer attachment


【解决方案1】:

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

建议修改见下文。

//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']);
 }

【讨论】:

    【解决方案2】:

    你必须像这样在你的表单标签中添加 enctype。

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

    试试这个表单标签

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2012-07-30
      • 1970-01-01
      • 2015-06-06
      相关资源
      最近更新 更多