如何从php codeigniter中的localhost发送邮件?

时间:2014-01-20 06:06:18

标签: php codeigniter email

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

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

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

5 个答案:

答案 0 :(得分:0)

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

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

    }

答案 1 :(得分:0)

你可以用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';
              }
         }

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

这对我来说非常合适。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

第一阶段:从此网站 glob.com 下载sendmail。
第二阶段:解压缩sendmail zip文件并将其放在wampserver目录中。
第三阶段:打开sendmail文件夹,然后在记事本中选择sendmail.ini文件,并在其中更改一些行:
1.smtp_server = smtp.gmail.com
2.smtp_port = 465
3.smtp_ssl = ssl
4.default_domain =本地主机
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图标以启用ssl_module,选择apache / apache_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');

阶段十二:(重要)在设置/安全性中启用您的gmail帐户中的(安全性较低的应用程序访问权限。)

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

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

希望有用。