在codeigniter中发送邮件时出错?

时间:2017-08-25 09:18:16

标签: php codeigniter-3

发送邮件时出错帮我解决了这个问题。

遇到以下SMTP错误:0 无法使用PHP SMTP发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。

  

错误

     

User-Agent:CodeIgniter日期:星期五,2017年8月25日09:16:26 +0000来自:   " XXX"返回路径:收件人:xx@x.com回复:" XXX"    学科:   =?UTF-8] Q = 57 = 65 = 65 = 6B = 6C = 79 = 20 = 43 = 6F = 75 = 72 = 73 = 65 = 20 = 42 = 61 = 74 = 63 = 68 = 20 = 52 ?= =?utf-8?Q?= 65 = 70 = 6F = 72 = 74?= X-Sender:xx@xx.com X-Mailer:CodeIgniter X-Priority:3(正常)

CODE

    $config['protocol'] = "smtp";
    $config['smtp_host'] = "smtp.gmail.com";
    $config['smtp_port'] = 587;
    $config['smtp_user'] = "abc@gmail.com";
    $config['smtp_pass'] = "123";
    $config['charset'] = "utf-8";
    $config['mailtype'] = "html";
    $config['smtp_crypto'] = "ssl";
    $config['newline'] = "\r\n";
    $config['crlf'] = "\r\n";

2 个答案:

答案 0 :(得分:0)

您可以使用 SwiftMailer 通过Codeigniter发送电子邮件。

  • 首先使用this link
  • 从Github下载swiftmailer项目
  • 下载后,只将 lib 文件夹复制到codeigniter的文件夹,并将文件夹的名称重命名为swift_mailer(或任何内容)你希望)

  • 现在在Codeigniter的模型文件夹中创建一个名为 Model_Email_Sending.php

  • 的模型
  • 现在将此处给出的代码粘贴到该文件中。 (替换服务器的Cpanel提供的电子邮件和密码以及邮件端口)

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    require_once APPPATH . 'libraries/swift_mailer/swift_required.php';
    class Model_Email_Sending extends CI_Model
    {
       function __construct()
       {
          parent::__construct();
       }
      function send_applicant_information_to_mail($applicant_id,$password,$first_name,$last_name,$email_id)
      {
          $system_email = "noreply@yourdomain.com";
          $email_msg = "Welcome to yourdomain.com ".$first_name . ' '.$last_name ."<br />";
          $email_msg .= "Thanks For applying<br />";
          $email_msg .= "Your application ID : " . $applicant_id . "<br />";
          $email_msg .= "Your application Password : " . $password . "<br />";
          $email_msg .= "You can Login now using this credential and can check your status.<br />";
          $to_name = $first_name;
          $from = $system_email;
          $from_name = 'Domain TEAM';
          $email_sub = "Application Information";
          $email_to = $email_id;
    
          $this->do_email($email_msg, $email_sub, $email_to, $to_name, $from, $from_name);
      }
       function do_email($msg=NULL, $sub=NULL, $to=NULL, $to_name=NULL, $from=NULL, $from_name=NULL)
      {
          $headers  = 'MIME-Version: 1.0' . "\r\n";
          $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
          $headers .= 'To: '.$to_name.' <'.$to.'>' . "\r\n";
          $headers .= 'From: '.$from_name.' <'.$from.'>' . "\r\n";
    
          $from = 'noreply@yourdomain.com';
          $from_name = 'Domain TEAM';
          $fromsend = array($from => $from_name);
    
    
          $transport = Swift_SmtpTransport::newInstance('mail.yourdomain.com', 587); //By default Cpanel use this port for email . You have to find your domain's port yourself
          $transport->setUsername('noreply@yourdomain.com');
          $transport->setPassword('yourpassword');
          $swift = Swift_Mailer::newInstance($transport);
    
          $message = new Swift_Message($sub);
          $message->setFrom($fromsend);
          $message->setBody($msg, 'text/html');
          $message->setTo($to);
    
          if ($recipients = $swift->send($message, $failures))
          {
              return 'ok';
          } else {
              echo "There was an error:\n";
              print_r($failures);
              return 'no';
          }
    
          $tha = mail($to, $sub, $msg, $headers);
    
          if(!$tha){
              return 'no';
           } else {
              return 'ok';
          }
       }
    }
    

N.B:

  

您必须使用代码的演示版替换您的域名电子邮件和密码   电子邮件和密码。最重要的是GMAIL或其他电子邮件   提供商现在不支持SMTP。所以,你必须使用第三个   像SwiftMailer或SendGRID这样的派对提供者我使用了类似的文件名   到我的代码。所以请注意代码。

提供名为 send_applicant_information_to_mail()的演示函数,需要从Controller调用以发送邮件。在模型中自己编写与您相关的功能。

希望这对你有用:)

答案 1 :(得分:-1)

line 211出错

"classes\Swift\Transport\EsmtpTransport.php".

更改

return $this->params['sourceIp'] ?? null; 

通过

return $this->params['sourceIp'];

中也有丢失的文件

包:swift_init.phpswift_required_pear.php