Welcome Guest, Not a member yet? Register   Sign In
Use Email as library on my project
#1

[eluser]Unknown[/eluser]
Hello Community,

On my project i need to use email to send to users mails, so i can use email librairy in controller but i will need to send mails from many controllers, so i create a table on my database when i register email parameters (smtp, port, user, pwd, ...) and i define a new classes "Mail" in library folder ...
this is "Mail" class code
Code:
<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mail
{
public $em;

//Constructeur
function __construct($em)
{
  $this->em = $em ;
    }

//envoie de mail
function sendMail($message , $sujet)
  {
   //Récupérer depuis la base de données les paramètres d'envoi de mails
   $repository = $this->em->getRepository('Entities\trere_param_mail');
   $param_mail = $repository->findAll();
  
   //Initialisation des params mail depuis la base de données
   foreach($param_mail as $param)
    {
      $config[$param->getREPARMPARAM()] = $param->getREPARMVALEUR();
    }

   $this->email->initialize($config);
      $this->email->from('ERP');
      $this->email->to('mail');
      $this->email->subject(sujet);
      $this->email->message($message);
    
   if($this->email->send())
     {
       return True;
     }
     else
    {
  return False;
    }

}

}
?>
And then i want to use it in my controllers like this :
Code:
class Login extends CI_Controller
{
  
    //Constructeur
public function __construct()
{
  //obligatoire
        parent::__construct();
  //ce code est exécuté à chaque fois on visite la page
  $this->load->library('email');
  $this->load->helper('email');
  $this->load->library('Mail');
    }
...
...
public function sendMail()
{
        ....
        ....
        $mail = new mail($this->doctrine->em);
        if($mail->sendMail($message,'sujet'))
   {
                 ....
                 }
                 else
                 { .... }
....

When i see my page, i have 2 warnings:
Quote:A PHP Error was encountered

Severity: Warning

Message: Missing argument 1 for Mail::__construct()
Filename: libraries/Mail.php
and
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: em

Filename: libraries/Mail.php
when i try to send mail, i have also an error :
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Mail::$email

Filename: libraries/Mail.php

Line Number: 29

Can you help me please resolving this !!

i'm sorry for my english also Sad

Thanks for help Smile




Theme © iAndrew 2016 - Forum software by © MyBB