【问题标题】:PHPMailer sending e-mail without form dataPHPMailer 发送没有表单数据的电子邮件
【发布时间】:2019-02-17 17:39:41
【问题描述】:

我正在尝试发送一封带有联系表格的电子邮件,但它没有显示数据。 我在这里尝试了一些答案,但无法正常工作。

我的代码:

<form class="form form-container method="post" action="assets/send_form_email.php">
    <div class="left">
      <input name="f_name" type="text" id="f_name" placeholder="Nome*" required />  
      <input name="f_email" type="email" id="f_email" placeholder="Email*" required />  
      <input name="f_phone" type="text" id="f_phone" required="required" maxlength="15"  placeholder="Telefone*" />  
      <textarea name="f_msg" id="f_msg" placeholder="Mensagem" rows="6"></textarea>  
    </div>

    <div class="right">
      <input name="f_cnpj" type="text" id="f_cnpj" placeholder="CNPJ" style="font-weight: 700;" required />  
      <input name="f_sector" type="text" id="f_sector" placeholder="Setor da empresa" style="font-weight: 700;" required />  
      <input name="f_faturamento" type="text" id="f_faturamento" placeholder="Faturamento médio" style="font-weight: 700;" required /> 
      <input name="f_valorMedio" type="text" id="f_valorMedio" placeholder="Valor médio de duplicatas" style="font-weight: 700;" required /> 
      <button type="submit" class="botaoContato">Enviar</button>
    </div>  
  </form>

这里是 PHP:

<?php
$nome = $_POST['f_name'];
$email = $_POST['f_email'];
$telefone = $_POST['f_phone'];
$cnpj = $_POST['f_cnpj'];
$setor = $_POST["f_sector"];
$faturamento = $_POST['f_faturamento'];
$valorMedio = $_POST['f_valorMedio'];
$msg = $_POST["f_msg"];

$mensagem = "Nome: '.$nome.' <br> 
            Email: '$email.' <br> 
            Telefone: '.$telefone.' <br><br> 
            CNPJ: '.$cnpj.' <br> 
            Setor: '.$setor.' <br> 
            Faturamento: '.$faturamento.' <br> 
            Valor Medio: '.$valorMedio.' <br> 
            Mensagem: '.$msg.' <br>";

require_once("class.phpmailer.php");

$mail = new PHPMailer();
$mail->SMTPDebug = 2;

$mail->IsSMTP();
$mail->Host = "smtp.****.com.br";
$mail->SMTPAuth = true; 
$mail->Username = 'contato@****.com.br'; 
$mail->Password = '********'; 

$mail->From = "contato@*****.com.br"; 
$mail->Sender = "contato@****.com.br"; 
$mail->FromName = "****"; 

$mail->AddAddress('contato@*****.com.br');

$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';

$mail->Subject = "Contato Site "; 
$mail->Body = $mensagem;
$mail->AltBody = $mensagem;

$enviado = $mail->Send();

$mail->ClearAllRecipients();
$mail->ClearAttachments();


if ($enviado) {
echo "E-mail enviado com sucesso!";
} else {
echo "Não foi possível enviar o e-mail.

";
echo "Informações do erro:
" . $mail->ErrorInfo;
}

我尝试删除变量并将其直接添加到 $mail->Body 但它不起作用。我不太了解 PHP,所以我依赖这里的帖子或一些教程,但我找不到错误。 :(

【问题讨论】:

    标签: php forms email phpmailer


    【解决方案1】:

    当您提出这样的问题时,最重要的是准确描述如何它不起作用 - 我们无法猜测。

    首先,您使用的是非常旧的 PHPMailer 版本,所以 get the latest,并且您的代码基于 the contact form example provided with PHPMailer

    您只发送纯文本内容,所以这样做:

    $mail->isHTML(false);
    $mail->Body = $mensagem;
    

    执行此操作时无需设置AltBody

    如果您的消息已到达,但未包含您期望的内容,请在调用 send() 之前执行此操作,以确认脚本收到了哪些参数,以及您要求它发送什么:

    var_dump($_POST, $mail->Body);
    

    你有SMTPDebug = 2,所以你应该看到调试输出。如果您从send() 获得false 返回值,则任何错误都应在$mail-&gt;ErrorInfo 中 - 您将在我链接到的示例代码中看到如何使用它。

    如果您没有看到 SMTP 调试输出,这意味着它根本无法连接,在这种情况下,您应该阅读the troubleshooting guide,它告诉您如何诊断连接问题。通常的原因是您的托管服务提供商可能会阻止出站 SMTP,或者您的 CA 证书已过期。

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 2014-07-15
      • 2014-07-09
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 2018-06-01
      • 2013-10-05
      • 1970-01-01
      相关资源
      最近更新 更多