14

I am trying to setup SMTP on CodeIgniter. Everything is working fine and I recieve success message on page, that email is sent without errors. But, email is not delivered.

Here is the code, that I use:

$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'my-email@gmail.com', 
'smtp_pass' => '***', 
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'wordwrap' => TRUE
);
$this->load->library('email', $config);

$this->email->from('my-email@gmail.com', 'Explendid Videos');
$this->email->to('my-email@gmail.com');
$this->email->reply_to('my-email@gmail.com', 'Explendid Videos');


$this->email->subject('Explendid Video - Contact form');

$message = "Contact form\n\n";
$message .= "Name: ". $_POST['name'] . "\n";
$message .= "Phone: ". $_POST['phone'] . "\n";
$message .= "Email: ". $_POST['email'] . "\n";

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

$this->email->send();

What can be the reason, that e-mail is not actually delivered.

trejder
  • 17,148
  • 27
  • 124
  • 216
Wasif Khalil
  • 2,217
  • 9
  • 33
  • 58

7 Answers7

31

Change it to the following:

$ci = get_instance();
$ci->load->library('email');
$config['protocol'] = "smtp";
$config['smtp_host'] = "ssl://smtp.gmail.com";
$config['smtp_port'] = "465";
$config['smtp_user'] = "blablabla@gmail.com"; 
$config['smtp_pass'] = "yourpassword";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n";

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

$ci->email->from('blablabla@gmail.com', 'Blabla');
$list = array('xxx@gmail.com');
$ci->email->to($list);
$this->email->reply_to('my-email@gmail.com', 'Explendid Videos');
$ci->email->subject('This is an email test');
$ci->email->message('It is working. Great!');
$ci->email->send();
RobinCominotto
  • 959
  • 8
  • 18
  • 3
    thanks! it worked by changing "ssl://smtp.googlemail.com" to "ssl://smtp.gmail.com" – Wasif Khalil Jun 25 '13 at 13:20
  • 2
    This gives me `fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known`. Removing `ssl://` from the beginning of address worked. – machineaddict May 12 '14 at 11:11
  • I don't understand the problem mail() function running successfully but not get email why. How do I know the problem if no error? – heySushil Mar 28 '19 at 09:57
  • good...email sent but it appeared as suspicious sender because the originated domain sender isn't the spf & Dkim list – gumuruh Mar 31 '23 at 02:54
5

replace

$config['protocol'] = 'smtp';

to

$config['protocol'] = 'sendmail';
Saty
  • 22,443
  • 7
  • 33
  • 51
4

here is work for me on apache2 server, ci 2.1.4: its very simple: first create a file called email.php under your application/config directory then type the following code inside them~>

<?php
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = '465';
$config['smtp_user'] = 'u'r gmail account';
$config['smtp_pass'] = 'password of u'r account';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
?>

then create a file called email.php under your application/controllers directory then type this code~>

    <?php
    class Email extends CI_Controller
    {

    function send()
    {
    // Loads the email library
    $this->load->library('email');
    // FCPATH refers to the CodeIgniter install directory
    // Specifying a file to be attached with the email
    // if u wish attach a file uncomment the script bellow:
    //$file = FCPATH . 'yourfilename.txt';
    // Defines the email details
    $this->email->from('some@of.mailaddress', 'ur Name');
    $this->email->to('email@detiny.your');
    $this->email->subject('Email Test');
    $this->email->message('Testing the email class.');
    //also this script
    //$this->email->attach($file);
    // The email->send() statement will return a true or false
    // If true, the email will be sent
    if ($this->email->send()) {
    echo "you are luck!";
    } else {
    echo $this->email->print_debugger();
    }
    }

    }
    ?>
0

Have you checked your php.ini file? Try it. If not then perhaps you could also try SPF. SPF or Sender Policy Framework is a new technology that allows easy detection of spam. Gmail honours SPF unless you manually mark those emails as not spam. Regardless of this, if you have received emails on another address then they must have reached Gmail too. Check your spam thoroughly, as Gmail does not discard emails even on very high spam suspicion rather they end up in the Spam folder.

You can set up a SPF that allows your webserver to send emails which will result in Gmail accepting emails sent by your webserver as authentic. See http://www.mydigitallife.info/how-to-set-up-and-create-sender-policy-framework-spf-domain-dns-txt-record-with-wizard/ and a wizard from Microsoft.

animuson
  • 53,861
  • 28
  • 137
  • 147
pasujemito
  • 312
  • 4
  • 16
0

you can change this script, for debug your problem,

$this->email->send();

to

if($this->email->send())
{
    echo 'Your email was sent.';
}

else
{
    show_error($this->email->print_debugger());
}
0

Use the following code

And dont froget to unable following two security settings in google.

1) https://www.google.com/settings/security/lesssecureapps << turn it on

2) https://accounts.google.com/b/0/DisplayUnlockCaptcha << Click continue

** Turn off 2 step verification if you have it enabled.

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com',
        'smtp_port' => 465,
        'smtp_user' => 'dkumara85@gmail.com',    //email id
        'smtp_pass' => 'xxxxxxxxxxx',            // password
        'mailtype'  => 'html', 
        'charset'   => 'iso-8859-1'
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");

    $this->email->from('dkumara85@gmail.com','my name');
    $this->email->to("dkumara85@gmail.com"); // email array
    $this->email->subject('email subject');   
    $this->email->message("my mail body");

    $result = $this->email->send();


    show_error($this->email->print_debugger());  // for debugging purpose :: remove this once it works...
kuma DK
  • 1,812
  • 1
  • 18
  • 36
0

I just modified the code from RobinCominotto to make it work in office365.

PS: I got it working when placing it in a controller and calling this function exactly like this. When I place this configs on email.php (config file) does not work anymore :(

    $ci = get_instance();
    $ci->load->library('email');
    $config['protocol'] = "smtp";
    $config['smtp_host'] = "smtp.office365.com";
    $config['smtp_port'] = "587";
    $config['smtp_user'] = "<HERE COMES YOUR EMAIL>"; 
    $config['smtp_pass'] = "<HERE COMES THE PASSWORD OF EMAIL>";
    $config['charset'] = "utf-8";
    $config['mailtype'] = "html";
    $config['newline'] = "\r\n";

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

    $ci->email->from('<HERE COMES YOUR EMAIL>', 'Blabla');
    $list = array('<HERE COMES TO EMAIL>', '<HERE COMES TO EMAIL>');
    $ci->email->to($list);
    $this->email->reply_to('<HERE COMES YOUR EMAIL>', 'Explendid Videos');
    $ci->email->subject('This is an email test');
    $ci->email->message('It is working. Great!');
    $ci->email->send();
    print_r($ci->email->print_debugger());