简体   繁体   中英

PHPmailer doesn't work taking email recipient from MySQL

my PHPmailer work in localhost with this script. But, it doesn't work when I upload it to my web hosting. It seems doesn't get recipient email from my database.

if I use this : $mail->addAddress('$email');

this works in localhostm but not in web hosting. it shows error : no recipient.

But, if I use this : $mail->addAddress($email);

then it shows error : SMTP connect() failed.

my code:

    <?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 = 'myemail@gmail.com';
            $mail->Password = 'mypass';
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;
            $mail->setFrom('myemail@gmail.com', 'Irmanto');
            $mail->addReplyTo('myemail@gmail.com', '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>";  
}
}
}

This is nothing to do with MySQL, nor PHPMailer. It's a basic PHP syntax error. This:

$mail->addAddress('$email');

Won't work because you're single quoting , so the var won't be interpolated, and you'll end up adding a useless or empty address - which is why you get an error saying no recipient. Just do this instead:

$mail->addAddress($email);

This then leads you to the "SMTP connect() failed" error, which is something that has been answered on here hundreds of times , and techniques to diagnose the problem are thoroughly documented in the troubleshooting guide . You've not provided enough information to diagnose your exact problem, because you've not read the docs the error points you to that show you how to do so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM