如何使用 php CodeIgniter 从本地主机发送电子邮件
how to send email from localhost with php CodeIgniter
我正在开发一个 php codeigniter 项目,我想从我的本地主机发送电子邮件。
以下是我的控制器功能。
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.google.com',
'smtp_port' => 465,
'smtp_user' => 'sender@gmail.com',
'smtp_pass' => 'password'
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from("sender@gmail.com");
$this->email->to("receiver@gmail.com");
$this->email->subject("Email with Codeigniter");
$this->email->message("This is email has been sent with Codeigniter");
if($this->email->send())
{
echo "Your email was sent.!";
} else {
show_error($this->email->print_debugger());
}
请注意,我在 php.ini 中启用了 'extension=php_openssl.dll' 扩展。我的 php.ini 文件位于 C:/AppServ/php5。当我 运行 代码时,我的页面加载时出现错误。
这些是错误:
The following SMTP error was encountered: 1923818231 Unable to find
the socket transport "ssl" - did you forget to enable it when you
configured PHP? Unable to send data: AUTH LOGIN Failed to send AUTH
LOGIN command. Error: Unable to send data: MAIL FROM:
Severity: Warning
Message: date(): It is not safe to rely on the system's timezone
settings. You are required to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those
methods and you are still getting this warning, you most likely
misspelled the timezone identifier. We selected the timezone 'UTC' for
now, but please set date.timezone to select your timezone.
Filename: libraries/Email.php
Line Number: 705
使用 PHPMailer。可在此处获得 PHPMailer。你可以这样使用它:
public function send_mail()
{
require_once(APPPATH.'third_party/PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // we are going to use SMTP
$mail->SMTPAuth = true; // enabled SMTP authentication
$mail->SMTPSecure = "ssl"; // prefix for secure protocol to connect to the server
$mail->Host = "smtp.gmail.com"; // setting GMail as our SMTP server
$mail->Port = 465; // SMTP port to connect to GMail
$mail->Username = "mail@gmail.com"; // user email address
$mail->Password = "password"; // password in GMail
$mail->SetFrom('mail@gmail.com', 'Mail'); //Who is sending
$mail->isHTML(true);
$mail->Subject = "Mail Subject";
$mail->Body = '
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>Heading</h3>
<p>Message Body</p><br>
<p>With Regards</p>
<p>Your Name</p>
</body>
</html>
';
$destino = receiver@gmail.com; // Who is addressed the email to
$mail->AddAddress($destino, "Receiver");
if(!$mail->Send()) {
return false;
} else {
return true;
}
}
记得在您的 gmail 帐户中为不太受信任的应用程序设置访问权限
在您的 $config
数组中,试试这个:
'smtp_host' => 'ssl://smtp.googlemail.com',
您可以使用 Codeigniter 库从本地主机和实时服务器发送电子邮件,如下所示:
$localhosts = array(
'::1',
'127.0.0.1',
'localhost'
);
$protocol = 'mail';
if (in_array($_SERVER['REMOTE_ADDR'], $localhosts)) {
$protocol = 'smtp';
}
$config = array(
'protocol' => $protocol,
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'Your-Email',
'smtp_pass' => 'Your-Email-Password',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n",
);
$this->load->library('email');
$this->email->initialize($config);
$this->email->from("From-Email");
$this->email->to("To-Email");
$this->email->subject("New user contacts");
$this->email->message($final_mail);
$flag = $this->email->send();
if($flag){
echo "Email sent";
}else{
echo "Email sending failed";
}
有关详细信息,请参阅 Coderanks.com 的 article。
我正在开发一个 php codeigniter 项目,我想从我的本地主机发送电子邮件。
以下是我的控制器功能。
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.google.com',
'smtp_port' => 465,
'smtp_user' => 'sender@gmail.com',
'smtp_pass' => 'password'
);
$this->load->library('email',$config);
$this->email->set_newline("\r\n");
$this->email->from("sender@gmail.com");
$this->email->to("receiver@gmail.com");
$this->email->subject("Email with Codeigniter");
$this->email->message("This is email has been sent with Codeigniter");
if($this->email->send())
{
echo "Your email was sent.!";
} else {
show_error($this->email->print_debugger());
}
请注意,我在 php.ini 中启用了 'extension=php_openssl.dll' 扩展。我的 php.ini 文件位于 C:/AppServ/php5。当我 运行 代码时,我的页面加载时出现错误。
这些是错误:
The following SMTP error was encountered: 1923818231 Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? Unable to send data: AUTH LOGIN Failed to send AUTH LOGIN command. Error: Unable to send data: MAIL FROM:
Severity: Warning
Message: date(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.
Filename: libraries/Email.php
Line Number: 705
使用 PHPMailer。可在此处获得 PHPMailer。你可以这样使用它:
public function send_mail()
{
require_once(APPPATH.'third_party/PHPMailer-master/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // we are going to use SMTP
$mail->SMTPAuth = true; // enabled SMTP authentication
$mail->SMTPSecure = "ssl"; // prefix for secure protocol to connect to the server
$mail->Host = "smtp.gmail.com"; // setting GMail as our SMTP server
$mail->Port = 465; // SMTP port to connect to GMail
$mail->Username = "mail@gmail.com"; // user email address
$mail->Password = "password"; // password in GMail
$mail->SetFrom('mail@gmail.com', 'Mail'); //Who is sending
$mail->isHTML(true);
$mail->Subject = "Mail Subject";
$mail->Body = '
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>Heading</h3>
<p>Message Body</p><br>
<p>With Regards</p>
<p>Your Name</p>
</body>
</html>
';
$destino = receiver@gmail.com; // Who is addressed the email to
$mail->AddAddress($destino, "Receiver");
if(!$mail->Send()) {
return false;
} else {
return true;
}
}
记得在您的 gmail 帐户中为不太受信任的应用程序设置访问权限
在您的 $config
数组中,试试这个:
'smtp_host' => 'ssl://smtp.googlemail.com',
您可以使用 Codeigniter 库从本地主机和实时服务器发送电子邮件,如下所示:
$localhosts = array(
'::1',
'127.0.0.1',
'localhost'
);
$protocol = 'mail';
if (in_array($_SERVER['REMOTE_ADDR'], $localhosts)) {
$protocol = 'smtp';
}
$config = array(
'protocol' => $protocol,
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'Your-Email',
'smtp_pass' => 'Your-Email-Password',
'mailtype' => 'html',
'starttls' => true,
'newline' => "\r\n",
);
$this->load->library('email');
$this->email->initialize($config);
$this->email->from("From-Email");
$this->email->to("To-Email");
$this->email->subject("New user contacts");
$this->email->message($final_mail);
$flag = $this->email->send();
if($flag){
echo "Email sent";
}else{
echo "Email sending failed";
}
有关详细信息,请参阅 Coderanks.com 的 article。