how to send email from localhost with php CodeIgni

2019-06-13 19:05发布

问题:

I am working on a php codeigniter project and I want to sent emails from my localhost.

Following is my controller functions.

        $config = Array(
              'protocol' => 'smtp',
              'smtp_host' => 'ssl://smtp.google.com',
              'smtp_port' => 465,
              'smtp_user' => 'sender@gmail.com',
              'smtp_pass' => 'password'
    );

    $this->load->library('email',$config);
    $this->email->set_newline("\r\n");

    $this->email->from("sender@gmail.com");
    $this->email->to("receiver@gmail.com");
    $this->email->subject("Email with Codeigniter");
    $this->email->message("This is email has been sent with Codeigniter");

    if($this->email->send())
    {
        echo "Your email was sent.!";
    } else {
        show_error($this->email->print_debugger());
    }

Note that I have enables the 'extension=php_openssl.dll' extension in php.ini. My php.ini file is located in C:/AppServ/php5. When I run the code, my page loads with errors.

These are the errors:

The following SMTP error was encountered: 1923818231 Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? Unable to send data: AUTH LOGIN Failed to send AUTH LOGIN command. Error: Unable to send data: MAIL FROM:

Severity: Warning

Message: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.

Filename: libraries/Email.php

Line Number: 705

回答1:

Use PHPMailer. Its available here PHPMailer. You can use it like this:

public function send_mail()
    {
        require_once(APPPATH.'third_party/PHPMailer-master/PHPMailerAutoload.php');
        $mail = new PHPMailer();
        $mail->IsSMTP(); // we are going to use SMTP
        $mail->SMTPAuth   = true; // enabled SMTP authentication
        $mail->SMTPSecure = "ssl";  // prefix for secure protocol to connect to the server
        $mail->Host       = "smtp.gmail.com";      // setting GMail as our SMTP server
        $mail->Port       = 465;                   // SMTP port to connect to GMail
        $mail->Username   = "mail@gmail.com";  // user email address
        $mail->Password   = "password";            // password in GMail
        $mail->SetFrom('mail@gmail.com', 'Mail');  //Who is sending 
        $mail->isHTML(true);
        $mail->Subject    = "Mail Subject";
        $mail->Body      = '
            <html>
            <head>
                <title>Title</title>
            </head>
            <body>
            <h3>Heading</h3>
            <p>Message Body</p><br>
            <p>With Regards</p>
            <p>Your Name</p>
            </body>
            </html>
        ';
        $destino = receiver@gmail.com; // Who is addressed the email to
        $mail->AddAddress($destino, "Receiver");
        if(!$mail->Send()) {
            return false;
        } else {
            return true;
        }
    }

Remember to set access for less trusted apps in your gmail account



回答2:

In your $config array, try this instead:

'smtp_host' => 'ssl://smtp.googlemail.com',


回答3:

You can use Codeigniter Library to send email from localhost and live server as follows:

$localhosts = array(
    '::1',
    '127.0.0.1',
    'localhost'
);

$protocol = 'mail';
if (in_array($_SERVER['REMOTE_ADDR'], $localhosts)) {
    $protocol = 'smtp';
}

$config = array(
    'protocol' => $protocol,
    'smtp_host' => 'ssl://smtp.googlemail.com',
    'smtp_port' => 465,
    'smtp_user' => 'Your-Email',
    'smtp_pass' => 'Your-Email-Password',
    'mailtype' => 'html',
    'starttls'  => true,
    'newline'   => "\r\n",
);

$this->load->library('email');
$this->email->initialize($config);
$this->email->from("From-Email");
$this->email->to("To-Email");
$this->email->subject("New user contacts");
$this->email->message($final_mail);
$flag = $this->email->send();

if($flag){
    echo "Email sent";
}else{
    echo "Email sending failed";
}

For more details refer to this article of Coderanks.com.