简体   繁体   English

无法在CodeIgniter中发送电子邮件

[英]can't send email in CodeIgniter

I am making a page to send emails and store into the database. 我正在制作一个页面以发送电子邮件并将其存储到数据库中。 I've tried and just managed to save in database, but can't be sent to email. 我已经尝试过并设法保存到数据库中,但是无法发送到电子邮件。

My Controller 我的控制器

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

and my model 和我的模特

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

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

How to solve it? 怎么解决呢?

Try this 尝试这个

In Controller 在控制器中

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

In Model 在模型中

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

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

Try this 尝试这个

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

    }

try this code that i've got from some free tutorial 尝试我从一些免费教程中获得的代码

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM