Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send attachments in email using Codeingniter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 550
    Comment on it

    Hello Reader's, If you have developed the web project in Codeigniter and want to send attachments using emails then in this blog you can learn how to do this.
    Sending the attachments can be done by several way in php, We will do this by using PHP Mailer since this is short and easy to send emails. So lets get started doing this:-

    Step1: - Download the latest version of PHP mailer. You can get it from here http://github.com/PHPMailer/PHPMailer

    Step2:- Create new controller for sending the attachments and put the PHP mailer code as below:-

    $email = new PHPMailer();
    $email->From      = 'youremail@abc.com';
    $email->FromName  = 'sendername';
    $email->Subject   = 'This is a email test blog to send attachmetns';
    $email->Body      = $msg;
    $email->AddAddress( 'useremail@abc.com' );
    $sourceAttachment = 'link the path of the your attachment herer';
    $email->AddAttachment( $sourceAttachment , 'attachment.pdf' );
    return $email->Send();

    In the code above creating a new PHPMailer() will require the file which we have download in step1.
    Extract the folder and paste it to somewhere it can be accessible by this controller.
    Now in the beginning of this code you need to call PHPMailerAutoload.php file which can be done by line as follows:-

    require FCPATH.'assets/PHPMailer/PHPMailerAutoload.php';

    Now you just need to run this code and your email will send to destination email address.  If you are not getting emails then in that case you need to use the SMTP. By using SMPT email is passes through a valid authentic process. For doing this you just need to turn on the SMPT by putting the code in the above code as follows:

    $mail->isSMTP();     // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';      // Specify main and backup server
    $mail->SMTPAuth = true;              // Enable SMTP authentication
    $mail->Username = 'your@gmail.com';   // SMTP username
    $mail->Password = 'password';         // SMTP password
    $mail->SMTPSecure = 'tls';           // Enable encryption, 'ssl' also accepted
    $mail->Port = 587; 

     

    And this functionality can be also done by using simple php mail() function. But that will requires huge lines of code which is lead to many bugs to takes more time for fixing. You can see this code is really short and easy to implement and works fine.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: