我将SMTP Mail与phpmailer一起使用,我想更新数据并使用phpmailer发送数据。更新数据成功,但是phpmailer不发送该电子邮件,也不显示任何错误。我该如何解决?
这是我的代码
date_default_timezone_get("Asia/Jakarta");
$getdatasUser = $this->db->query("SELECT IDStatusPendidikan, IDPosisiJabatan FROM mspencaker WHERE IDUser='$iduser'")->result_array();
$ab = $getdatasUser[0]['IDStatusPendidikan'];
$ac = $getdatasUser[0]['IDPosisiJabatan'];
$dateNow = date("Y-m-d");
$lowonganhasil = $this->db->query("SELECT a.NamaLowongan, a.GajiPerbulan, a.IDLowongan, a.Penempatan, a.SyaratKhusus, b.NamaPerusahaan, b.IDPerusahaan, a.TglBerakhir FROM mslowongan as a JOIN msperusahaan as b ON b.IDPerusahaan = a.IDPerusahaan WHERE a.IDStatusPendidikan='$ab' AND a.IDPosisiJabatan='$ac' AND a.TglBerakhir >= '$dateNow' ORDER BY a.TglBerakhir DESC LIMIT 7")->result_array();
// gambar
if (file_exists(BASEPATH .'assets/file/temp/'.$idpencakertemp.'.jpg')){
rename(realpath('assets/file/temp/'.$idpencakertemp.'.jpg'), realpath('assets/file/pencaker').'/'.$getmspencakerdata->IDPencaker.'.jpg');
}
// $this->load->model('EmailModel');
$this->load->library('PHPMailer');
$this->load->library('SMTP');
$email_admin = 'example@gmail.com';
$nama_admin = 'Nickname';
$password_admin = 'mypassword';
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPKeepAlive = true;
$mail->Charset = 'UTF-8';
$mail->IsHTML(true);
// $mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'ssl';
$mail->Username = $email_admin;
$mail->Password = $password_admin;
$mail->Mailer = 'smtp';
$mail->WordWrap = 100;
$mail->setFrom($email_admin);
$mail->FromName = $nama_admin;
$mail->addAddress($getmspencakerdata->Email);
$mail->AddEmbeddedImage('assets/img-acc-pencaker.png', 'acc');
$mail->Subject = 'Akun Verifikasi '.$getmspencakerdata->NamaPencaker;
$mail_data['subject'] = $getmspencakerdata->NamaPencaker;
$mail_data['induk'] = $getmspencakerdata->NomorIndukPencaker;
$mail_data['username'] = $getmsuserdata->Username;
$mail_data['password'] = $getmsuserdata->Password;
$mail_data['lowongan'] = $lowonganhasil;
$message = $this->load->view('email_temp', $mail_data, TRUE);
$mail->Body = $message;
if ($mail->send()) {
$this->session->set_flashdata('notifikasi', '<script>notifikasi("Pencaker Berhasil Ditambahkan", "success", "fa fa-check")</script>');
} else {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
请帮助我修复此代码
答案 0 :(得分:1)
两个错误:
// $mail->SMTPDebug = 2;
如果禁用调试输出,当您看不到调试输出时,也就不足为奇了。取消注释,并将其设置为3
,这是解决连接问题的正确级别。
我怎么知道这是一个连接问题?因为您正在这样做:
$mail->Port = 587;
$mail->SMTPSecure = 'ssl';
此组合无效。请使用端口587和'tls'
,或465和'ssl'
;其他组合将不起作用。
the troubleshooting guide中涵盖了这些内容。由于您使用的是gmail,因此建议您将代码基于the gmail example provided with PHPMailer。