【问题标题】:how to send mail from localhost in php codeigniter?如何在 php codeigniter 中从本地主机发送邮件?
【发布时间】:2014-01-20 06:06:17
【问题描述】:

我尝试发送邮件。它没有给出错误,但它没有发送邮件

        $config['smtp_host']  = "ssl://smtp.googlemail.com";
        $config['smtp_port']  = "456";
        $config['smtp_user']  = "mymailid@gmail.com";
        $config['smtp_pass']  = "mypassword";
        $this->email->initialize($config);

        $message = "my message";
        $this->email->from('mymailid@gmail.com'); 
        $this->email->to('recivermailid@gmail.com');
        $this->email->subject('Testing');
        $this->email->message($message);
        if($this->email->send())
        {
                echo 'Email sent.';
        }
        else
        {
                show_error($this->email->print_debugger());
        }

它返回“电子邮件发送”但邮件未送达

【问题讨论】:

  • 检查您的垃圾邮件文件夹,并尝试在配置中指定协议
  • 更改 localhost php.ini 文件中的 smtp 设置使用 gmail 发送电子邮件

标签: php codeigniter email


【解决方案1】:

试试这个经过测试的代码。

 function sendMail()
    {
       $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => 'xxx@gmail.com', 
      'smtp_pass' => 'xxx', 
      'mailtype' => 'html',
      'charset' => 'iso-8859-1',
      'wordwrap' => TRUE
    );

          $message = '';
          $this->load->library('email', $config);
          $this->email->set_newline("\r\n");
          $this->email->from('xxx@gmail.com');
          $this->email->to('xxx@gmail.com');
          $this->email->subject('Your Subject');
          $this->email->message($message);
          if($this->email->send())
         {
          echo 'Email sent.';
         }
         else
        {
         show_error($this->email->print_debugger());
        }

    }

