【问题标题】:Send PHP form via SMTP通过 SMTP 发送 PHP 表单
【发布时间】:2019-01-29 19:14:15
【问题描述】:

我发现了一些与我的查询类似的问题,但没有一个完全符合我的情况。

我有一个基本的 mail() PHP 设置用于我网站上的联系表单,但事实证明这在我的托管服务器上被阻止以避免垃圾邮件/滥用。

网络主机建议使用 SMTP 作为解决方法,但他们链接到的教程似乎没有为我提供有效的解决方案!

这是我的 HTML:

<form action="php/mail.php" method="POST">
<p class="form-field-title">Name</p>
<input class="contact-field focus-hide" type="text" id="name" name="name" required autocomplete="off">
<p class="form-field-title">Email Address</p>
<input class="contact-field focus-hide" type="email" id="email" name="email" required autocomplete="off">
<p class="form-field-title">Phone</p>
<input class="contact-field focus-hide" type="text" id="number" name="number" autocomplete="off">
<p class="form-field-title">Message</p>
<textarea class="contact-field focus-hide" id="message" name="message" data-gramm_editor="false" required autocomplete="off"></textarea>
<input class="contact-button" type="submit" value="Send Message">
</form>

这是我原来的(非 SMTP)mail.php 代码:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];

$formcontent="Name: $name \nEmail: $email \nPhone Number: $number     \nMessage: $message";
$recipient = "email@email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

header('Location: ../thanks.html');
exit();
?>

我有我的虚拟主机的电子邮件服务器详细信息,但我找到的 SMTP 解决方案都不适合我!

有人可以帮助我吗?我看到了一些关于使用 PEAR 的事情,但他们的网站似乎无法正常工作...?

谢谢!

【问题讨论】:

  • 如果你想使用本机 mail() 函数,你需要修改它的配置文件,这是不建议这样做的。你想要使用的解决方案是一个像 phpMailer 这样的第 3 方邮件库,它将为您完成所有繁重的工作。 github.com/PHPMailer/PHPMailer

标签: php smtp contact-form


【解决方案1】:

如果您的表单在电子邮件中,我认为您不能发布数据(也许我错了)。但是您可以使用 get 方法将其发送到您的脚本。

<form action="php/mail.php" method="GET">
</form>

然后更新你的 php :

<?php
$name = $_GET['name'];
$email = $_GET['email'];
$number = $_GET['number'];
$message = $_GET['message'];

$formcontent="Name: $name \nEmail: $email \nPhone Number: $number     \nMessage: $message";
$recipient = "email@email.com";
$subject = "Website Contact Form Enquiry";
$mailheader = "From: $email \r\n";

mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

header('Location: ../thanks.html');
exit();
?>

它应该可以完成这项工作。

【讨论】:

    猜你喜欢
    • 2010-12-21
    • 2014-03-16
    • 2013-03-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    相关资源
    最近更新 更多