Код не отправляет электронное письмо получателю ………………………………
class Welcome extends Frontend_Controller {
public function __construct() {
parent::__construct();
public function Subscribe_Mail() {
$this->_Creating_subscription_mail();
if ($this->form_validation->run() === TRUE) {
$email = $this->input->post('email');
$to_email = "zzz@gmail.com";
$from_email = trim($email);
$config['useragent'] = "CodeIgniter";
$config['protocol'] = "Send mail";
$config['SMTPSecure'] = 'ssl';
$config['smtp_host'] = 'mail.XXX.com';
$config['smtp_user'] = 'rrrrr';
$config['smtp_pass'] = 'xxxxxxxxxxxxxxxxxxx';
$config['mailpath'] = '/usr/bin/sendemail';
$config['smtp_port'] = '587';
$config['smtp_timeout'] = '5';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['mailtype'] = 'html';
$config['validation'] = TRUE;
$config['web_admin_email_id'] = 'support@handwritingiih.com';
$this->load->library('email');
$this->email->initialize($config);
$data = array();
$data['email'] = $email;
$this->email->from('xxx@gmail.com', 'Servhigh');
$this->email->to('$to_email');
$this->email->subject('Request Email On Servhigh.com');
$this->email->message('This is my message');
$this->email->send();
Я хочу знать, что не так с этим кодом в контроллере.
$this->email->to('$to_email');
исправить это
$this->email->to($to_email);
Пример:
$data ="hai";
echo "$data"; // output:hai
echo '$data'; // output:$data
echo $data; //output:hai
$config['mailpath'] = '/usr/bin/sendemail';
замещать
$config['mailpath'] = "/usr/bin/sendmail";
Используйте это, это работает для меня
function send_mail()
{
$this->load->library('email');
$config = array();
$config['useragent'] = "CodeIgniter";
$config['mailpath'] = "/usr/bin/sendmail";
$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.sendgrid.net";
$config['smtp_user'] = "your_user_name";
$config['smtp_pass'] = "your_password";
$config['smtp_port'] = "25";
$config['mailtype'] = 'html';
$config['charset'] = 'utf-8';
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$this->load->library('email');
$this->email->initialize($config);
$this->email->subject('TEST SUBJECT');
$this->email->message("THIS IS A TEST MESSAGE");
$this->email->from( "yourmailid" );
$this->email->to("yourmailid");
if($this->email->send())
{
echo "success";
}
else
{
echo $this->email->print_debugger();
}
}
Сначала проверьте, пытается ли он отправить или нет, добавив это в конце:
if(!$this->email->send()){
echo "Failed to send mail":
} else {
echo "Working... Mail Send";
}
И заменить это:
$this->email->to('$to_email');
к этому:
$this->email->to($to_email);
Пожалуйста, попробуйте следующий код (Пожалуйста, измените учетные данные в соответствии с вашими требованиями):
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function subscribe_mail() {
$config['smtp_host'] = 'mail.xxx.com';
$config['smtp_user'] = 'rrrrr';
$config['smtp_pass'] = 'xxxxxxxxxxxxxxxxxxx';
$config['charset'] = 'iso-8859-1';
$config['crlf'] = "\r\n";
$config['newline'] = "\r\n";
$config['mailtype'] = 'html';
$config['validate'] = TRUE;
$this->load->library('email');
$this->email->initialize($config);
$this->email->from('your@example.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->subject('Request Email On Servhigh.com');
$this->email->message($this->input->post('email'));
if($this->email->send()) {
echo "success";
}
else {
echo $this->email->print_debugger();
}
}
}
Прежде всего, я изменил название метода с Subscribe_Mail()
в subscribe_mail()
,
Во-вторых, я удалил if ($this->form_validation->run() === TRUE) {
так как я не нашел никаких правил валидации.
От руководство:
Поскольку вы не сказали классу проверки формы что-либо проверить
тем не менее, он возвращает FALSE (логическое значение false) по умолчанию.run()
метод
возвращает TRUE, если он успешно применил ваши правила без
любой из них терпит неудачу.
что в основном подразумевает, что если не определено правило валидации и если валидация НЕ удалась, run()
метод всегда вернет FALSE
, тем самым предотвращая код внутри if
заявление от исполнения.
В-третьих, я удалил некоторые ненужные конфиги (которые уже имеют указанное значение по умолчанию).
В-четвертых, я изменился Frontend_Controller
в CI_Controller
и удалил $this->_Creating_subscription_mail();
,
В-пятых, я исправил некоторые {}
проблемы.
Примечание: хотя я удалил часть проверки для удобства, в реальной реализации вы должны проверить данные перед обработкой.