【讨论】:

    【解决方案2】:

    你可以用 phpmailer 来做。你可以从这里下载它PHPMailer

    现在在你的函数中执行以下代码:

                 public function sendmail()
                    {
        require_once(APPPATH.'third_party/PHPMailer-master/PHPMailerAutoload.php');
    
                  $mail = new PHPMailer;
                  $mail->isSMTP();                            // Set mailer to use SMTP
                  $mail->Host = 'smtp.gmail.com';             // Specify main and backup SMTP servers
                  $mail->SMTPAuth = true;                     // Enable SMTP authentication
                  $mail->Username = 'Email Address';          // SMTP username
                  $mail->Password = 'Email Account Password'; // SMTP password
                  $mail->SMTPSecure = 'tls';                  // Enable TLS encryption, `ssl` also accepted
                  $mail->Port = 587;                          // TCP port to connect to
    
                  $mail->setFrom('info@codexworld.com', 'CodexWorld');
                  $mail->addReplyTo('info@codexworld.com', 'CodexWorld');
                  $mail->addAddress('john@gmail.com');   // Add a recipient
                  $mail->addCC('cc@example.com');
                  $mail->addBCC('bcc@example.com');
    
                  $mail->isHTML(true);  // Set email format to HTML
    
                  $bodyContent = '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
                  $bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';
    
                  $mail->Subject = 'Email from Localhost by CodexWorld';
                  $mail->Body    = $bodyContent;
    
                  if(!$mail->send()) {
                      echo 'Message could not be sent.';
                      echo 'Mailer Error: ' . $mail->ErrorInfo;
                  } else {
                      echo 'Message has been sent';
                  }
             }
    

    如果出现语法错误,请访问here

    这对我来说非常有效。

    【讨论】:

      【解决方案3】:

      您可以使用http://www.toolheap.com/test-mail-server-tool/ 它提供了在此页面上完成的所有必需配置。

      【讨论】:

        【解决方案4】:
            $config['protocol']    = 'smtp';
            $config['smtp_host']    = 'ssl://smtp.gmail.com';
            $config['smtp_port']    = '465';                            //ssl
            $config['smtp_timeout'] = '7';
            $config['smtp_user']    = 'xxxx@gmail.com'; //gmail id
            $config['smtp_pass']    = 'xxxx';           //gmail password
            $config['charset']    = 'utf-8';
            $config['newline']    = "\r\n";
            $config['mailtype'] = 'html'; 
            $config['validation'] = TRUE;
        

        【讨论】:

          【解决方案5】:

          如何在 localhost(wampserver) 中使用 php codeigniter 发送邮件 (sendmail)。

          第一阶段:从这个网站下载sendmail glob.com .
          第二阶段:解压 sendmail zip 文件并将其放入 wampserver 目录。
          第三阶段:打开 sendmail 文件夹并在记事本中选择 sendmail.ini 文件并在其中更改一些行:
          1.smtp_server=smtp.gmail.com
          2.smtp_port=465
          3.smtp_ssl=ssl
          4.default_domain=localhost
          5.error_logfile=error.log
          6.debug_logfile=debug.log
          7.auth_username=你的邮件(example@gmail.com)
          8.auth_password=您的邮件真实密码
          9.force_sender=你的邮件(example@gmail.com)
          10.hostname=localhost

          第四阶段:打开该地址(E:\wamp64\bin\apache\apache2.4.37\bin)下的php.ini文件
          第五阶段:在此处更改一些行并将 sendmail.exe 地址放入 sendmail_path 中,如下所示:
          ctrl+f -> 你必须搜索(邮件功能)并在 sendmail.ini 中找到它

          [mail function];
          For Win32 only.
          SMTP =
          smtp_port =
          ; For Win32 only.
          sendmail_from =
          ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
          sendmail_path ="E:\wamp64\sendmail\sendmail.exe -t -i"
          

          第六阶段:根据你安装wampserver的位置和你使用的php版本打开(E:\wamp64\bin\php\php7.2.14)地址中的php.ini
          第七阶段:在此处更改一些行:
          ctrl+f -> 你必须像下面这样搜索(邮件功能)。

          [mail function]
          ; For Win32 only.
          SMTP =
          smtp_port = 25
          ; For Win32 only.
          sendmail_from ="admin@wampserver.invalid"
          ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
          sendmail_path ="E:\wamp64\sendmail\sendmail.exe  -t -i"
          

          第八阶段:用桌面右下角的图标重新启动 wampserver。
          第九阶段:通过右键单击 wampserver 图标选择 apache/apache_module/ssl_module
          启用 ssl_module 第十阶段:通过右键单击 wampserver 图标选择 php/php_extensions/open_ssl 和 php/php_extensions/sockets
          启用 open_ssl 和套接字 再次重启 wampserver

          第 11 阶段:从控制器内部的 codeignter 中的视图中获取数据,并将这些代码保存在控制器中:

          if (isset($_POST['btn_send_contact_form'])) {
              
              $data = $_POST['frm'];
          
              $firstname = $data['firstname'];
              $lastname = $data['lastname'];
              $email = $data['email'];
              $msg_title = $data['msg_title'];
              $msg_text = $data['msg_text'];
          
              $this->load->library('email'); // email library must be included.
          
              $this->email->from($email, $firstname);
              $this->email->to('yourmail@gmail.com'); // the receiver mail
              // $this->email->cc('another@another-example.com');
              // $this->email->bcc('them@their-example.com');
              $this->email->subject($msg_title);
              $this->email->message($msg_text);
              
              $res = $this->email->send();
          
              if ($res) {
                  header("location: contact_us?contactUs_mail_sending=successful");
              } else {
                  header("location: contact_us?contactUs_mail_sending=error");
              }
              
          } 
          
          $this->load->view('pages/contact_us');
          

          第 12 阶段:(重要)在您的 gmail 帐户的设置/安全中启用(不太安全的应用访问)。

          第十三阶段:发送邮件成功。

          注意:如果一切正常,请尝试(在您的 gmail 帐户中启用不太安全的应用程序访问,虽然不建议这样做,但它可以让您发送一封简单的电子邮件(在 gmail 帐户中)。

          希望有用。

          【讨论】:

            猜你喜欢
            • 2016-11-02
            • 2013-08-19
            • 1970-01-01
            • 2012-01-28
            • 2015-08-22
            • 2016-01-31
            • 2015-01-25
            • 1970-01-01
            相关资源
            最近更新 更多