2017-02-17 203 views
-2
<?php 
if($_POST) 
{ 
    $to_Email  = "[email protected]"; //Replace with recipient email address 
    $subject  = 'Fitnes Studio Elit novi mail'; //Subject line for emails 


    //check if its an ajax request, exit if not 
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 

     //exit script outputting json data 
     $output = json_encode(
     array(
      'type'=>'error', 
      'text' => 'Request must come from Ajax' 
     )); 

     die($output); 
    } 

    //check $_POST vars are set, exit if any missing 
    if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userPhone"]) || !isset($_POST["userMessage"])) 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!')); 
     die($output); 
    } 

    //Sanitize input data using PHP filter_var(). 
    $user_Name  = filter_var($_POST["userName"], FILTER_SANITIZE_STRING); 
    $user_Email  = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL); 
    $user_Phone  = filter_var($_POST["userPhone"], FILTER_SANITIZE_STRING); 
    $user_Message  = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING); 

    //additional php validation 
    if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error. 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!')); 
     die($output); 
    } 
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!')); 
     die($output); 
    } 
    if(!is_numeric($user_Phone)) //check entered data is numbers 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Only numbers allowed in phone field')); 
     die($output); 
    } 
    if(strlen($user_Message)<5) //check emtpy message 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.')); 
     die($output); 
    } 

    //proceed with PHP email. 
    $headers = 'From: '.$user_Email.'' . "\r\n" . 
    'Reply-To: '.$user_Email.'' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

    $sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers); 

    if(!$sentMail) 
    { 
     $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.')); 
     die($output); 
    }else{ 
     $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .'. Thank you for contacting us. We will be in touch with you very soon.')); 
     die($output); 
    } 

我有问题,用户的手机号码是不是来的电子邮件.. 我想:PHP脚本电子邮件不发送电话号码?

$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name,$user_Phone, $headers); 

但是,当我尝试,邮件不来anymore.Any帮助,我米没有这样的PHP。

+0

这是因为您无法将电子邮件发送到手机号码,您必须将其发送到号码的运营商地址。对于某些移动网络,我认为这只能起作用。看看使用API​​来实现这一点。 – KDOT

+2

如果'$ user_Phone'有一个值,请尝试追加消息:'$ user_Message。=“ - 名称:”。 $ user_Name。 “ - 电话: ” 。 $ USER_PHONE; ',然后发送邮件:'$ sentMail = mail($ to_Email,$ subject,$ user_Message,$ headers);' – JustBaron

+0

您的代码中的哪个位置*您正在尝试*发送电话号码?完整的代码完全不使用该值,单行样本尝试将其用作“mail()”函数的随机参数。你认为这个论点有什么作用? – David

回答

1

用户名和USERPHONE之间的逗号(,)使邮件是不同的充参数 你可以试试这个

$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name.$user_Phone, $headers); 
+0

之前构建'user_message'字符串会更明智谢谢,问题解决了! (y) – Tomaesp

+0

最受欢迎(y) –

1

附加您的消息第一:

$user_Message .= " - Name: " . $user_Name . " - Phone: " . $user_Phone;

然后发送邮件:

$sentMail = mail($to_Email, $subject, $user_Message, $headers);