【问题标题】:PHP HTML Contact us form issuePHP HTML 联系我们表单问题
【发布时间】:2021-01-22 08:14:47
【问题描述】:

我正在使用 html (bootstrap4) 联系我们页面并尝试使其动态化。使用 pear mail 使用 php 测试脚本进行服务器测试。

但是当尝试进行动态调用并在电子邮件中调用归档时出现问题

任何人都可以查看下面的 php 脚本并告诉我问题出在哪里

我如何在这里调用这部分:$email_subject?电子邮件正文?和电子邮件?

<form id="contact-form" method="post" action="test.php">
  <div class="row">
    <!-- Name -->
    <div class="col-md-6 mb-2">
      <input type="text" name="Full_Name" class="form-control" placeholder="Firstname *" required="required" data-error="Firstname is required.">
    </div>
    <!-- Email -->
    <div class="col-md-6 mb-2">
      <input type="email" name="email" class="form-control" placeholder="email *" required="required" data-error="Valid email is required.">
    </div>
    <!-- subject -->
    <div class="col-md-12 mb-2">

      <input type="text" name="subject" class="form-control" placeholder="subject *" required="required" data-error="Valid subject is required.">
    </div>
    <!-- Message -->
    <div class="col-md-12 mb-2">
      <textarea name="message" class="form-control" placeholder="Message  *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
    </div>
    <!-- Submit Button -->
    <div class="col-12 text-right">
      <input type="submit" class=" form-control btn btn-success btn-send" value="Send message">
    </div>
  </div>
</form>
<!-- Email -->
<div class="col-md-6 mb-2"> <input type="email" name="email" class="form-control" placeholder="email *" required="required" data-error="Valid email is required."> </div>

<?php

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];

$host = "*******"; <!--i will use my hosting server url-->
$username = "*******"; <!--i will use my email-->
$password = "*****";  <!--i will use here my email pss-->
$port = "2525";
$to = "******";  <!--i will use the email here-->
$email_from = "*****";  <!--how to call email from here by input that i defined above in php file->
$email_subject = "******" ; <!--how to call sujectl from here by input that i defined above in php file->
$email_body = "*****";    <!--how to call message from here by input that i defined above in php file->
$email_address = "*****";   

$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, 'Message' => $message, Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);


if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

【问题讨论】:

  • 这里没有JS。删除了标签。请不要标记垃圾邮件
  • 你的意思是不是:$email_from = $email; 等等?其中 $email 是您从 $_POST["email"]; 获得的已清理地址
  • 我已经定义了就像你所说的那样“$email_from = $email;”但是在提交后没有收到任何电子邮件,甚至没有收到我的联系我们页面 trun white 没有消息显示并且没有收到收件箱..但是当我使用测试脚本代替 $email_form = "xyg@gmail.com" 并且与虚拟文本相同时对于其他字段表单的工作和接收电子邮件收件箱不知道是什么问题。
  • 请出示表格。也许您不发布它,或者您使用 ID 而不是 NAME
  • @mplungjan,感谢您在这里支持并尝试解决问题。我发现了错误,现在它可以工作了

标签: php html forms


【解决方案1】:

我已经对代码进行了轻微修改,这可能会有所帮助,如果您使用 gmail 发送电子邮件,请参阅本文的最后一点:https://help.dreamhost.com/hc/en-us/articles/216140597-How-do-I-send-PHP-mail-via-SMTP-

<?php

error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);

require_once "Mail/Mail-1.4.1/Mail.php";

$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];

$host = "*******"; <!--i will use my hosting server url-->
$username = "*******"; <!--i will use my email-->
$password = "*****";  <!--i will use here my email pss-->
$port = "2525";

$to = "******";  <!--i will use the email here-->
$reply_to = "*****";   

// Please note the change to the headers
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, Reply-To' => $reply_to);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $message);


if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

【讨论】:

  • 您的意思是您只使用发布的变量而不是中间变量。为什么这会改善不发送电子邮件的情况?
  • 我还将标题更改为正确的格式,并告知他使用 gmail 所需的格式
  • 指出这一点很有用。我看不出你还有什么变化
  • Jak,很抱歉,我也照你说的做了。
  • @Jak,感谢您在这里支持并尝试解决问题。我发现了错误,现在它完美地为我工作。
【解决方案2】:

在仔细阅读代码并对其工作进行轻微优化后,我将其粘贴在这里.. 可能对遇到与 html/php/pear 相同问题的人有所帮助

<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$host = "mail.example.com"; 
$username = "xyz@example.com";
$password = "abc";
$port = "25";
$to = "xyz@example.com";
$reply_to = "xyz@example.com"; 
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, 'Reply-To' => $reply_to);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

【讨论】:

  • 如果您包含对您所做更改的解释,将对未来的读者有所帮助。
猜你喜欢
  • 2021-04-26
  • 2023-01-05
  • 2016-03-26
  • 2018-08-28
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
相关资源
最近更新 更多