我正在开发一个php codeigniter项目,我想从我的localhost发送电子邮件。
以下是我的控制器功能。
$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。当我运行代码时,我的页面会加载错误。
这些是错误:
遇到以下SMTP错误:1923818231无法找到 套接字传输“ssl” - 你忘了启用它吗? 配置PHP?无法发送数据:AUTH LOGIN无法发送AUTH 登录命令。错误:无法发送数据:MAIL FROM:
严重性:警告
消息:date():依赖系统的时区是不安全的 设置。您必需使用date.timezone设置或 date_default_timezone_set()函数。如果您使用其中任何一个 方法,你最有可能仍然得到这个警告 拼写错误的时区标识符。我们为时区选择了“UTC” 现在,请设置date.timezone以选择您的时区。
文件名:libraries / Email.php
行号:705
答案 0 :(得分:1)
使用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帐户中不太受信任的应用设置访问权限
答案 1 :(得分:0)
您可以使用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。