【问题标题】:Error when sending smtp email with google and codeigniter使用 google 和 codeigniter 发送 smtp 电子邮件时出错
【发布时间】:2011-04-28 12:01:09
【问题描述】:

尝试为我的网站设置重置密码功能,但是如果没有发生此错误,我无法通过发送电子邮件。

无法使用 PHP mail() 发送电子邮件。 您的服务器可能未配置为 使用此方法发送邮件。

我使用 gmail 作为主机发送电子邮件。这是用于发送电子邮件的函数部分。

$user_email = $this->input->post('email_address');

    $query = $this->db->get_where('account', array('email_address' => $user_email));
    if($query) {
        $config['protocal'] = 'smtp';
        $config['mail_path'] = 'ssl://smtp.googlemail.com';
        $config['smtp_host'] = 'ssl://smtp.googlemail.com';
        $config['smtp_port'] = '465';
        $config['smtp_user'] = 'USEREMAIL';
        $config['smtp_pass'] = 'PASSWORD';
        $config['charset'] = "utf-8";
        $config['mailtype'] = "html";
        $config['newline'] = "\r\n";

        $this->email->initialize($config);

        $this->email->from('matthew.attanasio135@gmail.com', 'Matthew');
        $this->email->to($user_email); 

        $this->email->subject('Email Test');
        $this->email->message('<h1>Testing the email class.<h1>');  

        $this->email->send();
        if ( ! $this->email->send()) {
            show_error($this->email->print_debugger());
        } 
        else {
            echo('DONE');        
        } 

我也收到此错误::

消息:未定义索引:主题

我不明白为什么会发生这种情况,请您帮帮我谢谢。

【问题讨论】:

    标签: php email codeigniter smtp gmail


    【解决方案1】:

    您尝试发送电子邮件两次,第一次设置了所有选项,第二次没有设置

    改变

        $this->email->send();
        if ( ! $this->email->send()) {
            show_error($this->email->print_debugger());
        }
    

        if ( ! $this->email->send()) {
            show_error($this->email->print_debugger());
        } 
    

    如果仍然存在,您应该会收到相关错误。

    编辑:

    还将$config['protocal'] 更改为$config['protocol'] 以解决发送问题

    【讨论】:

    • 感谢您的回复解决了这个问题,但我仍然收到The following SMTP error was encountered: 503 5.5.1 RCPT first. z10sm7618335wfj.12 502 5.5.1 Unrecognized command. z10sm7618335wfj.12 The following SMTP error was encountered: 502 5.5.1 Unrecognized command. z10sm7618335wfj.12 Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method.的错误
    • 尝试将$config['smtp_port'] = '465'更改为$config['smtp_port'] = 465
    • 还有$this-&gt;email-&gt;set_newline("\r\n");在你初始化()之后;根据这个线程codeigniter.com/forums/viewthread/84689 似乎有帮助
    • 添加了 $config['smtp_port'] = 465$this-&gt;email-&gt;set_newline("\r\n"); 的更改,仍然显示为 The following SMTP error was encountered: 555 5.5.2 Syntax error. s39sm7641969wfc.4 data: 503 5.5.1 RCPT first. s39sm7641969wfc.4 The following SMTP error was encountered: 503 5.5.1 RCPT first. s39sm7641969wfc.4 502 5.5.1 Unrecognized command. s39sm7641969wfc.4 The following SMTP error was encountered: 502 5.5.1 Unrecognized command. s39sm7641969wfc.4 Unable to send email using PHP SMTP. Your server might not be configured to send mail using this method. 的错误
    • 我发现了我的问题,这与实际上没有设置 $user_email 变量有关。现在一切都很好,并且可以按预期工作,感谢您的所有帮助。
    【解决方案2】:

    您能否提供用于发送电子邮件的其余功能,您发布的所有内容看起来都正确...Message: Undefined index: Subject 来自其他地方,可能会导致问题。

    另外...这可能看起来很明显,但您实际上已经在某处加载了电子邮件类,对吧 ($this-&gt;load-&gt;library('email);)... 而不仅仅是初始化它?

    【讨论】:

      【解决方案3】:

      试试这个

          $config = array('auth' => 'login',
              'username' => '***@gmail.com',
              'password' => '***password',
              'port' => '465',
              'ssl' => 'ssl');
      
      
          $request = $this->getRequest();
      
      
          if ($this->getRequest()->isPost()) {
              if ($form->isValid($request->getPost())) {
                  try {
                      $smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
                      $mail = new Zend_Mail();
                      $mail->setBodyText($form->getValue('body'));
                      $mail->setBodyHtml('');
                      $mail->setFrom();
                      $mail->addTo());
                      $mail->setSubject('');
                      $mail->send($smtpHost);
                  } catch (Exception $e) {
                      die($e);
                  }
              }
          }
      

      【讨论】:

      • 不,此代码使用 Zend 框架而不是 Codeigniter。
      猜你喜欢
      • 2015-11-10
      • 2018-03-23
      • 1970-01-01
      • 2015-12-28
      • 2012-11-21
      • 1970-01-01
      • 2015-11-24
      • 2012-01-12
      • 2018-12-08
      相关资源
      最近更新 更多