CodeIgniter + MAMP : unable to send emails

2019-06-09 05:45发布

问题:

I am trying to send emails with CI running on MAMP free. But it doesn't work, my script is encountering an infinite loop and nothing is happening... Do i need to set up something especially to send emails from localhost?

Here is my email config for CI:

    $config = array();
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
    $config['smtp_port'] = 465;
    $config['smtp_user'] = '*****@gmail.com';
    $config['smtp_pass'] = '******';
    $config['mailtype'] = 'html';
    $config['charset']  = 'utf-8';
    $config['newline']  = '\r\n';

Cheers

EDIT: Here is my code to send an email:

    $config = array();
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
    $config['smtp_port'] = 465;
    $config['smtp_user'] = '****';
    $config['smtp_pass'] = '*****';
    $config['mailtype'] = 'html';
    $config['charset']  = 'utf-8';
    $config['newline']  = '\r\n';

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

    $this->email->from('****');
    $this->email->to($email);
    $this->email->subject($title);
    $this->email->message($content);

    $this->email->send();

    error_log($this->email->print_debugger());

With $title, $content and $email vars defined in another part of my script. Don't worry about this stuff, I've already checked that my issue is not due to these.

回答1:

Finally I found the solution:

Send emails with MAMP (mail() PHP function)

  1. Set up SSL: http://soundsplausible.com/2012/01/14/enable-ssl-in-mamp-2-0-5/

  2. Set up Postfix: http://benjaminrojas.net/configuring-postfix-to-send-mail-from-mac-os-x-mountain-lion/

  3. Into MAMP's php.ini (glance phpinfo() to know which version is used and conclude into which folder you need to edit), comment lines SMTP, smtp_port and sendmail_from. Uncomment line sendmail_path and set /usr/sbin/sendmail -t -i as new value.

You should be able to send emails now, if PostFix works fine (run test given in tutorial above).

Send emails with CI

To send emails with CI, you don't need to write your logins into a file for PostFix. However, you need to be able to run PostFix and SSL.

Here is an example of config file for a google account:

    $config['protocol'] = "smtp";
    $config['smtp_host'] = "smtp.gmail.com";
    $config['smtp_port'] = "587";
    $config['smtp_user'] = "*****";
    $config['smtp_pass'] = "*****";
    $config['smtp_crypto'] = "tls"; //very important line, don't remove it
    $config['smtp_timeout'] = "5"; //google hint
    $config['mailtype'] = "text";
    $config['charset']  = "utf-8";
    $config['newline'] = "\r\n";

Be careful with " " which are necessary, ' ' could create issues. Here I'm using a TLS connection. If you prefer SSL, use port 465 and fix correctly smtp_crypto value.



回答2:

You need use sendmail(details how install it on MAMP). Or you can use solution below to store emails in localhost(something like emulate email sending). I'm use this solution on my localhost(XAMPP). Maybe it will be helpful for you.

Specify path for send mail in php.ini

sendmail_path = "path/to/php path/to/sendmail.php"

The second step - you can try to use this script

    define('DIR','path/to/sendmail_dir');
    $stream = '';
    $fp = fopen('php://stdin','r');
    while($t = fread($fp,2048)){
        if($t === chr(0)){
            break;
        }
        $stream .= $t;
    }
    fclose($fp);

    $fp = fopen(mkname(),'w');
    fwrite($fp,$stream);
    fclose($fp);

    function mkname($i=0){
       $fn = DIR.date('Y-m-d_H-i-s_').$i.'.eml';
       if (file_exists($fn)){
           return mkname(++$i);
       }   
       else{
           return $fn;
       }
   }