Forum Moderators: coopster

Message Too Old, No Replies

phpmailer script Not loading variables from MYSQL database

phpmailer script Not loading variables from MYSQL database

         

swelch15

11:54 pm on Oct 16, 2018 (gmt 0)

5+ Year Member



For some reason the variable for Email and Username are not being loaded from the database. Everything else seems to be working. The select is correct as it works on it's own and returns what it is supposed to. The script returns 2 results like it is supposed to since there are 2 records that match. In fact I get the error message twice which is:
Message could not be sent.Mailer Error: You must provide at least one recipient email address.Message could not be sent.Mailer Error: You must provide at least one recipient email address.


<?php
require'../../../connect.php';
require 'PHPMailerAutoload.php';


$stmt = $dbc->prepare("SELECT Email, UserName FROM Members WHERE Type LIKE 'Client'");
$stmt->execute();

while($row = $stmt->fetch()) {
$Email = $row['Email'];
$UserName = $row['UserName'];

sendemail($Email, $UserName);
}

function sendemail($Email, $UserName){

$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'exampleATgmail.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

$mail->setFrom('exampleATgmail.comm', 'The Services App');
$mail->addAddress($Email); // Name is optional
$mail->addReplyTo('exampleATgmail.com', 'The Services App');


// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body = 'Hello ".$UserName."<br> This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
$mail->SMTPDebug = 2;
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent to ".$UserName."<br>';
}
}
?>




[edited by: not2easy at 2:31 am (utc) on Oct 17, 2018]
[edit reason] changed email to "exampleATgmail.com" [/edit]

not2easy

2:34 am on Oct 17, 2018 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Just a note - I have edited out an actual email address from your code, there are still email harvester bots out there, and no one wants that. Anywhere in the shared code above where you see "exampleATgmail.com", it represents a gmail address.

swelch15

3:46 am on Oct 17, 2018 (gmt 0)

5+ Year Member



Thanks I appreciate that. I ended up solving the problem luckily.

justpassing

7:58 am on Oct 17, 2018 (gmt 0)

5+ Year Member Top Contributors Of The Month



May be your database was not populated with the email and username?

I ended up solving the problem luckily.

May be you can share the solution, this can help someone else.

ps: if you deal with EU users, do not forget to be GDPR compliant (since you collect email addresses, at least)

swelch15

1:38 pm on Oct 17, 2018 (gmt 0)

5+ Year Member



Sure here is the code that I ended up using to fix the problem


<?php
require'../../../connect.php';
require 'PHPMailerAutoload.php';


$result = mysqli_query($dbc, "SELECT * FROM Members WHERE Type LIKE 'Client'");

while ($Members = $result->fetch_assoc()) {
$UserName = $Members['UserName'];
$mail = new PHPMailer;

//$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'NotARealEmail@gmail.com'; // SMTP username
$mail->Password = 'NotARealPassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

$mail->setFrom('NotARealEmail@gmail.com', 'The Services App');
$mail->addAddress($Members['Email'], 'Name of Person'); // Add a recipient
// $mail->addAddress('exampleEmail'); // Name is optional
$mail->addReplyTo('NotARealEmail@gmail.com', 'The Services App');


// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = 'One of your notifications has marked themselves as available';
$mail->Body = 'Hello, <br> one of your notifications is available.';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
$mail->SMTPDebug = 2;
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
?>