请问如何在当提交单表后,自动回复提交者Email感谢和内容信息?以下是我的代码

新手上路,请多包涵
<?php
$recipient_email    = "my@email.com"; //recepient
$from_email         = "my@email.com"; //from email using site domain.


if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
    die('Sorry Request must be Ajax POST'); //exit script
}

if($_POST){
    
    $sender_paypal     = filter_var($_POST["paypal"], FILTER_SANITIZE_STRING); //capture sender name
    $sender_name     = filter_var($_POST["name"], FILTER_SANITIZE_STRING); //capture sender name
    $sender_email     = filter_var($_POST["email"], FILTER_SANITIZE_STRING); //capture sender email
    $phone_number   = filter_var($_POST["phone"], FILTER_SANITIZE_NUMBER_INT);
    $date        = filter_var($_POST["date"], FILTER_SANITIZE_STRING);

    $attachments = $_FILES['file_attach'];
    
    
    //php validation, exit outputting json string
    if(strlen($sender_paypal)<4){
        print json_encode(array('type'=>'error', 'text' => 'Paypal is too short or empty!'));
        exit;
    }    
    if(strlen($sender_name)<4){
        print json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
        exit;
    }
    if(!filter_var($sender_email, FILTER_VALIDATE_EMAIL)){ //email validation
        print json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
        exit;
    }
    if(!$phone_number){ //check for valid numbers in phone number field
        print json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
        exit;
    }
    if(strlen($date)<3){ //check emtpy date
        print json_encode(array('type'=>'error', 'text' => 'date is required'));
        exit;
    }

    
    $file_count = count($attachments['name']); //count total files attached
    $boundary = md5("sanwebe.com"); 
    
    //construct a message body to be sent to recipient
    $message_body =  "Message from Your Web\n\n\n";
    $message_body .=  "Paypal No: $sender_paypal\n\n";
    $message_body .=  "Name: $sender_name\n\n";
    $message_body .=  "Date: $date\n\n";
    $message_body .=  "Phone No: $phone_number\n\n";
    $message_body .=  "Email: $sender_email\n";
    
    if($file_count > 0){ //if attachment exists
        //header
        $headers = "MIME-Version: 1.0\r\n"; 
        $headers .= "From:".$from_email."\r\n"; 
        $headers .= "Reply-To: ".$sender_email."" . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 
        
        //message text
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
        $body .= chunk_split(base64_encode($message_body)); 

        //attachments
        for ($x = 0; $x < $file_count; $x++){       
            if(!empty($attachments['name'][$x])){
                
                if($attachments['error'][$x]>0) //exit script and output error if we encounter any
                {
                    $mymsg = array( 
                    1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini", 
                    2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", 
                    3=>"The uploaded file was only partially uploaded", 
                    4=>"No file was uploaded", 
                    6=>"Missing a temporary folder" ); 
                    print  json_encode( array('type'=>'error',$mymsg[$attachments['error'][$x]]) ); 
                    exit;
                }
                
                //get file info
                $file_name = $attachments['name'][$x];
                $file_size = $attachments['size'][$x];
                $file_type = $attachments['type'][$x];
                
                //read file 
                $handle = fopen($attachments['tmp_name'][$x], "r");
                $content = fread($handle, $file_size);
                fclose($handle);
                $encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
                
                $body .= "--$boundary\r\n";
                $body .="Content-Type: $file_type; name=".$file_name."\r\n";
                $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
                $body .="Content-Transfer-Encoding: base64\r\n";
                $body .="X-Attachment-Id: ".rand(20000,99999)."\r\n\r\n"; 
                $body .= $encoded_content; 
            }
        }

    }else{ //send plain email otherwise
       $headers = "From:".$from_email."\r\n".
        "Reply-To: ".$sender_email. "\n" .
        "X-Mailer: PHP/" . phpversion();
        $body = $message_body;
    }
        
    $sentMail = mail($recipient_email, $sender_email, $body, $headers);
    if($sentMail) //output success or failure messages
    {       
        print json_encode(array('type'=>'done', 'text' => '谢谢你,线上报名表格已经成功提交!'));
        exit;
    }else{
        print json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));  
        exit;
    }
}
?>
阅读 2k
1 个回答

首先,不建议使用 mail 函数直接发送email,服务器支不支持先不说(并不是所有供应商都愿意开放25端口,这种容易被滥用的接口),国内的反垃圾机制基本上你这就被当垃圾处理了,甚至垃圾箱里都没有,直接过滤掉。所以一定要使用SMTP

以163为例,申请一个163邮箱,打开SMTP服务
下载一个配置类 http://files.cnblogs.com/file...

    require_once("email.class.php");
    $smtpServer="smtp.126.com";
    $smtpServerPort="25";
    $smtpUserMail="rhythmk@126.com";
    $mailTo="fengwuyan512@163.com";
    $user="wangkyx";
    $mailPwd="{发送邮箱密码}";
    $mailTitle="邮箱标题";
    $mailContent='<h1>测试邮件 001</h1>';
    // 邮件格式 (HTML/TXT)
    $mailType="HTML";
    // true表示是否身份验证
    $smtp=new smtp($smtpServer,$smtpServerPort,true,$user,$mailPwd);
    // 是否显示调试信息
    $smtp->debug=false;
    // 返回 bool
    $state=$smtp->sendmail($mailTo,$smtpUserMail,$mailTitle,$mailContent,$mailType);
    var_dump($state);
    
    (邮箱代码这段是百度抄的,这里是25端口是个示例,实际上主流邮箱都会提供SSL协议的端口)
    
    $mailContent 就是发送的内容,你可以把呀发送的东西写成HTML 的格式,把内容放进去(强烈建议行内样式),就像正常页面那样。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题