【发布时间】:2015-09-01 05:25:27
【问题描述】:
我正在开发一个 Codeigniter 项目,并从 phpmailer 添加了最新版本,并使用本教程创建了一个 mailer_model
https://phpsblog.wordpress.com/2010/02/14/phpmailer-en-codeigniter/
适应后,我用在另一个模型上,效果很好。
现在我遇到了这个烦人的错误
这是我的自定义库
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class My_PHPMailer {
public function My_PHPMailer()
{
require_once('PHPMailer/PHPMailerAutoload.php');
}
}
?>
这是有问题的模型
<?php
class Operators_model extends CI_Model {
public function send_access($idpatient, $code)
{
$this->load->model('Mailer_model');
#Update code for access on patient Client
$data = array('Code' => $code);
$this->db->where('idpatients', $idpatient);
$this->db->update('patients', $data);
#Find Patient Email
$qry = "SELECT Email FROM patients WHERE idpatients = '$idpatient'";
$result = $this->db->query($qry)->result();
$emailp = $result[0]->Email;
$subject = 'Access to Crossover Laboratory';
$message = '<p>You now can access to our laboratory results with your name and code.</p>
<p>Code: '. $code . ' </p>
<p>' . base_url() . '</p>
';
$body2 = "You now can access to our laboratory results with your name and code.\n
Code: ". $code . " \n
" . base_url() ;
$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>
<p>'.$message.'</p>
</body>
</html>';
$error = $this->Mailer_model->send_mail($subject, $body, $body2, $emailp, "") ;
return $error;
}
}
?>
希望你能帮我解决这个问题
更新
Mailer_Model.php
<?php
class Mailer_model extends CI_Model {
public function send_mail($subject, $body, $body2, $to, $attachment = "")
{
$this->load->library('My_PHPMailer');
$mail = new PHPMailer();
$mail->IsSMTP(); // establecemos que utilizaremos SMTP
$mail->SMTPAuth = true; // habilitamos la autenticación SMTP
$mail->SMTPSecure = "ssl"; // establecemos el prefijo del protocolo seguro de comunicación con el servidor
$mail->Host = "smtp.gmail.com"; // establecemos GMail como nuestro servidor SMTP
$mail->Port = 465; // establecemos el puerto SMTP en el servidor de GMail
$mail->Username = "mygmail@gmail.com"; // la cuenta de correo GMail
$mail->Password = "mypasshere"; // password de la cuenta GMail
$mail->SetFrom('laboratory@test.com', 'Crossover Laboratory'); //Quien envía el correo
$mail->AddReplyTo('laboratory@test.com', 'Crossover Laboratory'); //A quien debe ir dirigida la respuesta
$mail->Subject = $subject; //Asunto del mensaje
$mail->Body = $body;
$mail->AltBody = $body2;
$destino = $to;
$mail->AddAddress($destino);
if ($attachment != "")
{
$mail->AddAttachment($attachment); // añadimos archivos adjuntos si es necesario
}
$message = "Email Sent!";
if(!$mail->Send()) {
$message = "Error : " . $mail->ErrorInfo;
}
}
}
?>
2015 年 1 月 9 日更新
现在我更新了 phpmailer 类,并更改了 PHPMailerAutoload.php 所需的类(见上面更新的代码)
关于这个问题的模型正在运行,但在另一个问题中却没有,并且给了我这个错误
这就是Patients_model中存在问题的函数
public function Send_PDF($filename, $idreport)
{
#Send Email to client with php mailer library
#Check config.php on config folder for credentials
$this->load->library('email');
$this->load->model('Mailer_model');
$details = $this->ReportDetails($idreport);
$patientDetails = $this->PatientDataById($details[0]['idpatients']);
$to = $patientDetails[0]['Email'];
$subject = 'Crossover Report';
$message = 'Attached is the Report Requested';
$body2 = $message;
$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>
<p>'.$message.'</p>
</body>
</html>';
$emailresp = $this->Mailer_model->send_mail($subject, $body, $body2, $to, $filename) ;
return $emailresp;
}
【问题讨论】:
-
查看link
-
@Saty 谢谢,几个小时前已经检查过几次,我正在使用最后一个库,正如我在问题上所说,它适用于模型,而问题之一't
-
你有没有在你的控制器中调用phpmailer库???
-
你需要最新版本。 github.com/PHPMailer/PHPMailer
-
是的,那是因为您没有使用自动加载器。加载 'PHPMailerAutoload.php' 而不是
class.phpmailer.php。将您的代码基于最近的示例也可能会有所帮助。
标签: php codeigniter phpmailer