23
public function sendemail(){
  $config = Array( 
  'protocol' => 'smtp', 
  'smtp_host' => 'ssl://smtp.googlemail.com', 
  'smtp_port' => 465, 
  'smtp_user' => 'email@gmail.com', 
  'smtp_pass' => 'password', ); 

  $this->load->library('email', $config); 
  $this->email->set_newline("\r\n");
  $this->email->from('email@gmail.com', 'Name');
  $this->email->to('email@yahoo.com');
  $this->email->subject(' My mail through codeigniter from localhost '); 
  $this->email->message('Hello World…');
  if (!$this->email->send()) {
    show_error($this->email->print_debugger()); }
  else {
    echo 'Your e-mail has been sent!';
  }
}   

I get an error when I use codeigniter to send the email:

Message: mail() [function.mail]: Failed to connect to mailserver at 
"localhost" port 25, verify your "SMTP" and "smtp_port" setting in 
php.ini or use ini_set().

and

Unable to send email using PHP mail(). Your server might not be 
configured to send mail using this method.

What am I doing wrong?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Maulana Wahid
  • 245
  • 1
  • 3
  • 11

2 Answers2

41

Please check my working code.

function sendMail()
{
    $config = Array(
  'protocol' => 'smtp',
  'smtp_host' => 'ssl://smtp.googlemail.com',
  'smtp_port' => 465,
  'smtp_user' => 'xxx@gmail.com', // change it to yours
  'smtp_pass' => 'xxx', // change it to yours
  'mailtype' => 'html',
  'charset' => 'iso-8859-1',
  'wordwrap' => TRUE
);

        $message = '';
        $this->load->library('email', $config);
      $this->email->set_newline("\r\n");
      $this->email->from('xxx@gmail.com'); // change it to yours
      $this->email->to('xxx@gmail.com');// change it to yours
      $this->email->subject('Resume from JobsBuddy for your Job posting');
      $this->email->message($message);
      if($this->email->send())
     {
      echo 'Email sent.';
     }
     else
    {
     show_error($this->email->print_debugger());
    }

}
Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
  • 1
    is there any configuration in localhost so email can be sent? – Maulana Wahid Sep 03 '13 at 08:05
  • yeah you have to enable openssl in your localhost – Venkata Krishna Sep 03 '13 at 08:16
  • why everytime i change $this->email->from('xxx@gmail.com'); to other address like yahoo ,the email i recieved always sent from 'me' instead the sender? – Maulana Wahid Sep 03 '13 at 08:51
  • so how do i send email not only using gmail but by using other email services too? – Maulana Wahid Sep 03 '13 at 11:47
  • for that you can send it from PHP mail option in any server but not on localhost – Venkata Krishna Sep 03 '13 at 11:59
  • I got this error message. **A PHP Error was encountered** Severity: Warning Message: fsockopen(): unable to connect to ssl://smtp.googlemail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) Filename: libraries/Email.php Line Number: 1689 – Musaddiq Khan Aug 13 '14 at 14:55
  • Bit of extra info on the settings. SMTP username: Your full Gmail or Google Apps email address (e.g. example@gmail.com or example@yourdomain.com). SMTP password: Your Gmail or Google Apps email password – Jack Vial Dec 03 '15 at 13:43
  • @MusaddiqKhan I know this is late, but for future readers they might get help from this. Change the `smtp_host` to `'ssl://smtp.gmail.com'`. This is working fine for me. – Hitesh Apr 28 '16 at 02:49
  • but it still giving error like Message: Undefined variable: config – ashish bansal May 19 '17 at 03:29
  • Beside the addition of parameters mailset, word wrap and charset, how is your code different from the OP's code? The error message talks about the port number. It will be helpful if you also include details on what server settings you have on your machine such as changes to php.ini, etc. – Harish May 28 '17 at 05:46
  • I have found this tutorial which describe send emails in a CodeIgniter application using SMTP https://www.cloudways.com/blog/send-email-codeigniter-smtp/ – Owais Alam Aug 23 '17 at 11:42
  • this line has error `$this->load->library('email', $config);` it should be as `$this->load->library('email');` after it you can initialize as `$this->email->initialize($config);` – Haritsinh Gohil Dec 17 '18 at 13:09
0
$insert = $this->db->insert('email_notification', $data);
                $this->session->set_flashdata("msg", "<div class='alert alert-success'> Cafe has been added Successfully.</div>");

                //require ("plugins/mailer/PHPMailerAutoload.php");
                $mail = new PHPMailer;
                $mail->SMTPOptions = array(
                    'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true,
                ),
                );

                $message="
                     Your Account Has beed created successfully by Admin:
                    Username: ".$this->input->post('username')." <br><br>
                    Email: ".$this->input->post('sender_email')." <br><br>
                    Regargs<br>
                    <div class='background-color:#666;color:#fff;padding:6px;
                    text-align:center;'>
                         Bookly Admin.
                    </div>
                ";
                $mail->isSMTP(); // Set mailer to use SMTP
                $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
                $mail->SMTPAuth = true; 
                $subject = "Hello  ".$this->input->post('username');
                $mail->SMTDebug=2;
                $email = $this->input->post('sender_email'); //this email is user email
                $from_label = "Account Creation";
                $mail->Username = 'your email'; // SMTP username
                $mail->Password = 'password'; // SMTP password
                $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
                $mail->Port = 465;
                $mail->setFrom($from_label);
                $mail->addAddress($email, 'Bookly Admin');
                $mail->isHTML(true);
                $mail->Subject = $subject;
                $mail->Body = $message;
                $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
             if($mail->send()){

                  }
Rizwan
  • 21
  • 3
  • Hi, @rizwan. Try to add some comments to your code so, the changes can be noticed at first glance. – Rarblack Oct 15 '18 at 13:54
  • 1
    first instal PHPMailere , the paset in codeigniter root directlly in plugin folder, then paste this code in your file where you send email , above the new php mailer paste this one require ("plugins/mailer/PHPMailerAutoload.php") – Rizwan Oct 15 '18 at 15:44