Php 联系人页面-未发送电子邮件
我已经创建了一个联系人页面,它使用PHP和HTML代码。我在我们的服务器上运行代码。当我的代码运行时,我没有收到任何电子邮件。我已经尝试了很多,但我不知道如何解决这个问题。下面是我的代码:Php 联系人页面-未发送电子邮件,php,html,Php,Html,我已经创建了一个联系人页面,它使用PHP和HTML代码。我在我们的服务器上运行代码。当我的代码运行时,我没有收到任何电子邮件。我已经尝试了很多,但我不知道如何解决这个问题。下面是我的代码: <?php if (isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "pixel.thennarasu@tgmail.com"; $email_subject = "
<?php
if (isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "pixel.thennarasu@tgmail.com";
$email_subject = "Your email subject line";
function died($error)
{
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error . "<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if (!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($email_exp, $email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if (!preg_match($string_exp, $first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if (!preg_match($string_exp, $last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if (strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if (strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string)
{
$bad = array(
"content-type",
"bcc:",
"to:",
"cc:",
"href"
);
return str_replace($bad, "", $string);
}
$email_message .= "First Name: " . clean_string($first_name) . "\n";
$email_message .= "Last Name: " . clean_string($last_name) . "\n";
$email_message .= "Email: " . clean_string($email_from) . "\n";
$email_message .= "Telephone: " . clean_string($telephone) . "\n";
$email_message .= "Comments: " . clean_string($comments) . "\n";
// create email headers
$headers = 'From: ' . $email_from . "\r\n" . 'Reply-To: ' . $email_from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
echo '@mail';
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
修正了代码,只是在邮件前面删除了@。。我看不出它存在的任何理由:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "pixel.thennarasu@tgmail.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
echo '@mail';
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
回音“@mail”;
感谢您联系我们。我们将很快与您联系。
首先,从邮件功能中删除@。使用@将不会抛出任何错误。
邮件函数返回bollean值,因此请检查它:
$mail = mail();
var_dump($mail);
之后,检查邮件服务器上的日志(带有脚本的服务器,以及除获取此邮件之外的其他位置),设置错误的邮件头(如回复地址等)会导致许多问题
如何在本地主机上配置XAMPP:
您是否有SMTP服务器设置?
如果没有,则需要在本地运行邮件服务器。如果是Unix,请启用sendmail如果是Windows,请安装IIs的简单邮件传输服务器(不确定名称是否正确)组件。例如,对于windows 2003,请执行以下操作:您在spamyes brother中检查过吗?我检查过brother我没有收到电子邮件brother..为什么要使用“
邮件前面的@”功能?我修复了帖子中的语法和大小写错误,通过改进缩进和删除额外的换行符来重新格式化代码,删除了与当前问题不相关的标记,并从标题中删除了语言名称,因为由于存在标记,该语言名称不是必需的。@suppress警告,通过删除它并不能解决问题。我已删除类似“现在”的错误,例如无法在“localhost”端口25连接到mailserver,请验证您的“SMTP”和php.ini中的“smtp_端口”设置或使用ini_set()in@Thennarasu那就是你的主人的问题了。他们的邮件服务器有问题,或者他们刚刚阻止了您的邮件服务。@Thennarasu您在localhost中尝试代码是什么意思?现在尝试我的代码服务器站点而不是localhost。现在我可以在localhost xampp端尝试该代码吗?我已经删除了显示类似以下错误的行:无法在“localhost”连接到邮件服务器端口25,验证php.ini中的“SMTP”和“SMTP_端口”设置,或者在错误的邮件配置中使用ini_set(),您在服务器上做了什么?它是什么样的服务器?我们需要了解更多;)如果您没有配置邮件服务,请不要在localhost上测试它。我没有任何电子邮件服务器。我已在localhost php.ini中设置了我的服务器地址。只有我更改了,看起来像[邮件功能];XAMPP:如果您想使用Mercury SMTP=dotnetportfolio.com这样的SMTP服务器,请将此注释掉;smtp_port=25因此您没有配置任何内容,请搜索“在xampp上配置邮件服务器”:先生,我已更改其工作邮件发送的本地端。我已使用smtp。服务器端不工作
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "pixel.thennarasu@tgmail.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
echo '@mail';
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
$mail = mail();
var_dump($mail);