我的PHPmailer使用此脚本在localhost中工作。
但是,当我将其上传到虚拟主机时,它不起作用。
似乎没有从我的数据库中收到收件人电子邮件。
如果我使用这个:
$ mail-> addAddress('$ email');
这适用于localhostm,但不适用于虚拟主机。它显示错误:没有收件人。
但是,如果我使用这个:
$ mail-> addAddress($ email);
然后显示错误:SMTP connect()失败。
我的代码:
<?php
include 'dbconnect.php';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$npsn = $_POST['npsn'];
$error = false;
// basic npsn validation
if (empty($npsn)) {
echo "<script>alert('Anda belum memasukkan NPSN Sekolah Anda!'); window.location = 'lupapassword.php'</script>";
} else if (!preg_match("/^[0-9]+$/",$npsn)) {
echo "<script>alert('NPSN hanya boleh diisi Angka saja!'); window.location = 'lupapassword.php'</script>";
}else if (strlen($npsn) < 8) {
echo "<script>alert('NPSN harus 8 Angka!'); window.location = 'lupapassword.php'</script>";
}else {
// check npsn exist or not
$query = "SELECT npsn FROM users WHERE npsn='$npsn'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count<1){
echo "<script>alert('Maaf, NPSN Sekolah Anda tidak ditemukan!'); window.location = 'lupapassword.php'</script>";
}else {
// check email exist or not
$query2 = "SELECT email_sekolah FROM users WHERE npsn='$npsn'";
$result2 = mysql_query($query2);
$count2 = mysql_num_rows($result2);
if($count2<1){
echo "<script>alert('Maaf, sebelumnya anda belum mengisi email sekolah pada data sekolah. Password tidak bisa dikirimkan. Silahkan hubungi admin!'); window.location = 'lupapassword.php'</script>";
}
}
// if there's no error, continue to signup
if( !$error ) {
$query = "SELECT * from users WHERE npsn=$npsn";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);
$email = $data['email'];
$password = $data['password'];
// Konfigurasi SMTP
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'mypass';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('[email protected]', 'Irmanto');
$mail->addReplyTo('[email protected]', 'Irmanto');
// Subjek email
$mail->Subject = 'Password Member Area';
// Mengatur format email ke HTML
$mail->isHTML(true);
// Konten/isi email
$mailContent = "<p>my message</p>";
$mail->Body = $mailContent;
// Menambahkan penerima
$mail->addAddress($email);
// Kirim email
if(!$mail->send()){
echo 'Maaf, Password tidak bisa dikirim. </br>Silahkan hubungi admin!';
echo '</br></br>Error: ' . $mail->ErrorInfo;
}else{
echo "<script>alert('Password telah dikirim ke $email!'); window.location = 'index.php'</script>";
}
}
}
最佳答案
这与MySQL或PHPMailer无关。这是一个基本的PHP语法错误。这个:
$mail->addAddress('$email');
由于您是single quoting,因此无法正常工作,因此不会插值var,最终您将添加一个无用或空的地址-这就是为什么您收到一条错误消息,指出没有收件人。只需这样做:
$mail->addAddress($email);
然后,这将导致您出现“ SMTP connect()failed”错误,这是answered on here hundreds of times的错误,并且在the troubleshooting guide中详细记录了诊断问题的技术。您没有提供足够的信息来诊断您的确切问题,因为您尚未阅读文档,但错误提示您指出了如何进行操作。
关于php - PHPmailer无法从MySQL获取电子邮件收件人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46597010/