在Code Igniter中简单发送电子邮件(Modal,PHP,AJAX)

时间:2017-10-20 05:48:51

标签: php ajax codeigniter email

我在使用CodeIgniter的项目中发送简单的电子邮件时遇到了麻烦。

我的codeigniter就是这样的设置

应用

资产

系统

我发送的电子邮件是我的资产/公共/查询/ 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     = 'paul.aliassistance@gmail.com';
$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: Sales@euroits.com.au' . "\r\n";

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

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

现在在我的应用程序/ views / templates / _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._%+-]+@([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} hays。

1 个答案:

答案 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@email.com', 'Name');
        $this->email->to('someone@example.com');
        $this->email->cc('another@example.com');
        $this->email->bcc('and@another.com');

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

        $this->email->send();

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

}

复制上述代码并尝试使用内置的电子邮件类

的codeigniter