我尝试使用具有以下配置的gmail服务器发送电子邮件。
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => '[email protected]',
'smtp_pass' => '2334444@',
'mailtype' => 'html',
'charset' => 'iso-8859-1',
'starttls' => true,
);
$this->load->library('email', $config);
$this->email->from('[email protected]', 'Dinuka Thilanga');
$this->email->to('[email protected]');
$this->email->subject('Email Test');
$this->email->message('Testing the email class.');
$this->email->send();
echo $this->email->print_debugger(); die;
我在提交表单后发送邮件。但是事情是页面永远不会停止重新加载。请告诉我问题是什么?
放入
$this->email->set_newline("\r\n");
之间
$this->load->library('email', $config);
$this->email->from('[email protected]', 'Dinuka Thilanga');
所以最终代码看起来类似于
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from('[email protected]', 'Dinuka Thilanga');
它对我有用。
我花了很多时间在localhost中运行我的配置代码,但它总是给我一个错误(无法使用PHP SMTP发送电子邮件。您的服务器可能未配置为使用此方法发送邮件。)
但是在我的[[server中运行下面的代码时,它对我有用。
应用程序>控制器> Sendingemail_Controller.php
public function send_mail() {
$this->load->library('email');
$config = array();
$config['protocol'] = "smtp"; // you can use 'mail' instead of 'sendmail or smtp'
$config['smtp_host'] = "ssl://smtp.googlemail.com";// you can use 'smtp.googlemail.com' or 'smtp.gmail.com' instead of 'ssl://smtp.googlemail.com'
$config['smtp_user'] = "[email protected]"; // client email gmail id
$config['smtp_pass'] = "******"; // client password
$config['smtp_port'] = 465;
$config['smtp_crypto'] = 'ssl';
$config['smtp_timeout'] = "";
$config['mailtype'] = "html";
$config['charset'] = "iso-8859-1";
$config['newline'] = "\r\n";
$config['wordwrap'] = TRUE;
$config['validate'] = FALSE;
$this->load->library('email', $config); // intializing email library, whitch is defiend in system
$this->email->set_newline("\r\n"); // comuplsory line attechment because codeIgniter interacts with the SMTP server with regards to line break
$from_email = $this->input->post('f_email'); // sender email, coming from my view page
$to_email = $this->input->post('email'); // reciever email, coming from my view page
//Load email library
$this->email->from($from_email);
$this->email->to($to_email);
$this->email->subject('Send Email Codeigniter');
$this->email->message('The email send using codeigniter library'); // we can use html tag also beacause use $config['mailtype'] = 'HTML'
//Send mail
if($this->email->send()){
$this->session->set_flashdata("email_sent","Congragulation Email Send Successfully.");
echo "email_sent";
}
else{
echo "email_not_sent";
echo $this->email->print_debugger(); // If any error come, its run
}
}
和我在其中定义和email的视图页面通过post方法访问。应用程序>视图> emailtesting.phpf_email
<html>
<head>
<title> Send Email Codeigniter </title>
</head>
<body>
<?php
echo $this->session->flashdata('email_sent');
echo form_open('/Sendingemail_Controller/send_mail');
?>
<input type = "email" name = "f_email" placeholder="sender email id (from)" required />
<input type = "email" name = "email" placeholder="reciever email id (to)" required />
<input type = "submit" value = "SEND MAIL">
<?php
echo form_close();
?>
</body>
如果再次出现错误,请访问下面的官方文档: