I want to send an email to someone that clicks the link having the information on them from my database. So they put in their username , password, and email and get the 'Item' and 'Aisle' sent to them. The problem is they can have multiple items under their username. So I need to echo all the information in one email. But its not possible to echo information in an email. Currently it sends an email for each item and aisle information found so it can send 2+ emails of information. Any help would be loved. Thanks!
$Username = mysql_real_escape_string($_POST['Username']);
$Password = mysql_real_escape_string($_POST['Password']);
$Loc = mysql_real_escape_string($_POST['Loc']);
$To = mysql_real_escape_string($_POST['To']);
$Subject = "List";
$query = mysql_query("select * from Members where Username = '$Username' and Password = '$Password'");
while ($row = mysql_fetch_array($query)) {
$headers = 'From: email#email.com';
$Items = $row['Items'];
$Loc = $row['Loc'];
$msg= "Item: $Items
Aisle: $Loc\n";
mail($To, $Subject, $msg, 'From:' . $header);
echo 'Email sent to: ' . $To. '<br>';
Changing the 2nd part of your codes as below:
$query = mysql_query("select * from Members where Username = '$Username' and Password = '$Password'");
$items = '';
$headers = 'From: email#email.com';
while ($row = mysql_fetch_array($query)) {
$items .= $row['Items'] . PHP_EOL;
$loc = $row['Loc']; // what is Loc ?
}
$msg= "Item: $items
Aisle: $loc\n";
mail($To, $Subject, $msg, 'From:' . $header);
echo 'Email sent to: ' . $To. '<br>';
However, your table structure is weird. You should put your items in a separate table with your member ID as foreign key.
Try placing the message in a separate file and include it in the message.
like this.
$message = include('email_massage.php');
Related
I have the below code. I usually send an email to one recipient. My question is how can I modify for it to send to multiple recipients that are in my database.
Suppose I have a table called "tblemails" and a column called "email".
Note:
I also dont want to show all address to each member of my list (something like using Bcc).
Please Help?
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "sender#gmail.com";
$to = "receiver#gmail.com";
$subject = "Online order";
$message = "You just ordered";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
?>
I am sure you can fetch the email data from the database,
You can update header string as
$headers .= 'BCC: '. implode(",", $emailData) . "\r\n";
$emailData - Should be 1-D array which should contains all email ids. ref
you can use while loop
$a = mysqli_query($con,"SELECT email FROM table");
while($row = mysqli_fetch_array($a)){$email = $row['email'];mail($email,$subject,$msg,$header);}}
I hope you know how to fetch the email ids from the database.
$link = mysqli_connect("localhost", "root", "password", "DBName");
$sql = "SELECT * FROM tableA";
if ($res = mysqli_query($link, $sql)) {
if (mysqli_num_rows($res) > 0) {
while ($row = mysqli_fetch_array($res)) {
$recipients[] = $row['email'];
}
}
}
$to = 'some#email.com';
$bcc = implode(',', $recipients);
$headers .= 'From: noreply#company.com\r\n'; //here you can set sent from
$headers .= 'BCC: '.$bcc. '\r\n';
mail($to,$subject,$message, $headers);
i am working on script to send a mail to a list of emails from my table using php
presently i run a query on the table and echo all the emails meeting such requirements from the sql query
sql="SELECT * FROM people WHERE status in('member','client')";
$result = mysql_query($sql)or die("Cannot query orders_products data" . mysql_error());
while($row=mysql_fetch_array($result)) {
$list = array();
$list[] = $row['Email'];
echo implode(",",$list);
$email = 'xxxxxxxxxxx';
$emailto = implode( "," ,$list );
$subject = "xxxxxxxxxxxxxxxx";
$headers = "From: $email";
$body .= ++$i.") ".$row['title'] ."\n\n";
}
$body .= "Regards\n\n";
$body .= "xxxxxxxxxxxxxxxxxx\n\n";
$send = mail($emailto, $subject, $body, $headers);
when i try passing these emails to a mail function snippet the mail sending fails.
Please what could i be doin wrong, i need help
Update :
$list[] = $row['Email'];
echo implode(",",$list);
all the emails are displayed
But without commas.
$emailto = implode( "," ,$list );
i use the same method of imploding the array of data gotten from
$list[] = $row['Email'];
By fail i just mean i was hoping since the emails got echoed using the implode it would all successfully pass the emails to $emailto and then send a mail to each of them.
So, what I think you're trying to do is to get the email address from your table where the user is either a member or a client. Then, you have a from address, subject, and a body. In addition, it looks like maybe you're trying to add to the body some unique information. Finally, I think you want to send an individual email to each user, separately.
You could modify your code to be like this:
<?php
$sql="SELECT * FROM people WHERE status in('member','client')";
$result = mysql_query($sql) or die("Cannot query orders_products data" . mysql_error());
$emailContent = 'xxxxxxxxxxx';
$emailSubject = 'xxxxxxxxxxx';
$body = 'xxxxxxxxxxxxxxx';
$headers = "From: from#email.com";
while ($row = mysql_fetch_array($result)) {
// optionally modify body here...?
$emailBody = $body .= $row['title']; // or whatever you're trying to do here...
// send email to each individual person
mail($row['Email'], $emailSubject, $emailBody, $headers);
}
I think this is how you'd modify your code to get it to do what you're asking.
I have created a password reset function in PHP.
It's working just fine...........except that, for some reason, I'm unable to set the recipient's email address : "TO"
The code works this way :
(a) the user is asked to provide his login/username
(b) php sends an sql query to the database;
(c) if the username is found, php takes the email-address, and sends a Reset Link via email
(d) this reset-link has a unique "token" attached to it
(e) the user clicks on the link in his email, and is re-directed to a new page where he resets his password
Everything is working fine...........except for the email structure itself. The email comprises : TO, CC, SUBJECT, BODY, and HEADERS.
Everything is being shown..........except the actual "TO".
In fact, the only reason I know that the code works is because I'm getting a copy of the email, via the the "CC"
Here is my code :
if(isset($_POST['submit'])) {
$login = $_POST['login'];
$query = "select * from personal_data where login='$login'";
$result = mysqli_query($conn,$query);
$count=mysqli_num_rows($result);
$rows=mysqli_fetch_array($result);
if($count==0) {
echo "Sorry; that username does not exist in our database";
}
else {
function getRandomString($length)
{
$validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789!#+=%&/?*$";
$validCharNumber = strlen($validCharacters);
$result = "";
for ($i = 0; $i < $length; $i++) {
$index = mt_rand(0, $validCharNumber - 1);
$result .= $validCharacters[$index];
}
return $result; }
$token=getRandomString(40);
$q="insert into token (token,login) values ('".$token."','".$login."')";
mysqli_query($conn,$q);
function mailresetlink($to,$token){
$to = $rows['email'];
$subject = "Password Reset";
$uri = 'http://'.$_SERVER['HTTP_HOST'] ;
$message = '
<html>
<head>
<title>Password Reset Link</title>
</head>
<body>
<p>We received a Password-Reset request from your account.</p>
<p>Click on the following link to reset your password : Reset Password</p>
</body>
</html>
';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: Support<support#xxxxx.com>' . "\r\n";
$headers .= 'Bcc: Info<info#xxxxx.com>' . "\r\n";
if(mail($to, $subject, $message, $headers)) {
echo "A password reset link has been sent to your email address."
}
}
if(isset($_POST['login'])) {
mailresetlink($email,$token);
exit();
}
}
}
The reason why your code is not working is due to a few things.
One of which is that $rows needs to reside inside the function mailresetlink($to,$token) function's parameter.
Change that to function mailresetlink($to,$token,$rows) and do the same for the one inside if(isset($_POST['login'])){...}
if(isset($_POST['login'])) {
mailresetlink($email,$token,$rows);
exit();
}
Plus, if it isn't a typo or a bad paste; there is also a missing semi-colon in this line:
echo "A password reset link has been sent to your email address."
^ right there
Having done all of the above, successfully sent all of the information to Email during my test.
Sidenote: Your present code is open to SQL injection. Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.
You cannot define functions in if or while or whatever scope. Define them before or after you intend to use them. Try with the following code:
<?php
if ( isset($_POST['submit']) ) {
$login = $_POST['login'];
$email = $_POST['email'];
$query = "select * from personal_data where login='$login'";
$result = mysqli_query($conn, $query);
$count = mysqli_num_rows($result);
$rows = mysqli_fetch_array($result);
if ($count == 0) {
echo "Sorry; that username does not exist in our database";
} else {
if (isset($_POST['login'])) {
mailresetlink($email, $token, $rows);
exit();
}
}
}
function getRandomString($length)
{
$validCharacters = "ABCDEFGHIJKLMNPQRSTUXYVWZ123456789!#+=%&/?*$";
$validCharNumber = strlen($validCharacters);
$result = "";
for ($i = 0; $i < $length; $i++) {
$index = mt_rand(0, $validCharNumber - 1);
$result .= $validCharacters[$index];
}
return $result;
}
$token = getRandomString(40);
$q = "insert into token (token,login) values ('" . $token . "','" . $login . "')";
mysqli_query($conn, $q);
function mailresetlink($to, $token, $rows)
{
$to = $rows['email'];
$subject = "Password Reset";
$uri = 'http://' . $_SERVER['HTTP_HOST'];
$message = '
<html>
<head>
<title>Password Reset Link</title>
</head>
<body>
<p>We received a Password-Reset request from your account.</p>
<p>Click on the following link to reset your password : Reset Password</p>
</body>
</html>
';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: Support <support#xxxxx.com>' . "\r\n";
$headers .= 'Bcc: Info <info#xxxxx.com>' . "\r\n";
if (mail($to, $subject, $message, $headers)) {
echo "A password reset link has been sent to your email address.";
}
}
?>
Also, pay attention to Quentin's advice about preventing SQL injection.
What I did was:
Moved getRandomString and mailresetlink after the if block
Added parameter $rows to mailresetlink function, so it can find use of the $rows variable (which was out of the scope)
You also need to define $email, because it's not being set anywhere, so I did it for you (I guess you also have an input field with the name of email somewhere.
Test it, it should work.
I want to be able to put the name of the person in the email. But I don't know how to retain the value outside the loop. Also, I'm wondering if this will work if there is more than one result.
Here is the php:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
}
if (!empty($mail_to)){
sendEmail($mail_to);
}
}
// SEND EMAIL
function sendEmail($mail_to) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
In that example above, the mail never reads their name, as the value seems to be forgotten outside of the while loop. Can someone help me?
Currently, your code overwrites the email id stored in $mail_to in each iteration of the while loop. So, it will have only the email id of the last user. And you need name of each user (stored in $name and used in the function). You need to send the email whenever a user is found. Try this:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'];
$name = $row['name'];
if (!empty($mail_to))
sendEmail($mail_to, $name);
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
To solve your problem you need to put the send mail function inside the loop and add a name parameter in the function since you are using it inside the function.
Something like this might do the trick:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
$name = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
if (!empty($mail_to) and !empty($name)){
sendEmail($mail_to, $name);
}
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
If you use the phpmailer, you could do some like:
function joinDateFilter(){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "webmaster#mydomain.com";
$mail->Subject = "Congratulations";
$mail->Body = "Happy Anniversary!";
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
$mail->AddBCC($row['email'],$row['name']);
}
$mail->Send();
$mail->ClearAddresses();
}
Or if you want to have diffrent Subjects than you could do this...
function joinDateFilter(){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "webmaster#mydomain.com";
$mail->Body = "Happy Anniversary!";
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
$mail->Subject = "Congratulations " . $row['name'];
$mail->AddAddress($row['email'],$row['name']);
$mail->Send();
$mail->ClearAddresses();
}
}
Anyway, you save 1 function and its kind of handy to :D
And its also very easy to add attachments or sending html emails and stuff like this. How to do most things can be googled. So you may need a small tutorial.
If you download the mailer pac, you really only need the class.phpmailer.php the rest is not necessary :)
This should work:
<?php
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
if (!empty($mail_to)){
sendEmail($mail_to, $name);
}
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
?>
Keep in mind that mail function can be sometimes problematic. Google about that issue. What you can use as an replacement for the mail function is SwiftMailer
Currently I have 2 forms. On first user have to send code and receive it, and submit to second form and approve account. I need, when they put email and click submit, on email automatically is added code which they should get on their email, but they dont have to copy/paste code, because it should do automatically. Search for mysql_query("UPDATE users SET verify = 'verified', bullets = bullets + 5000 WHERE ID = '$ida'");
$showoutcome++; $outcome = "Your account is now verified!"; } - Here I need to add, $verifnum, because that's the code which they should get on their email, but as I said script should approve it automatically, and I will use only one form where they enter just email and click verify.
<?php
$saturate = "/[^a-z0-9]/i";
$saturated = "/[^0-9]/i";
$sessionidraw = $_COOKIE['PHPSESSID'];
$sessionid = preg_replace($saturate,"",$sessionidraw);
$userip = $_SERVER[REMOTE_ADDR];
$gangsterusername = $usernameone;
$playerrank = $myrank;
$playerarray =$statustesttwo;
$playerrank = $playerarray['rankid'];
$email = $playerarray['email'];
$verified = $playerarray['verify'];
$ref = $playerarray['ref'];
if($verified == 'verified'){die('<font color=silver face=verdana size=1>Your account is already verified!'); }
if($_POST['verify'] AND $_POST['email']){
$newemail = $_POST['email'];
if(!preg_match("/^[\ a-z0-9._-]+#[a-z0-9.-]+\.[a-z]{1,20}$/i", $_POST['email'])){ $showoutcome++; $outcome = "The email you entered is invalid!"; }else{
$verifnum = rand(1111,9999);
$to = "$newemail";
$subject = "SG - Email Verification";
$header = "From: State Gangsters - Email Verification <admins#stategangsters.com>\r\n" .
'Reply-To: State Gangsters <noreply#sgangsters.com>' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";
$body = "Your verification code is $verifnum!";
if (mail($to, $subject, $body, $header)){ $showoutcome++; $outcome = "An email has been sent, please check your inbox!";
mysql_query("UPDATE users SET verify = '$verifnum', email = '$newemail' WHERE ID = '$ida'");
}}}
if($_POST['code'] AND $_POST['verifyit']){
$newcode = $_POST['code'];
$getcodee = mysql_query("SELECT verify FROM users WHERE ID = '$ida'");
$doit = mysql_fetch_array($getcodee);
$getcode = $doit['verify'];
if($newcode == $getcode AND $getcode > 0){
mysql_query("UPDATE users SET verify = 'verified', bullets = bullets + 5000 WHERE ID = '$ida'");
$showoutcome++; $outcome = "Your account is now verified!"; }
else{ $showoutcome++; $outcome = "The verification code you entered is incorrect!";
}}
?>
if($_POST['code'] AND $_POST['verifyit']) {
Change that to use $_GET, and create a link in your e-mail that will post back to the page with the appropriate variables, e.g.
$body = "Your verification code is <a href='$PHP_SELF?code=$verifnum&verifyit=1'>$verifnum</a>!";
This aside, your code is really messy (three functionalities in one script), full of obsolete things (<font color=silver>?), weird constructions (using die for regular program flow?) and guaranteed loopholes (mysql_query with variables inserted directly in the SQL?!?!!!). It's not clear where your $ida comes from anyway, but I'm guessing (hoping) that's a consequence of copy/pasting code here for a minimal example.