【问题标题】:Email form from a website via PHP issues通过 PHP 问题从网站发送电子邮件表单
【发布时间】:2012-10-17 18:30:12
【问题描述】:

我正在编写一个个人网站,但我的联系人有问题。如果您能帮我找出问题所在,我将不胜感激。

网站的链接是www.tiryakicreative.com,php表单的代码如下:

<div id="form">
<form id="ajax-contact-form" action="contact_form/send_form_email.php…
<fieldset class="info_fieldset">
<div id="note"></div>
<div id="fields">
<label>Name</label>
<input class="textbox" type="text" name="name" value="" />
<label>E-Mail</label><input class="textbox" type="text" name="email" value="" />     
<label>Subject</label>
<input class="textbox" type="text" name="subject" value="" />
<label>Message</label>
<textarea class="textbox2" name="message" rows="5" cols="25"></textarea>
<label> </label><input class="button" type="image" src="send2.gif" id="submit"   Value="Send Message" />    
</div>
</fieldset>
</form>
</div>
</div>

这里是给定 html 代码的 php 代码:

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ian_tiryaki@hotmail.com";
$email_subject = "New Email from Website";


function died($error) {
// ERROR CODE GOES HERE
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.";
echo $error."";
echo "Please go back and fix these errors.";
die();
}

// validation expected data exists
if(!isset($_POST['name']) || 
!isset($_POST['email']) ||
!isset($_POST['subject']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.'); 
}

$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['subject']; // not required
$comments = $_POST['message']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Z…
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The subject you entered does not appear to be valid.';
}
if(strlen($comments) < 2) {
$error_message .= 'The message you entered do not appear to be valid.';
}
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:",…
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Subject: ".clean_string($telephone)."\n";
$email_message .= "Message: ".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 success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

【问题讨论】:

  • 当您尝试发送电子邮件时会发生什么?
  • 空白页打开,无操作...
  • 当 PHP 给你一个空白页面时,你应该检查 Web 服务器日志文件。另外,我假设实际代码中不存在省略号(“...”)。
  • 我仔细检查了省略号的代码,不,它们不存在。
  • 在代码段中间有一个函数是一种奇怪的语法。您应该检查服务器日志中的错误消息。

标签: php html forms email web


【解决方案1】:

将此设置为您的操作表单... 你肯定会从这里收到邮件.. 虽然有一个错误,但您可能会使用下面的代码使用 php 发送邮件而不声明变量... 喜欢

$email_to=$_POST['email'];
$email_subject=$_POST['subject'];
$email_message=$_POST['message'];
$headers=$_POST['title'];

    mail('$email_to', '$email_subject', '$email_message', '$headers');

否则 使用下面的代码发送邮件

mail('$_POST['email']','$_POST['subject']','$_POST['message']','$_POST['title']');

【讨论】:

  • 直接使用 POST 数组既存在安全风险,也不能保证“肯定会从这里收到邮件”,因为邮件功能失败可能还有其他原因。
【解决方案2】:

尝试添加

error_reporting(E_ALL);

到脚本的顶部。

您也可以尝试从邮件命令中删除@

mail($email_to, $email_subject, $email_message, $headers); 

因为这将抑制正在生成的任何错误。

这可能很简单,例如禁用附加标头的 PHP 邮件功能(某些主机出于安全原因这样做),在这种情况下邮件功能将失败。

【讨论】:

    【解决方案3】:

    您是否在用户发送电子邮件后收到电子邮件?

    【讨论】:

    • 那是评论,不是答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多