Codeigniter $ this-> email-> send()在mail()不起作用时不起作用

时间:2013-07-31 23:24:14

标签: php codeigniter email

我无法弄清楚为什么如果我尝试使用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

}

6 个答案:

答案 0 :(得分:24)

使用此设置电子邮件..

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

答案 1 :(得分:9)

我遇到了这个问题并找到了以下解决方案。只需对电子邮件配置进行一点改动,它就可以100%工作:

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

答案 2 :(得分:2)

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

答案 3 :(得分:2)

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

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

应用程序>控制器> 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_email email 的视图页面通过发布方法。 application> view> 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

答案 4 :(得分:1)

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

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

答案 5 :(得分:1)

我遇到了同样的问题,我尝试使用代码而不是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');

它工作正常。似乎有些服务器需要已经创建的电子邮件地址来发送电子邮件而其他服务器不是。

希望这是明确和有用的。