Ajax Email Send in Codeigniter

// routes

$route['admin/objednavka/send_email'] = "backend/order/send_email";

// html 

<div class="row static-info">
    <div class="col-md-5 name"> Email: </div>
    <input id="user_email" type="hidden" name="user_email" value="getsolved@gmail.com">

<div class="row static-info">
    <div class="col-md-5 name"> Subject: </div>
    <input id="user_email_sub" type="text" name="user_email_sub" value="getsolved@gmail.com">

<div class="row static-info">
    <div class="col-md-5 name"> body: </div>
    <textarea id="user_email_body" type="text" name="user_email_body">

<div class="row static-info">
    <div class="col-md-5 name"></div>
    <div class="col-md-5 value">  
        <button id="sendOrderEmail">Send Order Email</button>

<div class="row static-info">
    <div class="col-md-5 name"></div>
    <div class="col-md-2" style="display: none;" id="loading">
       <img src="<?= base_url() ?>assets/backend/img/preloader.gif" width="100%">

// jquery 

$("#sendOrderEmail").click(function () {

    var url = $(".url").val();
    var email = $('#user_email').val();
    var user_email_sub= $('#user_email_sub').val();
    var user_email_body= $('#user_email_body').val();

    // alert(email + email_type)
        url: url + "admin/objednavka/send_email",
        // url: url + "backend/order/send_email",
        type: 'post',
        data: { 
             'user_email_sub': user_email_sub,  
             'email': email, 
             'user_email_body': user_email_body 
        dataType: 'json',
        beforeSend: function () {
        success: function (json) {
            if (json.success) {
                alert('Email Successfully Sent')
        error: function (request, status, error) {
            alert("Nepodařilo se změnit.");

// controller
class Order extends Base_backend

    public function __construct()

        $this->layout->title('Objadnávka | Weboo Shop 2.0');

        $this->menuOpen->m_order = 1;

    public function sendEmailBackend($to, $subject, $body)
        $sender = $this->setting_model->get_setting("email", "sender", DEFAULT_LANG)->value;
        $senderName = $this->setting_model->get_setting("email", "sender-name", DEFAULT_LANG)->value;

        include_once APPPATH.'/libraries/phpmailer/PHPMailerAutoload.php';
        $mail = new PHPMailer;

        $mail->isSMTP();                            // Set mailer to use SMTP
        $mail->Host = _s("mail-server");            // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                     // Enable SMTP authentication
        $mail->Username = _s("mail-login");         // SMTP username
        $mail->Password = _s("mail-password");      // SMTP password
        $mail->SMTPSecure = 'ssl';                  // Enable TLS encryption, `ssl` also accepted
        $mail->Port = _s("mail-port");              // TCP port to connect to
        $mail->CharSet = 'UTF-8';
        $mail->setFrom(_s("mail-login"), $senderName);
        $mail->addReplyTo($sender, $senderName);

        $mail->isHTML(true);                         // Set email format to HTML

        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AltBody = $body;

        return 1;

    public function send_email()
        $item = $_POST['email_type'];
        $email_text  = $this->b_order_model->get_email_text_by_status($item);

        $email = $_POST['email'];
        $user_email_sub= $_POST['user_email_sub'];
        $user_email_body = $_POST['user_email_body '];

        $status = $this->sendEmailBackend($email,$user_email_sub, $user_email_body );

        echo json_encode(['success' => 1,'status' => $status]);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s