【问题标题】:Codeigniter $this->email->send() not working while mail() doesCodeigniter $this->email->send() 不工作,而 mail() 工作
【发布时间】:2013-08-01 16:56:38
【问题描述】:

我不知道为什么如果我尝试使用 CI 电子邮件类它不发送电子邮件,而如果我使用原生 PHP 邮件()类可以工作。

必须注意,有时我会收到“已发送电子邮件”,而实际上并未发送,有时我会收到错误消息“我的服务器未设置”。

我试图弄清楚如何设置它,但是……什么都没有……

控制器代码如下:

 if($this->form_validation->run()){

                //Set Language
                $this->lang->load('site', $this->session->userdata('lang'));

                //Random key
                $user_valid_key = md5(uniqid());

                //Prepare email
                $this->load->library('email', array('mailtype' => 'html'));
                $this->email->from($this->config->item('email_signup_from'), 'Wondermark.net');
                $this->email->to($this->input->post('email'));
                $this->email->subject($this->lang->line('email_signup_subject'));

                //Format mail con %s per inserire i campi necessari
                $signup_msg = sprintf($this->lang->line('email_signup_message'), $this->input->post('fname'), base_url().'main/signup_confirm/'.$user_valid_key);

                $this->email->message((string)$signup_msg);

                if($this->email->send()){
                    //TODO: load view...
                    echo "email sent";
                }else{
                    $to = $this->input->post('email');
                    mail($to, 'test', 'Other sent option failed');
                    echo $this->input->post('email');
                    show_error($this->email->print_debugger());
                }

                //TODO: Add to db

            }else{

            // Form validation failed

}

【问题讨论】:

  • 是的。我现在想知道这是否是在线完成的,“来自电子邮件”是否必须是域电子邮件? (即:info@mydomain.it)?
  • 我会说是的。否则,您必须是域的 SPF 记录中受信任的发件人。大多数 [read: good] 电子邮件提供商在建立与 SMTP 服务器的连接以发送邮件之前都需要进行身份验证。
  • 太棒了。它适用于域地址。现在...有没有办法为外部地址设置 ftp?我尝试在数组中使用电子邮件首选项,但这不起作用,它只是加载缓慢,没有任何反应。
  • 用于电子邮件首选项的 FTP? That's another class all together.

标签: php codeigniter email


【解决方案1】:

使用此设置电子邮件..

$this->load->library('email');

$config['protocol']    = 'smtp';
$config['smtp_host']    = 'ssl://smtp.gmail.com';
$config['smtp_port']    = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user']    = 'sender_mailid@gmail.com';
$config['smtp_pass']    = 'password';
$config['charset']    = 'utf-8';
$config['newline']    = "\r\n";
$config['mailtype'] = 'text'; // or html
$config['validation'] = TRUE; // bool whether to validate email or not      

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

$this->email->from('sender_mailid@gmail.com', 'sender_name');
$this->email->to('recipient@gmail.com'); 
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');  

$this->email->send();

echo $this->email->print_debugger();

【讨论】:

  • 是的!哇。我尝试了一切,从这个网站上每一个变化的每一个答案。更改配置,一次又一次尝试不同的系统。但最终做到了。谢谢。
  • 此解决方案并非一直有效。我试过了。
  • 用于测试,不会在生产中使用!
  • print_debugger() 不打印任何东西。
  • 文档特别说明如果您希望 print_debugger 输出任何您必须调用 send(FALSE) 以免刷新信息。
【解决方案2】:

我遇到了这个问题并找到了以下解决方案。只需对电子邮件配置稍作更改,即可 100% 正常工作:

$config['protocol'] = 'ssmtp';
$config['smtp_host'] = 'ssl://ssmtp.gmail.com';

