article

Sunday, March 23, 2014

PHPMailer in CodeIgniter

PHPMailer in CodeIgniter

The first thing download the library
http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/

move folder /system/application/libraries

create a new PHP file in the CodeIgniter’s library directory called my_phpmailer.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_PHPMailer {
    public function My_PHPMailer() {
        require_once('PHPMailer/class.phpmailer.php');
    }
}
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Phpmailer extends CI_Controller {
 public function My_Controller(){
        parent::Controller();
        $this->load->library('My_PHPMailer');
  $this->load->library('session');
    }
  function index($msg = NULL)
 {
  $data['message'] = $msg;
  $this->load->view('contactus',$data);
 }
 public function send_mail() {
        $mail = new PHPMailer;
  //From email address and name
  $mail->From = "from@yourdomain.com";
  $mail->FromName = "Full Name";
  //To address and name
  $mail->addAddress("recepient1@example.com", "Recepient Name");
  $mail->addAddress("recepient1@example.com"); //Recipient name is optional
  //Address to which recipient will reply
  $mail->addReplyTo("reply@yourdomain.com", "Reply");
  //CC and BCC
  $mail->addCC("cc@example.com");
  $mail->addBCC("bcc@example.com");
  //Send HTML or Plain Text email
  $mail->isHTML(true);
  $mail->Subject = "Subject Text";
  $mail->Body = "<i>Mail body in HTML</i>";
  $mail->AltBody = "This is the plain text version of the email content"; 
   if(!$mail->send()) {
    $data["message"] = "Error: " . $mail->ErrorInfo;
  } else {
   $data["message"] = "<p><h3>Message sent correctly!</h3></p>";
  } 
        $this->load->view('contactus',$data);
    }
}
View
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Codeigniter 3.1.10 Dev - Phpmailer </title>
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Oleo+Script:400,700" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Teko:400,700" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body id="page-top" class="index">
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Oleo+Script:400,700" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Teko:400,700" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<section id="contact">
   <div class="section-content">
    <h1 class="section-header">Get in <span class="content-header wow fadeIn " data-wow-delay="0.2s" data-wow-duration="2s"> Touch with us</span></h1>
    <h3>PHPMailer in CodeIgniter with bootstrap contact form</h3>
   </div>
   <div class="contact-section">
   <div class="container"><?php if(! is_null($message)) echo $message;?>  
    <form action="send_mail" method="post">
     <div class="col-md-6 form-line">
        <div class="form-group">
         <label for="exampleInputUsername">Your name</label>
          <input type="text" class="form-control" id="" placeholder=" Enter Name">
        </div>
        <div class="form-group">
          <label for="exampleInputEmail">Email Address</label>
          <input type="email" class="form-control" id="exampleInputEmail" placeholder=" Enter Email id">
        </div> 
        <div class="form-group">
          <label for="telephone">Mobile No.</label>
          <input type="tel" class="form-control" id="telephone" placeholder=" Enter 10-digit mobile no.">
        </div>
       </div>
       <div class="col-md-6">
        <div class="form-group">
         <label for ="description"> Message</label>
          <textarea  class="form-control" id="description" placeholder="Enter Your Message"></textarea>
        </div>
        <div>
         <button type="submit" class="btn btn-default submit"><i class="fa fa-paper-plane" aria-hidden="true"></i>  Send Message</button>
        </div>
     </div>
    </form>
   </div>
  </section>
<style>
/*Contact sectiom*/
.content-header{
  font-family: 'Oleo Script', cursive;
  color:#fcc500;
  font-size: 45px;
}
.section-content{
  text-align: center; 

}
#contact{
    font-family: 'Teko', sans-serif;
  padding-top: 60px;
  width: 100%;
  width: 100vw;
  height: 550px;
  background: #3a6186; /* fallback for old browsers */
  background: -webkit-linear-gradient(to left, #3a6186 , #89253e); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(to left, #3a6186 , #89253e); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
    color : #fff;    
}
.contact-section{
  padding-top: 40px;
}
.contact-section .col-md-6{
  width: 50%;
}
.form-line{
  border-right: 1px solid #B29999;
}
.form-group{
  margin-top: 10px;
}
label{
  font-size: 1.3em;
  line-height: 1em;
  font-weight: normal;
}
.form-control{
  font-size: 1.3em;
  color: #080808;
}
textarea.form-control {
    height: 135px;
   /* margin-top: px;*/
}
.submit{
  font-size: 1.1em;
  float: right;
  width: 150px;
  background-color: transparent;
  color: #fff;
}
</style>  
</body>
</html>

Related Post