无法在CodeIgniter中发送电子邮件

时间:2015-11-23 02:43:31

标签: php codeigniter email

我正在制作一个页面来发送电子邮件并存储到数据库中。 我已经尝试过,只是设法保存在数据库中,但无法发送到电子邮件。

我的控制器

public function create() 
    {
        $data = array(
        'email' => $this->input->post('email',TRUE),
        'requestorname' => $this->input->post('requestorname',TRUE),
        'namemess' => $this->input->post('namemess',TRUE),
        'nomess' => $this->input->post('nomess',TRUE),
        );
    $this->load->model('mymodel');
    $this->mymodel->insert($data);
    $this->load->library('email');
    $config = array();
    $config['charset'] = 'iso-8859-1';
    $config['useragent'] = 'Codeigniter';
    $config['protocol']= "smtp";
    $config['mailtype']= "html";
    $config['smtp_host']= "sxxxxx.gridserver.com";
    $config['smtp_port']= "465";
    $config['smtp_user']= "noreply@mywebsite.com";
    $config['smtp_pass']= "mypass"; 
    $config['newline']="\r\n"; 
    $config['wordwrap'] = TRUE;

    $this->email->initialize($config);
    $subject = "Confirmation";
    $this->email->from($config['smtp_user']);
    $this->email->to('email'); 
    $this->email->subject($subject);
    $this->email->message('.$data['requestorname'].','.$data['namemess'].','.$data['nomess'].');
    $this->email->send();
    echo $this->email->print_debugger();
            redirect(site_url('page'));
        }
    }

和我的模特

public $table = 'mydatabase';
    public $id = 'id';
    public $order = 'DESC';

  // insert data
    function insert($data)
    {
        $this->db->insert($this->table, $data);
    }

如何解决?

3 个答案:

答案 0 :(得分:0)

试试这个

在控制器中

public function create() 
{
    $this->load->model('mymodel');
    $this->load->library('email');

    $data = array(
        'email'         => $this->input->post('email',TRUE),
        'requestorname' => $this->input->post('requestorname',TRUE),
        'namemess'      => $this->input->post('namemess',TRUE),
        'nomess'        => $this->input->post('nomess',TRUE),
    );

    $this->mymodel->insert_data($data);


    $config['charset'] = 'iso-8859-1';
    $config['useragent'] = '/usr/sbin/sendmail'; // change 'Codeigniter';
    $config['protocol']= 'sendmail'; // changed "smtp";
    $config['mailtype']= "html";
    $config['smtp_host']= "sxxxxx.gridserver.com";
    $config['smtp_port']= "465";
    $config['smtp_user']= "noreply@mywebsite.com";
    $config['smtp_pass']= "mypass"; 
    $config['newline']="\r\n"; 
    $config['wordwrap'] = TRUE;
    $this->email->initialize($config);

    $subject = "Confirmation";
    $message = $data['requestorname'].",".$data['namemess'].",".$data['nomess']; # added

    $this->email->from('noreply@mywebsite.com'); # Change
    $this->email->to($data['email']); # Change
    $this->email->subject($subject);  # Change
    $this->email->message($message); # Change

    if (!$this->email->send()) {  # Improved
        echo $this->email->print_debugger();
    }
    else{
        redirect('page');
    }
}

在模型中

public function insert_data($data) # Improved
{
    $data = array(
       'title' => 'My title' ,
       'name' => 'My Name' ,
       'date' => 'My date'
    );

    $this->db->insert('table_name', $data); 
}

答案 1 :(得分:0)

试试这个

$config = Array(   
        'protocol' => 'smtp',
        'mailpath' =>'/usr/sbin/sendmail',
        'smtp_host' => 'sxxxxx.gridserver.com',
        'smtp_port' => 25,
        'smtp_timeout' =>7,
        'smtp_user' => 'noreply@gridserver.com', // change it to yours
        'smtp_pass' => '******', // change it to yours
        'mailtype' => 'html',
        'charset' => 'utf-8',
        'crlf' => "\r\n",
        'newline' => "\r\n",
        'wordwrap' => TRUE
    );

   $this->email->initialize($config);
    $this->email->from('noreply@mywebsite.com'); 
    $this->email->to($data['email']);


    $this->email->subject($subject); #change
    $this->email->message($message); #change
    if($this->email->send())
    {

        echo "mail send";
    }
    else
    {
        show_error($this->email->print_debugger());

    }

答案 2 :(得分:0)

尝试使用我从一些免费教程

获得的代码
$config = Array(   
        'protocol' => 'smtp',
        'mailpath' =>'/usr/sbin/sendmail',
        'smtp_host' => 'sxxxxx.gridserver.com',
        'smtp_port' => 25,
        'smtp_timeout' =>7,
        'smtp_user' => 'noreply@gridserver.com', // change it to yours
        'smtp_pass' => '******', // change it to yours
        'mailtype' => 'html',
        'charset' => 'utf-8',
        'crlf' => "\r\n",
        'newline' => "\r\n",
        'wordwrap' => TRUE
    );

   $this->email->initialize($config);
    $this->email->from('noreply@mywebsite.com'); 
    $this->email->to($data['email']);


    $this->email->subject($subject); #change
    $this->email->message($message); #change
    if($this->email->send())
    {

        echo "mail send";
    }
    else
    {
        show_error($this->email->print_debugger());

    }