【讨论】:

    【解决方案3】:

    Codeigniter 用户指南:https://www.codeigniter.com/user_guide/libraries/email.html

    此设置适合我:

    $config = Array(
                'protocol' => 'smtp',
                'smtp_host' => 'Your SMTP Server',
                'smtp_port' => 25,
                'smtp_user' => 'Your SMTP User',
                'smtp_pass' => 'Your SMTP Pass',
                'mailtype'  => 'html'
                );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    
    //Add file directory if you need to attach a file
    $this->email->attach($file_dir_name);
    
    $this->email->from('Sending Email', 'Sending Name');
    $this->email->to('Recieving Email address'); 
    
    $this->email->subject('Email Subject');
    $this->email->message('Email Message'); 
    
    if($this->email->send()){
       //Success email Sent
       echo $this->email->print_debugger();
    }else{
       //Email Failed To Send
       echo $this->email->print_debugger();
    }
    

    【讨论】:

    • Tks。我尝试了加载更多时间(尝试访问 smtp?)并引发错误...
    • 也为我工作,我在 /application/config/email.php 上添加了配置
    【解决方案4】:

    我使用了很多时间在 localhost 中运行我的配置代码,但它总是给我一个错误(无法使用 PHP SMTP 发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。 )

    但是当在我的服务器中运行下面的代码时,它对我有用。

    application>controller>Sendingemail_Controller.php

    public function send_mail() {
        $this->load->library('email');   
        $config = array();
        $config['protocol']     = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
        $config['smtp_host']    = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
        $config['smtp_user']    = "my@gmail.com"; // client email gmail id
        $config['smtp_pass']    = "******"; // client password
        $config['smtp_port']    =  465;
        $config['smtp_crypto']  = 'ssl';
        $config['smtp_timeout'] = "";
        $config['mailtype']     = "html";
        $config['charset']      = "iso-8859-1";
        $config['newline']      = "\r\n";
        $config['wordwrap']     = TRUE;
        $config['validate']     = FALSE;
        $this->load->library('email', $config); // intializing email library, whitch is defiend in system
    
        $this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
    
        $from_email = $this->input->post('f_email'); // sender email, coming from my view page 
        $to_email = $this->input->post('email'); // reciever email, coming from my view page
        //Load email library
    
        $this->email->from($from_email);
        $this->email->to($to_email);
        $this->email->subject('Send Email Codeigniter'); 
        $this->email->message('The email send using codeigniter library');  // we can use html tag also beacause use $config['mailtype'] = 'HTML'
        //Send mail
        if($this->email->send()){
            $this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
            echo "email_sent";
        }
        else{
            echo "email_not_sent";
            echo $this->email->print_debugger();  // If any error come, its run
        }
    }
    

    我定义 f_emailemail 的视图页面来自 post 方法。 应用程序>查看>emailtesting.php

    <html>
    <head>    
        <title> Send Email Codeigniter </title>
    </head>
    <body>
    <?php
    echo $this->session->flashdata('email_sent');
    echo form_open('/Sendingemail_Controller/send_mail');
    ?>
    <input type = "email" name = "f_email" placeholder="sender email id (from)"  required />
    <input type = "email" name = "email"  placeholder="reciever email id (to)" required />
    <input type = "submit" value = "SEND MAIL">
    <?php
    echo form_close();
    ?>
    </body>
    

    如果再次出现错误,请访问以下官方文档:

    https://codeigniter.com/user_guide/libraries/email.html

    【讨论】:

      【解决方案5】:

      在与同样的问题斗争了几个小时后,我最终决定更改我的配置以通过不同的服务器发送。由于某种原因,我的原始服务器会发送到某些地址,但不会发送到其他地址(在同一域中)。一旦我更改为 sendgrid,它就会按预期工作。

      如果您没有得到预期的结果,请尝试使用不同的 smtp 服务器。问题可能不是你的代码...

      【讨论】:

      • 我也有同样的问题。你是怎么解决这个问题的?
      • 正如我所说,尝试不同的 smtp 服务器。您也可以询问房东是否有限制。例如,GoDaddy 只允许通过他们的服务器发送电子邮件。
      【解决方案6】:

      我遇到了同样的问题,我尝试使用下面的代码而不是 Codeignitor 的邮件功能。

      mail('mypersonalmail@domainserver.com' , 'Test', 'Test Email');
      

      它工作正常,邮件被发送到电子邮件地址。该邮件已通过已创建的电子邮件地址发送(我认为)。在我的情况下是:

      gtt28651ff@p3plcpnl0552.srod.ahx3.domainserver.com
      

      所以我复制了这个电子邮件地址,然后用下面的代码试试。

      $this->email->from('gtt28651ff@p3plcpnl0552.srod.ahx3.domainserver.com', 'www.domainserver.com');
      

      而且效果很好。似乎有些服务器需要已经创建电子邮件地址才能发送电子邮件,而其他服务器则不需要。

      希望这是清晰和有帮助的。

      【讨论】:

        【解决方案7】:

        我花了好几个小时试图更改端口,在 php.ini 文件中启用 ssl 扩展名等,但没有任何效果,只是以消息 SMTP 服务器配置不正确。我使用 WAMP 作为 localhost,当我关闭时我的杀毒软件avast相同的配置工作!!!已成功发送。因此可能是您的防病毒程序阻止了本地计算机。

        【讨论】:

          猜你喜欢
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-13
          • 1970-01-01
          • 1970-01-01
          • 2012-09-04
          相关资源
          最近更新 更多