How to retrieve more than one data in email? - php

I have a table named 'laptop' and a column inside the table named 'Lap_War_Expiry'. I need to send an email to user when the warranty of the laptop is going to expire soon. For your information, there is more than one warranty that will expired in a day. But the code below is only send the same data from table 'laptop'. Why is that happened and what I have to add in the code so that the email send will retrieve two or more data from database ?
This is my coding for sending the email :
<?php
require 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; // "ssl://smtp.gmail.com" didn't worked
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
// ======== get database data ==================
$link = mysql_connect("localhost","root","");
$database="master_inventory";
mysql_select_db ($database,$link) OR die ("Could not open $database" );
$query = 'SELECT Lap_PC_Name, Lap_War_Expiry FROM laptop'; //xyz is id of desired user
name.
$result1 = mysql_query($query);
while($row = mysql_fetch_array($result1)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
}
$mail->Username = "email#gmail.com";
$mail->Password = "somepassword";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true;
$mail->From = "email#gmail.com";
$mail->FromName = "AMS";
$mail->addAddress("emailaddress","AMS");
$mail->Subject = "Notification on warranty expiry";
$mail->Body = "Dear Madam,<br/><br />The licensed for the following PC will expired
in less than one month.<br /><br /> PC Name : ".$Lap_PC_Name. "<br />Date of expired :"
.$Lap_War_Expiry;
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
?>

