【问题标题】:how to send email from localhost with php CodeIgniter如何使用 php CodeIgniter 从本地主机发送电子邮件
【发布时间】:2016-11-02 20:08:04
【问题描述】:

我正在开发一个 php codeigniter 项目,我想从我的本地主机发送电子邮件。

以下是我的控制器功能。

        $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());
    }

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

这些是错误:

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

严重性:警告

消息:date():依赖系统的时区是不安全的 设置。您必须使用 date.timezone 设置或 date_default_timezone_set() 函数。如果您使用了其中任何一个 方法并且您仍然收到此警告,您很可能 时区标识符拼写错误。我们选择了时区“UTC” 现在,但请设置 date.timezone 以选择您的时区。

文件名:libraries/Email.php

行号:705

【问题讨论】:

  • 您是否在 PHP 中激活了php_openssl 扩展?通过编辑php.ini 并删除# 评论来激活它
  • 我在 php.ini 中启用了 'extension=php_openssl.dll' 扩展。我的 php.ini 文件位于 C:/AppServ/php5 和 C:/AppServ/php7 文件夹中
  • wherever\apache\bin 中那个很可能被网络服务器使用的那个呢
  • 这可能对youtube.com/watch?v=TO7MfDcM-Ho有帮助

标签: php codeigniter email localhost


【解决方案1】:

您可以使用 Codeigniter 库从本地主机和实时服务器发送电子邮件,如下所示:

$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";
}

更多详情请参阅 Coderanks.com 的article

【讨论】:

    【解决方案2】:

    在你的 $config 数组中,试试这个:

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

    【讨论】:

      【解决方案3】:

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

      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;
              }
          }
      

      记得在您的 gmail 帐户中为不太受信任的应用设置访问权限

      【讨论】:

      • 但是要回答这个问题......请确保您已激活 Apache/PHP 使用的 php.ini 文件中的 php_openssl 扩展名
      猜你喜欢
      • 2015-08-22
      • 2018-05-20
      • 2015-01-03
      • 2016-07-22
      • 2012-01-28
      相关资源
      最近更新 更多