将表单数据发送到电子邮件codeigniter

时间:2016-08-19 08:58:45

标签: php email

我正在尝试将表单数据发送到电子邮件。功能运行完美,但电子邮件不会转到指定的电子邮件地址。我收到的答复为100但没有收到电子邮件。

我的控制器:

<?php
class Contact extends CI_Controller {

public function sendemail()
    {
        $this->load->model('Contactmodel');

        $response=array();
        $response=$this->Contactmodel->sendemail();
        echo json_encode($response);
    }
}
?>

我的模特

<?php
class Contactmodel extends CI_Model
{       
    public function _construct()
    {
        parent::_construct();
    }

    public function sendemail()
    {           
        $name = $_REQUEST['name'];
        $email = $_REQUEST['email'];
        $subject = $_REQUEST['subject'];
        $message = $_REQUEST['message'];

        $returnarray = array();
        $to_email = 'cse.akhilraj@gmail.com';
        $this->email->to($to_email);
        $this->email->from($email, $name);
        $this->email->subject($subject);
        $this->email->message($message);

        if($this->email->send())
        {
            $returnarray[0] = 100;
        }
        else
        {
            $returnarray[0] = 101;
        }

        return $returnarray;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

    <?php
    class Contactmodel extends CI_Model
    {

        public function _construct()
        {
            parent::_construct();
        }



public function sendemail()
    {
        $name = $_REQUEST['name'];
        $email = $_REQUEST['email'];
        $subject = $_REQUEST['subject'];
        $message = $_REQUEST['message'];

                $email_to= 'cse.akhilraj@gmail.com';

                $email_subject= 'Enquiry';

                $email_message = "Form details below.\n\n";

                function clean_string($string)
                {
                    $bad = array("content-type","bcc:","to:","cc:","href");
                    return str_replace($bad,"",$string);
                }

                $email_message .= "First Name: ".clean_string($name)."\n";
                $email_message .= "Email: ".clean_string($email)."\n";
                $email_message .= "Subject: ".clean_string($subject)."\n";
                $email_message .= "Comments: ".clean_string($message)."\n";

    // create email headers

        $headers = 'From: '.$email."\r\n".
                    'Reply-To: '.$email."\r\n" .
                    'X-Mailer: PHP/' . phpversion();

                $this->email->to($email_to);
                $this->email->from($headers);
                $this->email->subject($email_subject);
                $this->email->message($email_message);

                $returnarray = array();

        if($this->email->send())
        {
            $returnarray[0] = 100;
        }

        else

        {
            $returnarray[0] = 101;
        }

        return $returnarray;

    }
    }
    ?>