PHP Codeigniter - 电子邮件功能无法在服务器上运行

时间:2016-08-01 20:31:50

标签: php html codeigniter email codeigniter-2

我编写以下代码来发送来自localhost的电子邮件。它在我的本地主机上工作正常,但当我将文件上传到服务器时,它停止工作。以下是我的代码:

public function Sent_Confirmation_Email()
        {
        $emailid  = $this->uri->segment(3);

        $verificationLink = base_url() . 'MainController/Confirm_Activation/'.$emailid;

        $msg .= "Thank you for creating an account with us.<br /><br /><br />";
        $msg .= " Please <a href='".$verificationLink."' target='_blank'>Click Here</a> to verify your email.<br /><br /><br />";
        $msg .= "Kind regards,<br />";
        $msg .= "Real Animation Works.";


        if( ! ini_get('date.timezone') )
        {
        date_default_timezone_set('GMT');
        }        

        $config = Array('protocol' => 'smtp',
                        'smtp_host' => 'ssl://smtp.googlemail.com',
                        'smtp_port' => 465,
                        'smtp_user' => 'myemail@gmail.com',
                        'smtp_pass' => 'mypassword' 
        );

        $this->load->library('email',$config);
        $this->email->set_newline("\r\n");
        $this->email->set_mailtype("html");
        //$this->email->isHTML(true);
        $this->email->from("myemail@gmail.com");
        $this->email->to("$emailid");
        $this->email->subject("Email Confirmation - Courses and Tutors");
        $this->email->message($msg);

        if($this->email->send())
        {
        $this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
        redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid);
        } else {
        show_error($this->email->print_debugger());
        }
        }

以下是我运行代码时出现的错误的屏幕截图:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个

function Sent_Confirmation_Email()
{
    $this->load->library('email'); # Added

    $emailid  = $this->uri->segment(3);

    $verificationLink = base_url() . 'MainController/Confirm_Activation/'.$emailid;

    $msg .= "Thank you for creating an account with us.<br /><br /><br />";
    $msg .= " Please <a href='".$verificationLink."' target='_blank'>Click Here</a> to verify your email.<br /><br /><br />";
    $msg .= "Kind regards,<br />";
    $msg .= "Real Animation Works.";


    if( ! ini_get('date.timezone') )
    {
        date_default_timezone_set('GMT');
    }        

    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'ssl://smtp.googlemail.com';
    $config['smtp_port'] = '465';
    $config['smtp_user'] = 'myemail@gmail.com';
    $config['smtp_pass'] = 'password';
    $config['charset'] = 'iso-8859-1';  # Added
    $config['mailtype'] = 'html'; # Added
    $config['wordwrap'] = TRUE;

    $this->email->initialize($config);  # Added
    $this->email->set_newline("\r\n");

    $this->email->from("myemail@gmail.com");
    $this->email->to($emailid);  # Changed
    $this->email->subject("Email Confirmation - Courses and Tutors");
    $this->email->message($msg);

    if(!$this->email->send())  # Changed
    {
        echo $this->email->print_debugger();
    } 
    else 
    {
        $this->session->set_flashdata('msg', 'A confirmation email has been sent to ' . $emailid .'. Please activate your account using the link provided.');
        redirect(base_url() . 'MainController/EConfirmationPage/'.$emailid); # Not sure in this part.
    }
}