2017-10-20 69 views
0

我在使用CodeIgniter的項目中發送簡單的電子郵件時遇到了麻煩。在代碼點火器(Modal,PHP,AJAX)中簡單發送電子郵件

我笨是設置這樣的

應用

資產

系統

我發送電子郵件是在我的資產/公/查詢/ Inquiry.php

<?php 
if(isset($_POST['contactFrmSubmit']) && !empty($_POST['name']) && 
!empty($_POST['email']) && (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) && !empty($_POST['message'])){ 

// Submitted form data 
$name = $_POST['name']; 
$email = $_POST['email']; 
$message= $_POST['message']; 

/* 
* Send email to admin 
*/ 
$to  = '[email protected]'; 
$subject= 'Contact Request Submitted'; 

$htmlContent = ' 
<h4>Contact request has submitted at CodexWorld, details are given below. 
</h4> 
<table cellspacing="0" style="width: 300px; height: 200px;"> 
    <tr> 
     <th>Name:</th><td>'.$name.'</td> 
    </tr> 
    <tr style="background-color: #e0e0e0;"> 
     <th>Email:</th><td>'.$email.'</td> 
    </tr> 
    <tr> 
     <th>Message:</th><td>'.$message.'</td> 
    </tr> 
</table>'; 

// Set content-type header for sending HTML email 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 

// Additional headers 
$headers .= 'From: [email protected]' . "\r\n"; 

// Send email 
if(mail($to,$subject,$htmlContent,$headers)){ 
    $status = 'ok'; 
}else{ 
    $status = 'err'; 
} 

// Output status 
echo $status;die; 
} 
?> 

現在我的看法是在我的應用程序/ views/templa TES/_parts/public_master_footer.php

通過我的CMS我的方式,這就是爲什麼我的路就是這樣

<div class="col-md-12"> 
    <button class="btn btn-info btn-sm" data-toggle="modal" data-target="#modalForm">Ask a question</button> 
</div> 

<!-- Modal --> 
<div class="modal fade" id="modalForm" role="dialog"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <!-- Modal Header --> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal"> 
        <span aria-hidden="true">&times;</span> 
        <span class="sr-only">Close</span> 
       </button> 
       <h4 class="modal-title" id="myModalLabel">Contact Form</h4> 
      </div> 

      <!-- Modal Body --> 
      <div class="modal-body"> 
       <p class="statusMsg"></p> 
       <form role="form"> 
        <div class="form-group"> 
         <label for="inputName">Name</label> 
         <input type="text" class="form-control" id="inputName" placeholder="Enter your name"/> 
        </div> 
        <div class="form-group"> 
         <label for="inputEmail">Email</label> 
         <input type="email" class="form-control" id="inputEmail" placeholder="Enter your email"/> 
        </div> 
        <div class="form-group"> 
         <label for="inputMessage">Message</label> 
         <textarea class="form-control" id="inputMessage" placeholder="Enter your message"></textarea> 
        </div> 
       </form> 
      </div> 

      <!-- Modal Footer --> 
      <div class="modal-footer"> 
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       <button type="button" class="btn btn-primary submitBtn" onclick="submitContactForm()">SUBMIT</button> 
      </div> 
     </div> 
    </div> 
</div> 

<!--ASK A QUESTION BACKEND--> 
<script> 
    function submitContactForm(){ 
     var reg = /^[A-Z0-9._%+-][email protected]([A-Z0-9-]+\.)+[A-Z]{2,4}$/i; 
     var name = $('#inputName').val(); 
     var email = $('#inputEmail').val(); 
     var message = $('#inputMessage').val(); 
     if(name.trim() == ''){ 
      alert('Please enter your name.'); 
      $('#inputName').focus(); 
      return false; 
     }else if(email.trim() == ''){ 
      alert('Please enter your email.'); 
      $('#inputEmail').focus(); 
      return false; 
     }else if(email.trim() != '' && !reg.test(email)){ 
      alert('Please enter valid email.'); 
      $('#inputEmail').focus(); 
      return false; 
     }else if(message.trim() == ''){ 
      alert('Please enter your message.'); 
      $('#inputMessage').focus(); 
      return false; 
     }else{ 
      $.ajax({ 
       type:'POST', 
       url:'https://www.euroits.com.au/assets/public/inquiry/inquiry', 
       data:'contactFrmSubmit=1&name='+name+'&email='+email+'&message='+message, 
       beforeSend: function() { 
        $('.submitBtn').attr("disabled","disabled"); 
        $('.modal-body').css('opacity', '.5'); 
       }, 
       success:function(msg){ 
        if(msg == 'ok'){ 
         $('#inputName').val(''); 
         $('#inputEmail').val(''); 
         $('#inputMessage').val(''); 
         $('.statusMsg').html('<span style="color:green;">Thanks for contacting us, we\'ll get back to you soon.</p>'); 
        }else{ 
         $('.statusMsg').html('<span style="color:red;">Some problem occurred, please try again.</span>'); 
        } 
        $('.submitBtn').removeAttr("disabled"); 
        $('.modal-body').css('opacity', ''); 
       } 
      }); 
     } 
    } 
</script> 
<!-- END --> 

幫助我的人請我試了一下,通常它的工作原理和它沒有錯誤,它只是扔我的else{statusMsg}乾草。

+0

爲什麼使用PHP mail()函數,嘗試codeigniter的郵件功能() 你測試了開發模式的頁面,看看是否有任何錯誤顯示? –

+0

我正在試驗先生 – NoobProgrammer

+0

您是否測試過使用開發模式的頁面,並查看是否顯示任何錯誤? –

回答

0

創建一個名爲mailer.php在申請文件/控制器

<?php defined('BASEPATH') OR exit('No direct script access allowed'); 

class Mailer extends CI_Controller 
{ 

    public function index() 
    { 
     $this->load->library('email'); 

     $this->email->from('[email protected]', 'Name'); 
     $this->email->to('[email protected]'); 
     $this->email->cc('[email protected]'); 
     $this->email->bcc('[email protected]'); 

     $this->email->subject('subject'); 
     $this->email->message('message'); 

     $this->email->send(); 

     echo $this->email->print_debugger(); 
    } 

} 

複製上面的代碼,並與笨嘗試內置電子郵件類