You need to move everything from below your while loop into it.
EDIT
Change
while($row = mysql_fetch_array($result1)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
}
to
while($row = mysql_fetch_array($result1)) {
$Lap_PC_Name = $row['Lap_PC_Name'];
$Lap_War_Expiry = $row['Lap_War_Expiry'];
$mail->Username = "email#gmail.com";
$mail->Password = "somepassword";
$mail->IsHTML(true); // if you are going to send HTML formatted emails
$mail->SingleTo = true;
$mail->From = "email#gmail.com";
$mail->FromName = "AMS";
$mail->addAddress("emailaddress","AMS");
$mail->Subject = "Notification on warranty expiry";
$mail->Body = "Dear Madam,<br/><br />The licensed for the following PC will expired
in less than one month.<br /><br /> PC Name : ".$Lap_PC_Name. "<br />Date of expired :"
.$Lap_War_Expiry;
if(!$mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";
}

Related

PHP SMTP Mail script not working

I'm coding a php smtp mail script to send a mail to my users when they lose their password. However it's not working.
The processor always returns:
Message could not be sent...
I've tried it with Gmail smtp and I'm getting the same results.
Can you help? There are two files, the form, and the processor.
Below are the codes:
form:
<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'>
<link rel = 'stylesheet' href = 'style.css' type = 'text/css'>
<title>Reset Password</title>
</head>
<?php
$db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
$sql = $db->query('select id from posts order by id desc');
$row = $sql->fetch(PDO::FETCH_ASSOC);
echo "<a href = 'browse.php?page=".$row['id']."'>Browse</a> <a href = 'index.php'>Home</a>";
echo "<hr>";
echo '<form name = "forgot-pass" action = "reset-pass-notif.php" method = "post">
<label><em>Input your registered email address</em></label><br>
<input type = "text" name = "email"><br>
<em>Enter Image Text:</em><img src="captcha.php" /><br />
<input name="captcha" type="text" /><br>
<input type = "submit" name = "submit" value = "Send Reset-Password Link">
</form>
<hr>
Browse Home';
?>
</html>
processor:
<html>
<head>
<meta name = 'viewport' content = 'width = device-width, initial-scale = 1.0, maximum-scale = 4.0, user-scalable = yes'>
<link rel = 'stylesheet' href = 'style.css' type = 'text/css'>
<title>Reset Password Notification</title>
</head>
<?php
error_reporting(E_ALL);
session_start();
$db = new PDO('mysql:host=host;dbname=db;charset=utf8', 'user', 'pass');
$sql0 = $db->query('select id from posts order by id desc limit 1');
$row0 = $sql0->fetch(PDO::FETCH_ASSOC);
echo 'Browse Home';
echo '<hr>';
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$sql = $db->prepare('select * from users where email = :email');
$sql->bindParam(':email', $email);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$row = $sql->fetch();
$username = $row['username'];
$user_check = $row['user_hash'];
$subject = 'Reset your Password';
$link = 'http://bquotes.xyz/reset-pass-land.php?username='.$row['username'].'&user_check='.$row['user_hash'].'';
$message = 'Reset your password here: '.$link.' If you cannot click on the link, copy it and paste in your browser address bar';
$sql = $db->prepare('select email from users where email = :email');
$sql->bindParam(':email', $email);
$sql->execute();
$sql->setFetchMode(PDO::FETCH_ASSOC);
$row = $sql->fetch();
//require_once ('class.phpmailer.php');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.domain.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#domain.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('user#domain.com', 'BQuotes Webmaster');
//$mail->addAddress($email); // Add a recipient
/*$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$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 = $subject;
$mail->Body = $message;
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
/*if(!$mail->send()) {
//echo '<em>Message could not be sent. Mailer Error:</em> ', "<em>", $mail->ErrorInfo, "</em>";
echo '<span class = "red"><em>Message could not be sent. You have one or more invalid fields.</em></span>';
} else {
echo '<span class = "green"><em>Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.</em></span>';
}*/
if (($email)&&($username)&&!empty($_POST['captcha'])&&($_POST['captcha']==$_SESSION['code'])&&($mail->send())){
echo '<span class = "green"><em>Message has been sent. Please check your Spam folders if not in Inbox by 10 minutes.</em></span>';
}
else {echo '<span class = "red"><em>Message could not be sent. You have one or more invalid fields.</em></span>';
}
echo "<hr>";
echo 'Browse Home';
?>
</html>
Thanks!
The correct setting for PHPMailer:
<?php
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Username = "user#domain.com";
$mail->Password = "xxxx";
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.domain.com";
$mail->Port = "465";
$mail->setFrom('from#domain.com', 'John Doe');
$mail->addReplyTo('from#domain.com', 'John Doe');
$mail->AddAddress("mailto#otherdomain.com", "Jane Smith");
$mail->CharSet = 'UTF-8';
$mail->IsHTML(true);
$mail->Subject = 'Bla bla or from DB';
$mail->Body = 'Here come the content...';
if (!$mail->send()) {
echo 'Error: ' . $mail->ErrorInfo;
exit;
}
?>
Please use this code .make sure you have given permission in Mail account from you want to send mail.
My Account->Sign-in & security->Allow less secure apps: OFF
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = false;
$mail->isSMTP();
$mail->Host = 'smtp.live.com';
$mail->SMTPAuth = true;
$mail->Username = 'mail#gmail.com';
$mail->Password = 'Password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom($details['from'], $details['from']);
if(is_array($details['to'])){
foreach ($details['to'] as $key => $value) {
$mail->addAddress($value['email'], $value['name']);
}
}else{
$mail->addAddress($details['to'], isset($details['name'])?:$details['to']);
}
$mail->isHTML(true);
$mail->Subject =$details['subject'];
$mail->Body =$details['body'];
$mail->send();
Are you running this on localhost? Try Putting off your antivirus for some time. Antivirus tends to block email sending from localhost at times

how do i send a user message to their mail from phpmailer?

Hi guys, please I'm having problem with my code. I want to accept user
email and verify it from my database then send the user an email using
PHPMailer but it's giving me error, I guess I did some bunch of error
coding here. please how do I go about it? this question might be silly
but I really need assistance from anyone, please. Thanks.
here is what I've tried out.
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$con = mysqli_connect("localhost","root","password007","voting");
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$email = mysqli_real_escape_string($con,$_POST['email']);
$sql = "SELECT VotersID,Email,First_name FROM voters WHERE Email = '$email' ";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
$votersID = $row['VotersID'];
$first = $row['First_name'];
if($count == 1) {
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '******#gmail.com';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->From = '*****#gmail.com';
$mail->FromName = 'West End University College';
$mail->addAddress($email, $first);
$mail->addReplyTo('*****#gmail.com', 'West End University College');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'code for login Confirmation';
$mail->Body = 'Hi, your code is:';
$mail->addAttachment($votersID);
}
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
else{
echo "Message sent successfully";
}
}
?>
Errors :
Notice: Undefined variable: mail in
C:\xampp\htdocs\voters\accConfirm.php on line 45 Fatal error: Call to
a member function send() on null in
C:\xampp\htdocs\voters\accConfirm.php on line 45

PHP SMTP Custom contact form

I have this form, that send fine locally, but when i upload it to hostgator i get the following error message
Mailer Error: Could not instantiate mail function.
my php code is
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>
I have spoken to my host and they said its not within there support to deal with these things, but said my port and host are correct.
So now I'm rather confused. is there anything obvious I'm missing?
Just to let you know if anyone finds this, my problem was i was missing $mail->IsSMTP(); from my config.
the SMTP config section should be as follows
<?php
if(isset($_POST['submit'])){
require 'PHPMailer/PHPMailerAutoload.php';
// Send mail
$mail = new PHPMailer();
// Data received from POST request
$name = stripcslashes($_POST['tbName']);
$emailAddr = stripcslashes($_POST['tbEmail']);
$company = stripcslashes($_POST['tbCompany']);
$comment = stripcslashes($_POST['taMessage']);
$subject = stripcslashes($_POST['tbSubject']);
// SMTP Configuration
$mail->SMTPAuth = true;
$mail->IsSMTP();
$mail->Host = "gator3209.hostgator.com"; // SMTP server
$mail->Username = "****#*****.com";
$mail->Password = "***********";
$mail->SMTPSecure = 'tls';
$mail->Port = 25;
$mail->AddAddress('****#*****.com');
$mail->From = "****#*****.com";
$mail->FromName = "Website Contact Form - " . $name;
$mail->Subject = $subject;
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML("Name:" . $name . "<br /><br />Email:" . $emailAddr. "<br /><br />Company:" . $company. "<br /><br />Subject:" . $subject. "<br /><br />" . $comment);
$message = NULL;
if(!$mail->Send()) {
$message = "Mailer Error: " . $mail->ErrorInfo;
} else {
$message = "Message sent!";
}
}
?>

