How to send emails from codeigniter with phpmailer? [closed]

1

Hello good morning you will see I am very new in this and recently I was entrusted with the task of sending emails. I have to create a form that allows me to take errands and then another that is to send notifications of the raids and I did it allows me to save and modify what is written in the bd, but the notifications asked me to be notified to the person by half of an automatic email and in that email the motive and the message appear using phpmailer and the truth I do not have the slightest idea of how to do it but try not to understand it I do not know if you could help me:

link

In this folder you can find all the files that I am using for a message along with an image of the bd. It is necessary to clarify that you are using several tools (jsgrid and bootstrap) using js in the same way, I need help please how would you get it to work ?

    
asked by genesis96 14.03.2018 в 19:32
source

2 answers

3

Hello, you can try the following:

1.- You can execute a function every time you need to send an email notification

2.- To send a simple email using php mailer
 a) Download php mailer and unzip the folder in " tuproyecto / application / libraries /"
 b) Load library of php mailer in function __construct () {} of your controller
require_once('application/libraries/PHPMailer/PHPMailerAutoload.php');
 c) Use php mailer where you need it

$mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";
        $mail->Host = "Host";
        $mail->Port = 465;
        $mail->CharSet = 'UTF-8';
        $mail->Username ='[email protected]'; //Email para enviar
        $mail->Password = '123'; //Su password
        //Agregar destinatario
        $mail->setFrom('[email protected]', 'user name sender');
        $mail->AddAddress('[email protected]');//A quien mandar email
        $mail->Subject = "Notificación";
        $mail->msgHTML ("<h1>Ejemplo contenido html</h1>");
       if(!$mail->send()) {
      echo 'Error al enviar email';
      echo 'Mailer error: ' . $mail->ErrorInfo;
     } else {
       echo 'Mail enviado correctamente';
     }
    
answered by 14.03.2018 в 20:12
0

In CodeIgniter there is the library email to send emails.

In the theory, controllers should be the ones who handle all these actions, so in one of your choice, you can have a function similar to the following:

public function enviarcorreo(){

  $correo = $this->input->post('email');

  $this->load->library('email');

  $config['protocol'] = 'sendmail';
  $config['mailpath'] = '/usr/sbin/sendmail';
  $config['charset'] = 'iso-8859-1';
  $config['wordwrap'] = TRUE;

  $this->email->initialize($config);

  $this->email->from('[email protected]', 'Nombre de tu compania');
  $this->email->to($correo);

  $this->email->subject('Email');
  $this->email->message('Aqui iria el mensaje que necesitas enviar."]);

  $this->email->send();
}

In this hypothetical and simplified case, this is the step of the actions

  • We received the email from the recipient by means of a post request (this would be common, but it may be that it is stored in the database)
  • We load the email library
  • The default configuration is used
  • The configuration is initialized
  • We specify the sender and recipient
  • We specify the title and the content
  • The function send() sends the message
  • I understand that it is not phpmailer , but if you are using CodeIgniter, keep in mind that there is already a library for this and it is not necessary to use another one, but, so that you are using the framework?

        
    answered by 14.03.2018 в 20:15