【问题标题】:Send email by using codeigniter library via localhost通过 localhost 使用 codeigniter 库发送电子邮件
【发布时间】:2013-09-06 08:44:53
【问题描述】:
public function sendemail(){
  $config = Array( 
  'protocol' => 'smtp', 
  'smtp_host' => 'ssl://smtp.googlemail.com', 
  'smtp_port' => 465, 
  'smtp_user' => 'email@gmail.com', 
  'smtp_pass' => 'password', ); 

  $this->load->library('email', $config); 
  $this->email->set_newline("\r\n");
  $this->email->from('email@gmail.com', 'Name');
  $this->email->to('email@yahoo.com');
  $this->email->subject(' My mail through codeigniter from localhost '); 
  $this->email->message('Hello World…');
  if (!$this->email->send()) {
    show_error($this->email->print_debugger()); }
  else {
    echo 'Your e-mail has been sent!';
  }
}   

我在使用 codeigniter 发送电子邮件时收到错误:

Message: mail() [function.mail]: Failed to connect to mailserver at 
"localhost" port 25, verify your "SMTP" and "smtp_port" setting in 
php.ini or use ini_set().

和

Unable to send email using PHP mail(). Your server might not be 
configured to send mail using this method.

我做错了什么?

【问题讨论】:

    标签: codeigniter email


    【解决方案1】:
    $insert = $this->db->insert('email_notification', $data);
                    $this->session->set_flashdata("msg", "<div class='alert alert-success'> Cafe has been added Successfully.</div>");
    
                    //require ("plugins/mailer/PHPMailerAutoload.php");
                    $mail = new PHPMailer;
                    $mail->SMTPOptions = array(
                        'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true,
                    ),
                    );
    
                    $message="
                         Your Account Has beed created successfully by Admin:
                        Username: ".$this->input->post('username')." <br><br>
                        Email: ".$this->input->post('sender_email')." <br><br>
                        Regargs<br>
                        <div class='background-color:#666;color:#fff;padding:6px;
                        text-align:center;'>
                             Bookly Admin.
                        </div>
                    ";
                    $mail->isSMTP(); // Set mailer to use SMTP
                    $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
                    $mail->SMTPAuth = true; 
                    $subject = "Hello  ".$this->input->post('username');
                    $mail->SMTDebug=2;
                    $email = $this->input->post('sender_email'); //this email is user email
                    $from_label = "Account Creation";
                    $mail->Username = 'your email'; // SMTP username
                    $mail->Password = 'password'; // SMTP password
                    $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
                    $mail->Port = 465;
                    $mail->setFrom($from_label);
                    $mail->addAddress($email, 'Bookly Admin');
                    $mail->isHTML(true);
                    $mail->Subject = $subject;
                    $mail->Body = $message;
                    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
                 if($mail->send()){
    
                      }
    

    【讨论】:

    • 嗨,@rizwan。尝试将一些 cmets 添加到您的代码中,以便第一眼就能注意到更改。
    • 首先安装 PHPMailere ,codeigniter root 中的paset 直接在插件文件夹中,然后将此代码粘贴到您发送电子邮件的文件中,在新的 php 邮件程序上方粘贴这个需要 ("plugins/mailer/PHPMailerAutoload .php")
    【解决方案2】:

    请检查我的工作代码。

    function sendMail()
    {
        $config = Array(
      'protocol' => 'smtp',
      'smtp_host' => 'ssl://smtp.googlemail.com',
      'smtp_port' => 465,
      'smtp_user' => 'xxx@gmail.com', // change it to yours
      'smtp_pass' => 'xxx', // change it to yours
      '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'); // change it to yours
          $this->email->to('xxx@gmail.com');// change it to yours
          $this->email->subject('Resume from JobsBuddy for your Job posting');
          $this->email->message($message);
          if($this->email->send())
         {
          echo 'Email sent.';
         }
         else
        {
         show_error($this->email->print_debugger());
        }
    
    }
    

    【讨论】:

    • localhost有没有配置可以发邮件?
    • 是的,你必须在你的本地主机中启用 openssl
    • 为什么每次我更改 $this->email->from('xxx@gmail.com');到雅虎等其他地址,我收到的电子邮件总是从“我”而不是发件人发送?
    • 那么我如何不仅使用 gmail 还使用其他电子邮件服务发送电子邮件?
    • 因为你可以从任何服务器中的 PHP 邮件选项发送它,但不能在本地主机上
    猜你喜欢
    • 2018-03-18
    • 2014-07-22
    • 2018-12-10
    • 2016-04-16
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2012-12-07
    相关资源
    最近更新 更多