PHPMailer sending multiple emails to every receipents in database

I am using this code to send email to everyone in the database in a loop. Problem occurred when I find out that user 1 in database also getting all emails sent to other users in the database. User 2 gets all email sent to other users after him and so on. I want to send a single email to everyone customized with their name and emails. Did check and applied couple of solutions from this forum but they are not working.
#SEND EMAILS
require '../includes/PHPMailer/PHPMailerAutoload.php';
require '../includes/PHPMailer/config.php';
if(isset($_POST['submit'])){
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = $smtp_server_w;
$mail->Username = $username_w;
$mail->Password = $password_w;
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->setFrom("test#test.com", "Test Name");
$sql_mail = "SELECT * FROM users";
$run_mail = mysqli_query($conn, $sql_mail);
while ($row_mail = mysqli_fetch_assoc($run_mail)) {
$name = $row_mail["user_name"];
$email = $row_mail["user_email"];
$mail->ClearAddresses();
$mail->addAddress('test#test.com', 'Test Name');
$mail->addBCC($email, $name);
$mail->isHTML(true); // Set email format to HTML
$bodyContent = "<p>Multiple Messages</p>";
$mail->Subject = $row_mail['user_name'].", New job is available " ;
$mail->Body = $bodyContent;
$mail->AltBody = $bodyContent;
if(!$mail->send()) {
echo "Message could not be sent.";
$error = '<div class="alert alert-danger"><strong>Mailer Error: '. $mail->ErrorInfo.'</strong></div>';
} else {
$error = '<div class="alert alert-success"><strong>Message has been sent</strong></div>';
}
}
}

SMTP : Could not authenticate [duplicate]

This question already has answers here:
"SMTP Error: Could not authenticate" in PHPMailer
(21 answers)
Closed 6 years ago.
<?php
include_once('include/connection.php');
if(isset($_POST['submit']))
{
$name=mysql_real_escape_string($_POST['name']);
$contact=mysql_real_escape_string($_POST['contact']);
$email=mysql_real_escape_string($_POST['email']);
$comments=mysql_real_escape_string($_POST['comments']);
/*
echo var_dump($name);
echo var_dump($contact);
echo var_dump($email);
echo var_dump($comments);
*/
$fdate=date("d/m/Y");
$date = date("m/d/Y h:i:s a");
//echo date("m/d/Y h:i:s a", time());
$query = "INSERT INTO `form_entry`(`name`, `contact`, `email`, `comments`)
VALUES ('$name', '$contact', '$email', '$comments')";
//echo var_dump($query);
mysql_query($query) or die ('Error updating database: '.mysql_error());
//SEND CONFIRMATION EMAIL
require_once('mailer/class.phpmailer.php');
$message= "
<br /><br />
-------------------------------------------------------------------------------------------------
-- Name : $name <br>
-- Contact : $contact <br>
-- Email ID : $email <br>
-- Comments : $comments <br>
------------------------------------------------------------------------ -------------------------
";
$subject = "Enquiry from $name";
$mail = new PHPMailer();
$mail->IsSMTP();
//$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username="xyz#gmail.com";
$mail->Password="abc123##!";
$mail->SetFrom('xyz#gmail.com','xyz ');
$mail->FromName="xyz";
$mail->AddAddress($email);
$mail->AddReplyTo("xyz#gmail.com","xyz ");
$mail->Subject = $subject;
$mail->Body = "Hello Sir/Madam,";
$mail->IsHTML($message);
$mail->Send();
echo "<script>alert('Data Saved Successfully');</script>";
mysql_close($connection);
}
?>
Output : " SMTP Error : Could not authenticate "
It's gmail authentication I'm trying.
ID & Password used to verify are surely perfect.
Checked the SMTP server settings from Google, Looks same.
I checked with other posts but didn't solve my problem.
Pls help for the same! Thanks in advance!
you can try the below code
$to = to#gmail.com;
$from = 'info#test.com';
$name = 'test.com';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'bh-24.webhostbox.net';
$mail->Port = 465;
$mail->Username = 'info#test.com';
$mail->Password = '1234#';
$mail->IsHTML(true);
$mail->From="info#test.com";
$mail->FromName="test.com";
$mail->Sender=$from; // indicates ReturnPath header
$mail->AddReplyTo($from, $FromName); // indicates ReplyTo headers
$mail->Subject = $subject;
$msg = "this is a test message";
$mail->Body = $msg;
$mail->AddAddress($to);
$mail->Send();

Categories