Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向电子邮件发送数据和发送确认电子邮件的表单_Php_Forms_Function_Email_Post - Fatal编程技术网

Php 向电子邮件发送数据和发送确认电子邮件的表单

Php 向电子邮件发送数据和发送确认电子邮件的表单,php,forms,function,email,post,Php,Forms,Function,Email,Post,我一直在寻找解决我问题的办法,但似乎无法使它发挥作用 我有一个表单,从用户那里获取数据,然后在他们单击提交时通过电子邮件发送给我 我也希望他们收到一封确认电子邮件,地址是他们提供的,这就是我被卡住的地方 如果表格是必需的,请询问,我会发送,但这里是PHP <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = "webhost@compumodsa.com"; $email_

我一直在寻找解决我问题的办法,但似乎无法使它发挥作用

我有一个表单,从用户那里获取数据,然后在他们单击提交时通过电子邮件发送给我

我也希望他们收到一封确认电子邮件,地址是他们提供的,这就是我被卡住的地方

如果表格是必需的,请询问,我会发送,但这里是PHP

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

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "webhost@compumodsa.com";
$email_subject = "CompuMod Web-site Order";

function died($error) {

// your error code can go here
echo "Something was entered incorrectly…";
echo "Please correct the following.<br /><br />";
echo $error."<br /><br />";
echo "After correcting, please try again.<br /><br />";
die();
}

// validation expected data exists
if(
!isset($_POST['site']) ||
!isset($_POST['page']) ||
!isset($_POST['domain']) ||
!isset($_POST['host']) ||
!isset($_POST['service']) ||
!isset($_POST['firstname']) ||
!isset($_POST['lastname']) ||
!isset($_POST['company']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['idnumber']) ||
!isset($_POST['address']) ||
!isset($_POST['suburb']) ||
!isset($_POST['city']) ||
!isset($_POST['province']) ||
!isset($_POST['postcode']) ||
!isset($_POST['comments'])) {
died('Something was entered incorrectly…'); 
}

$site = $_POST['site']; // required
$page = $_POST['page']; // required
$domain = $_POST['domain']; // required
$host = $_POST['host']; // required
$service = $_POST['service']; // required
$firstname = $_POST['firstname']; // required
$lastname = $_POST['lastname']; // required
$company = $_POST['company']; // not required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$idnumber = $_POST['idnumber']; // required
$address = $_POST['address']; // required
$suburb = $_POST['suburb']; // not required
$city = $_POST['city']; // required
$province = $_POST['province']; // required
$postcode = $_POST['postcode']; // required
$comments = $_POST['comments']; // not required

$error_message = "";
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$site)) {
$error_message .= 'Your “Base Site Design” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$page)) {
$error_message .= 'Your “Extra Pages” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$domain)) {
$error_message .= 'Your “Domain Registration” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$host)) {
$error_message .= 'Your “Host Server Size” was not Selected.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$service)) {
$error_message .= 'Your “Service Package” was not Selected.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$firstname)) {
$error_message .= 'Your “First Name” was not entered or entered incorrectly.<br />';
}
if(!preg_match($string_exp,$lastname)) {
$error_message .= 'Your “Last Name” was not entered or entered incorrectly.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'Your “E-Mail” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$telephone)) {
$error_message .= 'Your “Phone/Cell” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$idnumber)) {
$error_message .= 'Your “SA ID Number” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z0-9 .'-]+$/";
if(!preg_match($string_exp,$address)) {
$error_message .= 'Your “Street Address” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$city)) {
$error_message .= 'Your “City” was not entered or entered incorrectly.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$province)) {
$error_message .= 'Your “Povince” was not entered or entered incorrectly.<br/>';
}
$string_exp = "/^[0-9]+$/";
if(!preg_match($string_exp,$postcode)) {
$error_message .= 'Your “Postal Code” was not entered or entered incorrectly.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Client request details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

//request email variables
$email_message .= "Base Site Design: - ".clean_string($site)."\n";
$email_message .= "Extra Pages: - ".clean_string($page)."\n";
$email_message .= "Domain Registration: - ".clean_string($domain)."\n";
$email_message .= "Host Server Size: - ".clean_string($host)."\n";
$email_message .= "Service Package: - ".clean_string($service)."\n";
$email_message .= "First Name: - ".clean_string($firstname)."\n";
$email_message .= "Last Name: - ".clean_string($lastname)."\n";
$email_message .= "Company: - ".clean_string($company)."\n";
$email_message .= "E-Mail: - ".clean_string($email)."\n";
$email_message .= "Phone/Cell: - ".clean_string($telephone)."\n";
$email_message .= "SA ID Number: - ".clean_string($idnumber)."\n";
$email_message .= "Address: - ".clean_string($address)."\n";
$email_message .= "Suburb: - ".clean_string($suburb)."\n";
$email_message .= "City: - ".clean_string($city)."\n";
$email_message .= "Province: - ".clean_string($province)."\n";
$email_message .= "Postal Code: - ".clean_string($postcode)."\n";
$email_message .= "Extra Requests: - ".clean_string($comments)."\n";

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email_from."\r\n" .
            'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers);

//conformation email variables
$subject = "Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)." for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email."\r\n" .
            'X-Mailer: PHP/' . phpversion(); 
@mail($email, $subject, $email_message, $headers);

header('Location: http://www.compumodsa.com/index.php/component/k2/item/21');

?>
<!-- include your own success html here --> 
<?php
}
?>
确认电子邮件将发送给用户

