如何从php CodeIgniter从localhost发送电子邮件 [英] how to send email from localhost with php CodeIgniter

查看:160
本文介绍了如何从php CodeIgniter从localhost发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事php codeigniter项目工作,我想从本地主机发送电子邮件。



以下是我的控制器功能。

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

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

$ this-> email-> from(sender@gmail.com);
$ this-> email->至(receiver@gmail.com);
$ this-> email-> subject(Email with Codeigniter);
$ this-> email-> message(这是电子邮件已发送与Codeigniter);

if($ this-> email-> send())
{
echo您的电子邮件已发送!
} else {
show_error($ this-> email-> print_debugger());
}

请注意,我已经在php中启用了'extension = php_openssl.dll'的.ini。我的php.ini文件位于C:/ AppServ / php5。当我运行代码时,我的页面加载错误。



这些是错误:


遇到以下SMTP错误:1923818231无法找到
套接字传输ssl - 当您
配置PHP时,您是否忘记启用它?无法发送数据:AUTH LOGIN无法发送AUTH
LOGIN命令。错误:无法发送数据:MAIL FROM:



严重性:警告



消息:date():It依靠系统的时区
设置是不安全的。您需要 使用date.timezone设置或
date_default_timezone_set()函数。如果您使用这些
方法中的任何一种,并且您仍然收到此警告,那么您很可能
错误地标记了时区标识符。我们现在为
选择了时区'UTC',但请设置date.timezone来选择您的时区。



文件名:libraries / Email.php


行号:705



解决方案

使用PHPMailer的。它可以在这里 PHPMailer 。您可以这样使用:

  public function send_mail()
{
require_once(APPPATH。 THIRD_PARTY / PHPMailer的主/ PHPMailerAutoload.php');
$ mail = new PHPMailer();
$ mail-> IsSMTP(); //我们要使用SMTP
$ mail-> SMTPAuth = true; // enabled SMTP authentication
$ mail-> SMTPSecure =ssl; //安全协议连接到服务器的前缀
$ mail-> Host =smtp.gmail.com; //将GMail设置为SMTP服务器
$ mail-> Port = 465; //连接到Gmail的SMTP端口
$ mail->用户名=mail@gmail.com; //用户电子邮件地址
$ mail-> Password =password; // GMail中的密码
$ mail-> SetFrom(amail@gmail.com','邮件'); //谁发送
$ mail-> isHTML(true);
$ mail-> Subject =邮件主题;
$ mail-> Body ='
< html>
< head>
< title> Title< / title>
< / head>
< body>
< h3>标题< / h3>
< p>消息体< / p>< br>
< p> With Regards< / p>
< p>您的姓名< / p>
< / body>
< / html>
';
$ destino = receiver@gmail.com; //将邮件地址发送到
$ mail-> AddAddress($ destino,Receiver);
if(!$ mail-> Send()){
return false;
} else {
return true;
}
}

请记住在Gmail中设置不受信任的应用的访问权限帐户


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

解决方案

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

这篇关于如何从php CodeIgniter从localhost发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