I'm trying to send an email to users with the group level of 4 or 5. I have the group levels distinguished in my 'users' table. I'm trying to get the email and 'firstname' of the users with the 'group' level of 4 or 5. Then to use that email and first name to create variables with it and use it for my email.
Am I structuring my SQL statement incorrectly? I am not getting any errors.
The area where I am struggling with are these lines.
if($sql_email = "SELECT 'email' FROM 'users' WHERE 'group' = 4 AND 5") {
$email = $con->query($sql_email);
} else {
echo "Query not working right";
}
$sql_name = "SELECT 'firstname' FROM 'users' WHERE 'group' = 4 AND 5";
$admin_name = $con->query($sql_name);
More code to show how I am using it
ini_set('display_errors', 1);
error_reporting(E_ALL);
//User Request Email that goes to the commissioner and creator
if(isset($_POST['submit'])) {
$con = mysqli_connect("localhost","root","","db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($sql_email = "SELECT 'email' FROM 'users' WHERE 'group' = 4 AND 5") {
$email = $con->query($sql_email);
} else {
echo "Query not working right";
}
$sql_name = "SELECT 'firstname' FROM 'users' WHERE 'group' = 4 AND 5";
$admin_name = $con->query($sql_name);
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$to = $email;
$subject = 'There is a new user request to join the SFL';
$message = '
<html>
<head>
<title>New SFL User Request</title>
</head>
<body>
<p>Hi '.$admin_name.',</p><br>
<p>Thank you for wanting to join the!</p>
<p>Thank you,</p>
<p>Administration</p>
</body>
</html>
';
$from = "user-requests#example.com";
$Bcc = "user-requests-confirm#example.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}
UPDATE
$sql_email = "SELECT `email` FROM `users` WHERE `group` = 4 OR `group` = 5";
$email = $con->query($sql_email);
if( ! $con->query($sql_email) )
echo "Query not working right";
$sql_name = "SELECT `firstname` FROM `users` WHERE `group` = 4 OR `group` = 5";
$admin_name = $con->query($sql_name);
I get this error..
Catchable fatal error: Object of class mysqli_result could not be converted to string in /home4/db/public_html/example.com/register.php on line 219
Line 219 is this...
<p>Hi '.$admin_name.',</p><br>
After you execute a select query, you need to fetch records. I assume you need to send email to all users found.
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];
$sql_email = "SELECT `email`, `firstname` FROM `users` WHERE `group` IN (4,5)";
$users = $con->query($sql_email);
if (($users) && ($users->num_rows > 0)){// Got any record?
// output data of each row
while($user = $users->fetch_assoc()){
$to = $user['email'];
$subject = 'There is a new user request to join the SFL';
$message = '
<html>
<head>
<title>New SFL User Request</title>
</head>
<body>
<p>Hi '.$user['firstname'].',</p><br>
<p>Thank you for wanting to join the!</p>
<p>Thank you,</p>
<p>Administration</p>
</body>
</html>
';
$from = "user-requests#example.com";
$Bcc = "user-requests-confirm#example.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Bcc: '.$Bcc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}
}
else{
echo "No User Found!";
}
You can learn more from here: Select Data With MySQLi
SELECT anything FROM `users` WHERE `group` = 4 OR `group` = 5
Besides,
if($sql_email = "SELECT 'email' FROM 'users' WHERE 'group' = 4 AND 5")
is useless as the result will always be true.
You can say
if( ! $con->query($sql_email) )
echo "Query not working right";
Related
I'm making this code that (1) moves records from one table to another (in the same database), (2) sends the contents of table 1 to a pre-determined e-mail, and (3) delete the contents of table 1 - simulating an "add to cart" feature.
My problem is that the code below will only be successful in sending the e-mail if $headers is not sent on mail(). However, I need to send the table contents as HTML or at least allow for for the different records. If by any chance the e-mail isn't sent, then the delete part of the code isn't executed either. what am I doing wrong?
Thanks in advance!
Modified code (that works if I DON'T send $headers)
<?php
include '../config/database.php';
date_default_timezone_set('CET');
$date = date('Y-m-d H:i:s');
$query = "INSERT INTO claims_archive (t20pctID, total_amount, user_id, sent) SELECT t20pctID, total_amount, user_id, #date FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query);
if ($stmt->execute()) {
$query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, c.total_amount FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%sony%' ORDER BY t.main_artist";
$stmt=$con->prepare($query);
$stmt->execute();
$to = "testmail2#gmail.com";
$subject = "Test";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: Test <testmail1#mail.com>" . "\r\n";
$body = "Sent on: ". $date . "-\r\n";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$body .= "Track title: ".$row ["track_title"]. "-";
}
$success = mail($headers, $to, $subject, $body);
if ($success) {
$query_delete = "DELETE FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query_delete);
$stmt->execute();
header('Location: cart.php?action=sent');
} else {
header('Location: cart.php?action=sent_failed');
}
} else {
header('Location: cart.php?action=sent_failed');
}
include 'layout_foot.php';
?>
Original code
<?php
include '../config/database.php';
date_default_timezone_set('CET');
$date = date('Y-m-d H:i:s');
$query = "INSERT INTO claims_archive (t20pctID, total_amount, user_id, sent) SELECT t20pctID, total_amount, user_id, #date FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query);
if ($stmt->execute()) {
//header('Location: cart.php?action=sent'); //please disregard this line as I forgot to remove it when I wrote this post.
$query = "SELECT t.t20pctID, t.main_artist, t.track_title, t.original_album, c.total_amount FROM cart c LEFT JOIN tblclaims t ON t.t20pctID = c.t20pctID WHERE t.t20pctID LIKE '%sony%' ORDER BY t.main_artist";
$stmt=$con->prepare($query);
$stmt->execute();
$to = "testmail2#gmail.com";
$subject = "Test";
$headers = "Test <testmail1#mail.com>". "\r\n". "MIME-Version: 1.0" ."\r\n". "Content-type: text/html; charset=iso-8859-1" ."\r\n";
$body = "Sent on: ". $date . "-";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$body .= "Track title: ".$row ["track_title"]. "-";
}
$success = mail($to, $subject, $body);
if ($success) {
header('Location: cart.php?action=sent');
} else {
header('Location: cart.php?action=sent_failed');
}
$query_delete = "DELETE FROM cart WHERE t20pctID LIKE '%sony%'";
$stmt = $con->prepare($query_delete);
$stmt->execute();
} else {
header('Location: cart.php?action=sent_failed');
}
include 'layout_foot.php';
?>
Your delete code will not be executed in any case since your are redirecting before this code run.
Remove header('Location: cart.php?action=sent'); written just
below to if ($stmt->execute()) {.
Place your delete code in if($success) condition before header.
Update $headers variable with below code and include it into your mail function:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Test <testmail1#mail.com>' . "\r\n";
This
$success = mail($headers, $to, $subject, $body);
has not the right parameter order ...
See http://php.net/manual/de/function.mail.php The correct signature is this:
bool mail ( string $to , string $subject , string $message
[, string $additional_headers [, string $additional_parameters ]] )
so
$success = mail($to, $subject, $body, $headers);
i design an application and build with phonegap all is working but when a value in my mysql database that is attached the app is updated i want an email to be sent automatically to the user. When the counter completes counting the value of "status" in runnindAds table is updated and i want to an email to be sent each time that value is updated, below is the script i wrote for the email to be sent but i don't know how to automate it, please help with what i can should or i can
<?
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";
$id=$_GET["rid"];
$status = 3;
mysql_query("update runningAds set status = 3 where id = '$id'");
// get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];
sendConfirmationEmail($username, $stopTime);
header("location: ../view_running_ads.php");
function sendConfirmationEmail($username, $stopTime)
{
$result2 = mysql_query("select * from dapUsers where userName='$username'");
$row = mysql_fetch_array($result2);
$to = $row['email'];
//$from = "admin#addirectng.com";
$subject = 'Advert Runtime Alert';
$message = "Dear $username,<br \>
Your display picture advert runtime has ended at $stopTime. <br \>
Advert Direct Team";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <admin#addirectng.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
?>
Try this:
<?php
include('../db_connect.php');
require_once "PHPMailer-master/class.phpmailer.php";
$id=$_GET["rid"];
$status = 3;
// get username
$qaz = mysql_query("select * from runningAds where id = $id") or die (mysql_error());
$wsx = mysql_fetch_array($qaz);
$username = $wsx["users"];
$stopTime = $wsx['stopTime'];
if (mysql_query("update runningAds set status = 3 where id = '$id'"))
{
sendConfirmationEmail($username, $stopTime);
}
header("location: ../view_running_ads.php");
function sendConfirmationEmail($username, $stopTime)
{
$result2 = mysql_query("select * from dapUsers where userName='$username'");
$row = mysql_fetch_array($result2);
$to = $row['email'];
//$from = "admin#addirectng.com";
$subject = 'Advert Runtime Alert';
$message = "Dear $username,<br \>
Your display picture advert runtime has ended at $stopTime. <br \>
Advert Direct Team";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <admin#addirectng.com>' . "\r\n";
mail($to,$subject,$message,$headers);
}
?>
Tell me if it works or not.
I am working on a membership signup/join form. The form data is submitted to another page called join.php on pressing submit button I am getting Internal Server Error message. Can anybody help me find the reason/mistake in my coding? Though the data is successfully entered into database.
<?php
// file name : join.php
$con = mysqli_connect("$DBHOST", "$DBUSER", "$DBPASS","$DBNAME");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$ip = $_SERVER['REMOTE_ADDR'];
$name = mysqli_real_escape_string($con,$name);
$email = mysqli_real_escape_string($con,$email);
$phone = mysqli_real_escape_string($con,$phone);
$city = mysqli_real_escape_string($con,$city);
$state = mysqli_real_escape_string($con,$state);
$check = "SELECT COUNT(*) FROM `members` WHERE phone=".$phone."
OR email=".$email." Limit 1";
if (mysqli_query($con,$check)>=1){
echo ("The phone number <strong>".$phone."</strong> or email <strong>
".$email." </strong> address is already registered with us.");
}else{
$query = mysqli_query($con,"INSERT INTO `members`
(`name`,`email`, `phone`, `city`, `state`,`ip`, `regdate`)
VALUES('".$name."','".$email."','".$phone."','".$city."',
'".$state."','".$ip."', NOW('') )")
or die("MYSQL ERROR :".mysqli_error($con));
/* PREPARE MESSAGE FOR EMAIL TO NEW MEMBER */
header("Refresh=07;URL=./index.php");
$headers4 = "<join#mydomain.com>";
$headers = "Reply-to: $headers4\n";
$headers .= "From: $headers4\n";
$headers .= "Errors-to: $headers4\n";
$headers .= "Content-Type: text/html; charset=utf-8\n";
$message = "<br>Dear ".$name." <br><br>";
$message .= "Thanks for joining.<br> Your details are";
$message .= "<br>Name - ".$name." <br>Mobile No. - ".$phone."<br>";
$message .= "Email - ".$email."<br>City, State - ".$city.",".$state."<br>";
$message .= "<br>Regards,<br>Name";
mail("".$email."", "Thanks for Joining", "".$message."", "".$headers."");
echo "<p>Congratulations!<br>IP-".$ip."<br>Your data has been added
into our membership database.<br><strong>Thank you for joining.</strong>";
}
mysqli_close($con);
?>
So many mistakes in it.. Improve your code style to give its quality a boost.
Start by fixing the quotes, very missleading:
mail("".$email."", "Thanks for Joining", "".$message."", "".$headers."");
Should be
mail($email, 'Thanks for Joining', $message, $headers);
$check = "SELECT COUNT(*) FROM `members` WHERE phone=".$phone."
OR email=".$email." Limit 1";
Has missing quotes too, I don't thinkg email and phone are numbers.
$check = "SELECT COUNT(*) FROM `members` WHERE phone='".$phone."'
OR email='".$email."' Limit 1";
There is no header called Refresh, this is kinda Javascript style, but you need HTTP:
header("Refresh=07;URL=./index.php");
Fixed:
header("Location: index.php");
Finally enable error reporting to see what's really wrong.
I was trying to find out the error but I coudnt. I use codeigniter.
Here is the code
function send_emailalertstoAdmin($emailmessage,$ad_title)
{
$this->load->model('user_model');
$query_result = $this->user_model->load_administrator_email();
//$this->load->database();
//$query = $this->db->query('SELECT users.email FROM users WHERE users.id =1');
foreach ($query_result as $row)
{
$email= $row->email;
$successmasg = '';
$from = "info#xxxt.com";
$subject = "xxx ". $ad_title . " is Stolen";
$message = $emailmessage;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: xxxxx.com <info#xxxxt.com>' . "\r\n";
$successmasg = mail($email, $subject, $message, $headers);
}
}
I got the following error.
Fatal error: Call to undefined method User_model::load_administrator_email()
Here is the load_administrator_email() function
function load_administrator_email()
{
$sql = "SELECT users.email FROM users WHERE users.id =1";
$queryData = $this-> db->query($sql);
return $queryData -> result();
}
I was trying to find the error but I coudnt.
what I tried,
1. sql query is correct and it gives correct result.
2. when I hardcode email and comment calling load_administrator_email() in first function ,email goes correctly.
please help me to correct this.
thx in advance
Load your model like this..
$this->load->model('user_model', '', TRUE);
Or else load it in the constructor
I am using a custom form, with a Joomla 1.6 Platform. The User email goes through fine, but the email notifing us of the sign up does not come through. Is it possible to send 2 emails back to back like this or is there an error in my code?
///----------------User Email After Registered----------------------------///
// Send notification email //
$to = $email;
$subject = "$hs2 Alumni Football Team";
$message = "
<html>
<head></head>
<body>
<h1>Alumni Football USA</h1>
<p> Hi $firstname,</p>
<p>You are now able to sign up for $hs2 games! Login to Reserve your spot!</p>
<p>Username: $username</p>
<p>Password: $showpass</p>
<p>$hs2 Alumni Football Team Page---$remail2 </p>
<p>This is an automatic Email, please do not respond/reply</p>
</body>
</html>
";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: Alumni Football USA<NOREPLY#alumnifootballusa.com>' . "\r\n";
// Send notification //
mail($to,$subject, $message, $headers);
///---------------Email site owner-----------------------------------------///
$phone1 = $cell;
$cell1 = formatPhone1($phone1);
$phone2 = $home;
$home1 = formatPhone2($phone2);
$query = "SELECT t_name FROM " . $table_prefix . "bl_teams WHERE id = ".$team_id;
$db->setQuery($query);
$hs2 = $db->loadResult();
$query = "SELECT ((date_format(now(),'%Y') - date_format(birthday,'%Y')) - (date_format(now(),'00-%m-%d') < date_format(birthday,'00-%m-%d')))
FROM " . $table_prefix . "users WHERE'".$email."' = username";
$db->setQuery($query);
$age45 = $db->loadResult();
$query = "SELECT t_city FROM " . $table_prefix . "bl_teams WHERE ".$team_id."= id ";
$db->setQuery($query);
$city5 = $db->loadResult();
$query = "SELECT s.s_descr FROM " . $table_prefix . "bl_seasons as s WHERE ".$s_id."= s.s_id ";
$db->setQuery($query);
$state = $db->loadResult();
$query = "SELECT u.email FROM " . $table_prefix . "users as u, " . $table_prefix . "bl_teamcord as tc WHERE tc.s_id = ".$s_id." AND FIND_IN_SET('$team_id',tc.teams) AND tc.u_id = u.id AND
tc.reg_emails = 1";
$db->setQuery($query);
$remail1 = $db->loadResultArray();
if ($remail1){
$remail = implode(",",$remail1);
}else{
$remail = "";}
// Send notification email //
$to = $remail;
$subject = "***Reserved***$hs2,$state --$firstname $lastname";
$message = "
<html>
<head><title>Alumni Football USA</title></head>
<body>
<h1>$firstname $lastname</h1>
</body>
</html>
";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: Alumni Football USA <webmaster#alumnifootballusa.com>' . "\r\n";
// Send notification //
mail($to,$subject, $message, $headers);
///--------------------------------------------------------------///
The problem might be you $to variable might not have email value:
if ($remail1){
$remail = implode(",",$remail1);
}
else{
$remail = ""; //you might have this
}
$to = $remail; //so if $remail is blank so will be the $to as well
So ensure your $to variable holds email,
if(!empty($to)) {
//send email
mail($to,$subject, $message, $headers);
}