【问题标题】:Converting PHP Contact Form to SMTP将 PHP 联系表转换为 SMTP
【发布时间】:2014-01-23 08:53:56
【问题描述】:

今晚在制作功能联系表时遇到了很多问题。搞砸了几个小时后,我终于发现我只能将 SMTP 与我的虚拟主机一起使用。

谁能告诉我如何填写表格?

这是我当前的 SMTP PHP 表单

$mail = new PHPMailer();

//Your SMTP servers details

$mail->IsSMTP();               // set mailer to use SMTP
$mail->Host = "localhost";  // specify main and backup server or localhost
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "enquiries@c(hidden)y.co.uk";  // SMTP username
$mail->Password = "******"; // SMTP password
//It should be same as that of the SMTP user

$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form

$mail->From = $mail->Username;  //Default From email same as smtp user
$mail->FromName = "Display Name";

$mail->AddAddress("enquiries@c(hidden)y.co.uk", "chapnolo"); //Email address where you wish to receive/collect those emails.

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = $_POST['subject'];
$message = "Name of the requestor :".$_POST['fullname']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Query :".$_POST['query'];
$mail->Body    = $message;

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
header("Location: $redirect_url");
}
?>

我需要我的表单具有以下功能,以便我检索所需的电子邮件正文。

$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$web = $_POST['web'];


$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries@c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";

我们将不胜感激

【问题讨论】:

  • 你遇到了什么错误?
  • 没有错误。顶部脚本有效。但它没有功能,因为我需要它来向我发送姓名、联系电话、电子邮件和消息正文

标签: php html forms email smtp


【解决方案1】:

Metexora - 所以你的代码没有任何问题,你只是没有告诉它向你发送姓名联系号码、电子邮件和消息正文 - 只需更改:

$message = "Name of the requestor :".$_POST['fullname']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Query :".$_POST['query'];

$message = "Name of the requestor :".$name." \r\n <br>Email Adrress :".$email." \r\n <br> Phone number :".$number."\r\n <br> Message: ".$message."\r\n <br> Website: ".$web;

(注意:小心,你在这里重新声明 $message,这不是真正推荐的做法,因为它们指的是不同的东西,用户消息和电子邮件消息,你应该只使用更改内容如果它仍然指向同一事物,则变量)

【讨论】:

  • 嗯,我已经这样做了,但是当我提交表单时,它只是将我带到一个空白屏幕,其 URL 为“c*(hidden)*gy.co.uk/mail.php
  • 这很令人惊讶。它由$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form 设置为重定向到根 URL(服务器主页),而不是脚本。也许尝试删除重定向,看看它现在是否会回显文本。如果您可以将您现在拥有的代码作为一个片段分享,我可以为您进一步查看,但要破译您第一次发送的两个片段有点困难。
  • stackoverflow.com/questions/20951198/… 我已经用我当前的代码发布了另一个。
猜你喜欢
  • 1970-01-01
  • 2017-02-03
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 2015-04-21
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多