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>

<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>

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

<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>
</div>

<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%">
    </div>
</div>

// 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)
    $.ajax({
        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 () {
            $("#loading").show();
        },
        success: function (json) {
            
            if (json.success) {
                alert('Email Successfully Sent')
            }
            $("#loading").hide();
            
        },
        error: function (request, status, error) {
            alert("Nepodařilo se změnit.");
            $("#loading").hide();
        }
    });
});

// controller
class Order extends Base_backend
{

    public function __construct()
    {
        parent::__construct();

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

        $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->addAddress($to);     
        $mail->addReplyTo($sender, $senderName);

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

        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AltBody = $body;
        $mail->send();

        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]);
    }
}

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s