//conformation email variables
$subject = "Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)." for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";

$headers = 'From: '.$email_from."\r\n".
            'Reply-To: '.$email."\r\n" .
            'X-Mailer: PHP/' . phpversion(); 
@mail($email, $subject, $email_message, $headers);
用户未收到任何电子邮件


非常感谢您的帮助……

不知道您在这里想做什么,但这可能是您的问题所在

if($_POST['email'] == "post")
{ 
    //email variables
    $subject = "Indie Rally - Welcome & Thanks!";
    $email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)."for your request\n\n";
    $email_message .= "A Compumod representative will contact you as soon as posable.\n";

    // create email headers
    $headers = 'From: webhost@compumodsa.com' . "\r\n" .
                'Reply-To: $email' . "\r\n" .
                'X-Mailer: PHP/' . phpversion(); 

    mail($email, $subject, $email_message, $headers); 

}
假设$_POST['email']是填写表单的人的电子邮件地址,看看它是否等于明显无效的POST电子邮件地址,A总是会通过条件检查,或者B通过条件检查并将无效的to:地址传递给PHP邮件函数

由于您之前已经执行了测试,以确保电子邮件地址既存在又有效,因此您根本不应该要求自动应答器功能具有此条件。只需发送电子邮件

这将留给你这个

/*
 * Notice the removal of the `if()` statement
 */
//email variables
$subject = "Indie Rally - Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)."for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";

// create email headers
$headers = 'From: webhost@compumodsa.com' . "\r\n" .
            'Reply-To: $email' . "\r\n" .
            'X-Mailer: PHP/' . phpversion(); 

mail($email, $subject, $email_message, $headers); 

如果里面有什么不必要的东西,请告诉我。有什么问题吗?它有用吗?邮件寄出去了吗?是否存在任何错误,您是否正在检查这些错误?如果表单未使用POST方法且输入未命名,则可能会使您的代码停止。在HTML表单中添加required可替换开头的所有ifisset部分。@Twinfriends否,对所有输入执行服务器端验证是正确的。永远不要相信客户方。[编辑]我把问题说得更清楚了一点…很抱歉,听起来我是个彻头彻尾的傻瓜。正如您所说,代码有问题。你能帮我更正一下吗?@C-GuL我试图澄清我所说的话。请看答案的底部。我已经编辑了问题的最后一个代码框。仍然没有邮件发送给用户。如果我替换@mail$电子邮件;通过$email\u发送到网络主机地址。现在在我看来,它似乎不是从输入中获取邮件地址。它只适用于其他地址,而不适用于gmail地址。我是一个不知名的发件人,这就是为什么gmail帐户没有收到它。
/*
 * Notice the removal of the `if()` statement
 */
//email variables
$subject = "Indie Rally - Welcome & Thanks!";
$email_message = "Thank you ".clean_string($firstname)." ".clean_string($lastname)."for your request\n\n";
$email_message .= "A Compumod representative will contact you as soon as posable.\n";

// create email headers
$headers = 'From: webhost@compumodsa.com' . "\r\n" .
            'Reply-To: $email' . "\r\n" .
            'X-Mailer: PHP/' . phpversion(); 

mail($email, $subject, $email_message, $headers);