Привет, у меня есть форма запроса на моем веб-сайте, как только я отправлю данные в форме запроса, не могу отправить электронное письмо администратору относительно запроса. Это перенаправление функции приветствия / запроса, не получая никаких ошибок
контроллер:
class Welcome extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form','url')); $this->load->library(array('session', 'form_validation', 'email')); } public function index() { $this->load->model('index_model'); $data['records2'] = $this->index_model->get_all_banners(); $data['mainpage'] = "index"; $this->load->view('templates/template',$data); } function request() { $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('<br /><span class="error"> ','</span>'); $this->form_validation->set_rules('name','First Name' , 'required'); $this->form_validation->set_rules('email','Email'); $this->form_validation->set_rules('phone','Mobile Number'); $this->form_validation->set_rules('description','Description'); if($this->form_validation->run()== FALSE) { $data['mainpage']='index'; $this->load->view('templates/template',$data); } else { //get the form data $name = $this->input->post('name'); $from_email = $this->input->post('email'); $subject = $this->input->post('phone'); $message = $this->input->post('description'); //set to_email id to which you want to receive mails $to_email = 'xxxxx@gmail.com'; $config=Array( 'protocol'=> 'smtp', 'smtp_host' => 'ssl://smtp.gmail.com', //smtp host name 'smtp_port' => '465', //smtp port number 'smtp_user' => 'xxx@gmail.com', 'smtp_pass' => '*******', //$from_email password 'mailtype' =>'html', 'charset' => 'iso-8859-1', 'wordwrap' => TRUE ); //send mail $this->load->library('email',$config); $this->email->from($name); $this->email->to('$to_email'); $this->email->subject($subj); $this->email->message($messagess1); $this->email->set_newline("\r\n"); if ($this->email->send()) { // mail sent $this->session->set_flashdata('msg','<div class="alert alert-success text-center">Your mail has been sent successfully!</div>'); redirect('welcome'); } else { //error $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">There is error in sending mail! Please try again later</div>'); redirect('welcome'); } } } }
Скачать: codeigniter-phpmailer
public function send_mail_ctrl() { $config['useragent'] = 'PHPMailer'; // Mail engine switcher: 'CodeIgniter' or 'PHPMailer' $config['protocol'] = 'smtp'; // 'mail', 'sendmail', or 'smtp' $config['mailpath'] = '/usr/sbin/sendmail'; $config['smtp_host'] = 'smtp.gmail.com'; $config['smtp_user'] = 'youremail'; $config['smtp_pass'] = 'password'; $config['smtp_port'] = 465; $config['smtp_timeout'] = 30; // (in seconds) $config['smtp_crypto'] = 'ssl'; // '' or 'tls' or 'ssl' $config['smtp_debug'] = 0; // PHPMailer's SMTP debug info level: 0 = off, 1 = commands, 2 = commands and data, 3 = as 2 plus connection status, 4 = low level data output. $config['smtp_auto_tls'] = false; // Whether to enable TLS encryption automatically if a server supports it, even if `smtp_crypto` is not set to 'tls'. $config['smtp_conn_options'] = array(); // SMTP connection options, an array passed to the function stream_context_create() when connecting via SMTP. $config['wordwrap'] = true; $config['wrapchars'] = 76; $config['mailtype'] = 'html'; // 'text' or 'html' $config['charset'] = null; // 'UTF-8', 'ISO-8859-15', ...; NULL (preferable) means config_item('charset'), ie the character set of the site. $config['validate'] = true; $config['priority'] = 3; // 1, 2, 3, 4, 5; on PHPMailer useragent NULL is a possible option, it means that X-priority header is not set at all, see https://github.com/PHPMailer/PHPMailer/issues/449 $config['crlf'] = "\n"; // "\r\n" or "\n" or "\r" $config['newline'] = "\n"; // "\r\n" or "\n" or "\r" $config['bcc_batch_mode'] = false; $config['bcc_batch_size'] = 200; $config['encoding'] = '8bit'; $this->load->library('email'); $this->email->initialize($config); /*$subject = 'This is a test'; $message = '<p>This message has been sent for testing purposes.</p>';*/ $message = $this->input->post('message'); $email = $this->input->post('email'); $subject = $this->input->post('subject'); // Get full html: $body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=' . strtolower(config_item('charset')) . '" /> <title>' . html_escape($subject) . '</title> <style type="text/css"> body { font-family: Arial, Verdana, Helvetica, sans-serif; font-size: 16px; } </style> </head> <body> ' . $message . ' </body> </html>'; // Also, for getting full html you may use the following internal method: //$body = $this->email->full_html($subject, $message); $result = $this->email ->from('your_email@gmail.com') ->reply_to('any_email@gmail.com') // Optional, an account where a human being reads. ->to($email) ->subject($subject) ->message($body) ->send(); var_dump($result); echo '<br />'; //echo $this->email->print_debugger(); if ($result == TRUE) { // mail sent echo "<div class='alert alert-success text-center'>Your mail has been sent successfully!</div>"; } else { echo'<div class="alert alert-danger text-center">Error in sending your message! Please try again later.</div>'; } exit; }
Попробуйте использовать PHP Mailer и Gmail как SMTP-сервер и разрешить менее безопасное приложение в gmail. Смотрите пример кода здесь