Codeigniter $this->email->send() not working while mail() does
Asked Answered
B

8

17

I can't figure out why if I try to use the CI Email Class it doesn't send emails, while if I use the native PHP mail() Class works.

Has to be noted that sometimes I get "email sent" while is not actually sent and sometimes I get the error "my server is not setup".

I tried to figure out how to set it up but... nothing...

Controller code follows:

 if($this->form_validation->run()){

                //Set Language
                $this->lang->load('site', $this->session->userdata('lang'));

                //Random key
                $user_valid_key = md5(uniqid());

                //Prepare email
                $this->load->library('email', array('mailtype' => 'html'));
                $this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
                $this->email->to($this->input->post('email'));
                $this->email->subject($this->lang->line('email_signup_subject'));

                //Format mail con %s per inserire i campi necessari
                $signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);

                $this->email->message((string)$signup_msg);

                if($this->email->send()){
                    //TODO: load view...
                    echo "email sent";
                }else{
                    $to = $this->input->post('email');
                    mail($to, 'test', 'Other sent option failed');
                    echo $this->input->post('email');
                    show_error($this->email->print_debugger());
                }

                //TODO: Add to db

            }else{

            // Form validation failed

}
Baulk answered 31/7, 2013 at 23:24 Comment(7)
I trust you've thoroughly inspected the Email Class?Verona
Yap. I'm now wondering if this is done online, does the "from email" have to be a domain email? (i.e: [email protected])?Baulk
I'm going to say yes. otherwise, you'd have to be a trusted sender in the domain's SPF record. Most [read: good] email providers will require authentication before establishing a connection to the SMTP server to send mail.Verona
Great. It works with the domain address. Now... Is there a way to set up ftp for outside address? I tried with email preferences in an array and that doesn't work, it just loads slowly and nothing happens.Baulk
FTP for Email preferences? That's another class all together.Verona
I'm sorry that is a typo... Should have been Is there a way to set up this for outside addresses?Baulk
let us continue this discussion in chatVerona
L
31

Use this setup email..

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

$config['protocol']    = 'smtp';
$config['smtp_host']    = 'ssl://smtp.gmail.com';
$config['smtp_port']    = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user']    = '[email protected]';
$config['smtp_pass']    = 'password';
$config['charset']    = 'utf-8';
$config['newline']    = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not      

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

$this->email->from('[email protected]', 'sender_name');
$this->email->to('[email protected]'); 
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();

echo $this->email->print_debugger();
Lovettalovich answered 17/10, 2013 at 12:41 Comment(6)
YES! WOW. I tried everything, from every answer from every variation on this site. Changing the config, again and again trying different systems. But that FINALLY did it. Thank you.Sheasheaf
this solution is not working all time. I have tried it.Eggshell
Works for testing, wouldn't use it in production!Gony
print_debugger() not printing anything.Luba
The docs specifically say if you want print_debugger to output anything you must call send(FALSE) so as not to flush the info.Outsize
Also, there is no field 'validation', it should be 'validate' although leaving it as the default FALSE also seems to perform smtp authentication.Outsize
J
13

I faced this problem and found the following solution. Just a little change in the email config and it's working 100%:

$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';
Jeff answered 30/4, 2015 at 10:6 Comment(0)
S
3

Codeigniter User Guide: https://www.codeigniter.com/user_guide/libraries/email.html

This setup works for me:

$config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'Your SMTP Server',
            'smtp_port' => 25,
            'smtp_user' => 'Your SMTP User',
            'smtp_pass' => 'Your SMTP Pass',
            'mailtype'  => 'html'
            );
$this->load->library('email', $config);
$this->email->set_newline("\r\n");

//Add file directory if you need to attach a file
$this->email->attach($file_dir_name);

$this->email->from('Sending Email', 'Sending Name');
$this->email->to('Recieving Email address'); 

$this->email->subject('Email Subject');
$this->email->message('Email Message'); 

if($this->email->send()){
   //Success email Sent
   echo $this->email->print_debugger();
}else{
   //Email Failed To Send
   echo $this->email->print_debugger();
}
Sivia answered 1/8, 2013 at 15:4 Comment(2)
Tks. I tried and just loads for more time (trying to access smtp?) and throws an error...Baulk
Worked for me too, i have added the config on the /application/config/email.phpOrtrude
I
2

I am using much time Run my configure code in localhost but it always gives me an error (Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.)

But when run this Below code in my server it works for me.

application>controller>Sendingemail_Controller.php

public function send_mail() {
    $this->load->library('email');   
    $config = array();
    $config['protocol']     = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
    $config['smtp_host']    = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
    $config['smtp_user']    = "[email protected]"; // client email gmail id
    $config['smtp_pass']    = "******"; // client password
    $config['smtp_port']    =  465;
    $config['smtp_crypto']  = 'ssl';
    $config['smtp_timeout'] = "";
    $config['mailtype']     = "html";
    $config['charset']      = "iso-8859-1";
    $config['newline']      = "\r\n";
    $config['wordwrap']     = TRUE;
    $config['validate']     = FALSE;
    $this->load->library('email', $config); // intializing email library, whitch is defiend in system

    $this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break

    $from_email = $this->input->post('f_email'); // sender email, coming from my view page 
    $to_email = $this->input->post('email'); // reciever email, coming from my view page
    //Load email library

    $this->email->from($from_email);
    $this->email->to($to_email);
    $this->email->subject('Send Email Codeigniter'); 
    $this->email->message('The email send using codeigniter library');  // we can use html tag also beacause use $config['mailtype'] = 'HTML'
    //Send mail
    if($this->email->send()){
        $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
        echo "email_sent";
    }
    else{
        echo "email_not_sent";
        echo $this->email->print_debugger();  // If any error come, its run
    }
}

and my view page where I defined f_email and email comes through post method. application>view>emailtesting.php

<html>
<head>    
    <title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)"  required />
<input type = "email" name = "email"  placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>

if some error comes again please visit official documentation below:

https://codeigniter.com/user_guide/libraries/email.html

Iminourea answered 13/2, 2020 at 12:44 Comment(0)
M
1

After fighting with this same problem for a couple hours I finally decided to change my config to send through a different server. My original server for some reason would send to some addresses but not others (in same domain). As soon as I changed to sendgrid it worked as expected.

If you are not getting the results you expect, try a different smtp server. The problem may not be your code...

Misusage answered 20/1, 2014 at 22:48 Comment(2)
I have the same problem. How did you fix this?Malo
As I said, try a different smtp server. You might also ask your host if there are restrictions in place. For example, GoDaddy only allows email to be sent through their server.Misusage
A
1

I had the same problem and I try below code instead of Codeignitor's mail function.

mail('[email protected]' , 'Test', 'Test Email');

It works and mail is send to the email address. That mail has sent by already created email address (As i think). In my case it is:

[email protected]

So I copy this email address and try it with below code.

$this->email->from('[email protected]', 'www.domainserver.com');

And it work fine. It seems to be some servers need already created email address to send the email while others are NOT.

Hope this is clear and helpful.

Alkalinity answered 8/1, 2016 at 6:9 Comment(0)
V
0

Lately, sending emails through Google SMTP can be very tricky due to their security checks.

Verify if:

  1. you have the rDNS on your server and it's matching your server IP
  2. disable the 2-step auth on the email you are using to send emails from (on Google settings)
  3. have IMAP and POP enabled on the email you are using to send emails from (on Google settings)
  4. allow less secure apps on the email you are using to send emails from (on Google settings)

Change the CI settings as follows:

    //Google settings for CI
    $config['protocol'] = 'ssmtp'; //smtp not working 
    $config['smtp_host'] = 'ssl://ssmtp.gmail.com'; //notice ssmtp
    $config['smtp_user'] = '[email protected]'; //must use Google
    $config['smtp_pass'] = '***********';
    $config['smtp_port'] = '465';
    $config['smtp_crypto'] = 'ssl';

    //general settings
    $config['_smtp_auth'] = TRUE; //important
    $config['smtp_timeout'] = 30;
    $config['charset'] = 'utf-8';
    $config['mailtype'] = 'html';
    //optional
    $config['wrapchars'] = 76;
    $config['wordwrap'] = TRUE;

I hope this will save you the 2 days I have struggled with this issue.

Vallo answered 1/7, 2022 at 17:37 Comment(0)
T
-2

I've spent hours trying to change the port,enabling the ssl extension in php.ini file etc nothing worked only ended up in a message SMTP server is not configured properly.I use WAMP as a localhost,when i turn off my antivirus avast the same config worked!!! the was send successfully.so it may be your antivirus program blocking on a local machine.

Tolu answered 14/8, 2021 at 16:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.