Send mail with CodeIgniter Email Library

六眼飞鱼酱① 提交于 2019-12-24 07:18:39

问题


I am getting error whenever I try sending email using code igniter. I have a form where users submits data if successful a welcome email is send to the user. Ajax file to pass the data to the controller

            $("#submit-details").on("submit", function(){
            $.ajax({
                type: 'POST',
                url: '<?php echo base_url() ?>'+'index.php/car/host ',
                data: $("#submit-details").serialize(),
                asyn: false,
            }).done(function (data) {
                console.log(data);

            });

            $("#submit-details")[0].reset();
            $(".contact-form, .form-btns").hide();
            $(".contact-confirm").show();
            return false;
        });

Here is my controller. public function host()

public function host()
{
         $field = array(
            'first_name' =>$this->input->post('first_name'),
            'last_name' =>$this->input->post('last_name'), 
            'email_address' =>$this->input->post('email_address'), 
            'cities' =>$this->input->post('cities'), 
            'registration_number' =>$this->input->post('registration_number'), 
            'vehicle_make' =>$this->input->post('vehicle_make'), 
            'vehicle_model' =>$this->input->post('vehicle_model'), 
            'vehicle_registration_year' =>$this->input->post('vehicle_registration_year'),  
         );

        $result = $this->Drivetrymodel->host($field);
        $this->Drivetrymodel->welcomeHostEmail($this->input->post('email_address');); //
        $msg['success'] = false;
        if ($result) {
            $msg['success'] = true;
        }

        echo json_encode($msg);
}

And here is my model.

public function welcomeHostEmail(){
    $from = "address@mail.com";    //senders email address
    $subject = 'Welcome !';  //email subject


    //sending email inside message body
    $message = $this->load->view("frontend/emails/welcomehost", TRUE);



    //config email settings
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'smtpout.secure.net';
    $config['smtp_port'] = '465';
    $config['smtp_user'] = $from;
    $config['smtp_pass'] = '*******';  //sender's password
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = 'TRUE';
    $config['newline'] = "\r\n"; 

    $this->load->library('email', $config);
    $this->email->initialize($config);
    //send email
    $this->email->from($from);
    $this->email->to($receiver);
    $this->email->subject($subject);
    $this->email->message($message);

    // $this->email->send();


    if($this->email->send()){
        //for testing
        echo "sent to: ".$receiver."<br>";
        echo "from: ".$from. "<br>";
        echo "protocol: ". $config['protocol']."<br>";
        echo "message: ".$message;
        return true;
    }else{
        echo "email send failed";
        return false;

    } 

    return $this->email->print_debugger(); 
}

If I run this is what I get.

<h4>A PHP Error was encountered</h4>
<p>Severity: 4096</p>
<p>Message:  Object of class CI_Loader could not be converted to string</p>
<p>Filename: libraries/Email.php</p>
<p>Line Number: 677</p>

if I try to convert the message to string by passing empty string like this

$message = $this->load->view("frontend/emails/welcomehost", "", TRUE);

I get a new error 500 (Internal server error).

来源:https://stackoverflow.com/questions/43676702/send-mail-with-codeigniter-email-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!