How to get mysqli table variable into $message - php

I have a reminder mail function on my website. I get the email adresses from my sql database, and it is working just fine. But I also have an item no. from the database that I have to put in the mail to tell the users which item have expired and need to be returned. I have the mail text inside a $message. But I don´t know how to get the item no. variable from the database inside the message. I have been looking around for examples and tutorials, but nothing works. This is what I´ve got so far. Any one of you got some advice?
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$message = "Dear user \n\n Please return item no. $row['Item_no'] as soon as possible. \n\n Regards Thomas";
//echo ($message);
mail ( $row['Email'], "Reminder", $message );

You just need to concatenate $row['Item_no'] inside your echo.
$message = "Dear user \n\n Please return item no. " . $row['Item_no'] . " as soon as possible. \n\n Regards Thomas";

Related

PHP Email $Subject Line Will Not Pull Variable

I've searched around with no luck here. I created a PHP email form that works great. Now I need to edit the email subject line to pull information from the contact form.
Here's what I'm looking to have as the email subject line:
"You have a $variable inquiry from $variable.
Here's my current code that is giving me an error on the $Subject line:
$EmailFrom = "Contact#xxx.com";
$EmailTo = "ryan#xxx.com";
$Subject = "You have a .$Subj inquiry from .$Name";
$Name = Trim(stripslashes($_POST['sender_name']));
$Email = Trim(stripslashes($_POST['sender_email']));
$Message = Trim(stripslashes($_POST['message']));
$System = Trim(stripslashes($_POST['system']));
$Item = Trim(stripslashes($_POST['item']));
$Subj = Trim(stripslashes($_POST['subj']));
So I have tried adding a <> around the variables in the $Subject. I have also tried with the periods as above, as well as with just the $xxx.
When I send the email, it will send as:
"You have a <> inquiry from <>"
or
"You have a . inquiry from ."
Depending on which characters I have around the $xxx variable.
I know this is probably some stupid error, but can someone hint me at what I need to do to have those variable pulled into the $Subject?
Thanks in advance,
Ryan
You have your variable declarations out of order.
You are using $Name in the first line before you have assigned a value to it. You need to put the lines that give values to $Name and $Subj before the line that uses them.

Get all rows into a variable then mailto for each PHP

So, I found this thread (Get all data from mysql row in a variable) but I am too much of a beginner to make it apply easily to my situation. Thank you for helping me out... sorry for the total newb questions.
I have a PHP form that lets the user select one of my tables in a database where email addresses are stored to send an email to each of them. Right now, I have this code:
$recipientid = $_POST['recipientid'];
$body = $_POST['body'];
$subject = $_POST['subject'];
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: SENDER NAME <senderemail#gmail.com>' . "\r\n";
date_default_timezone_set('America/Los_Angeles');
$time = date('m/d/Y h:i:s a', time());
$sendbody = $body . "<br><br>This is a bulk email announcement sent by
the institution.<br><br>It was sent at " . $time . ". If you have any
questions about this message or wish to unsubscribe, please contact
the institution.";
if($recipientid == 'allstudents'){
// SEE NOTE #2//
$recipientlist = //email addresses
}
$process=explode(",",$recipientlist);
reset($process);
foreach ($process as $to) {
$sent=mail($to,$subject,$sendbody,$headers);
}
if ($sent) { //(success echo goes here... it is quite long so i removed it.)
} else {
echo "Email could not be sent, PLEASE CONTACT US.";
}
What is the easiest way to capture all of the email addresses in the column of the specified table and then loop a mailto for each? I was originally trying to get them all into one string and then explode them as you can see, but that might not be the best solution. Am I on the right track here?
(NOTE #2 FROM IF)
HERE IS WHERE I NEED SOMETHING... I was sort of thinking about trying to use the following. I need it to grab all the emails from the column emailaddresses in the table students. I am using an if statement because there are four other things that $recipientid could equal, and each different one grabs email addresses from a different table.
array pg_fetch_all_columns ( resource $result [, int $column = 3 ] ) But then, I don't know how to get this array to work with my mail. I originally tried to use just a SELECT * from emailaddresses and then use each row somehow but I don't know how to implement that.
YES, I know I am using mysql not mysqli and I know that mailto is probably not the best solution, but it is what I have to work with right now (unless you can suggest an alternative route for the mail loop).
Thank you again! I really want to learn what I am doing, so an explanation would be appreciated:)
(and ps I am using the mail function with the explode because of this article http://tutorial.world.edu/web-development/php-script/how-to-send-out-mass-emails-php-script/)
I might be a little confused about the question. It sounds like you have a database with email address and you want to send an email for each email address. I think you can just do the query SELECT emailaddress from table and cycle through the results and use your mail function each time.
$query = *your select query*
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$sent=mail($row['emailadress'],$subject,$sendbody,$headers);
if ($sent) { //(success echo goes here... it is quite long so i removed it.)
} else {
echo "Email could not be sent, PLEASE CONTACT US.";
}
If you want your user to select the table the email addresses are coming from you can use a form and a variable in the query.
You can use the below piece of code to send the mail.
Consider you have an email field in your table 'students'
then
$sel = mysql_query("select email from students");
while($info = mysql_fetch_assoc($sel))
{
$to = $info['email'];
$sent=mail($to,$subject,$sendbody,$headers);
//---Remaining code goes on
}
try it.

Limit contact form emails per second/minute

I recently made a portfolio website and put it online on 000webhost.com. Today when I logged in, the account was suspended because someone sent more then 70 emails in a minute via my contact form - something that the webhosting does not allow.
I am looking for some way to stop this from hapening again. I used both php and javascript/jquery for form validation.
This is my curent php validation code.
$firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["email"];
$message = $_POST["message"];
$to = "fox.team001#gmail.com";
$subject = $firstName . " " . $lastName;
$headers = "From: " .$firstName . " " . $lastName . "\r\nReply-To:" . $email;
if(validateEmail($email)){
#mail($to , $subject , $message , $headers);
}
validate($firstName , $lastName , $email , $message);
function validate ($firstName , $lastName , $email , $message){
if(!empty($firstName) && !empty($lastName) && !empty($email) && !empty($message)){
if(validateEmail($email)){
header("refresh:5; url=http://www.foxteam.net");
}else{
header("refresh:0; url=http://www.foxteam.net/contact.php");
}
}else{
header("refresh:0; url=http://www.foxteam.net/contact.php");
}
}
function validateEmail($email) {
$pattern = "^[A-Za-z0-9_\-\.]+\#[A-Za-z0-9_\-]+\.[A-Za-z0-9]+$";
if(preg_match("/{$pattern}/", $email)) {
return true;
}else{
return false;
}
}
Can anyone tell me how can I stop spammers to send spam emails?
It's very hard to stop spam coming through a contact form completely, however there are a number of methods you can use to reduce it, some of which include:
Use a honeypot - the idea behind this is to have a hidden field on your form with a generic name (e.g. answer), if this field has anything in it, then don't bother sending the email (but still tell the user that the email has been sent) - it is obviously spam as there is no other way the field could have been filled out.
IP limiting - store the user's IP address somewhere and limit the number of emails per minute/hour that each IP address can send.
Word filtering - have a list of words, if any are found then don't send the email (usualy words like viagra, penis, etc).
CAPTCHA, to me, this is a last resort. If you do use one, implement recaptcha, it is by far the best one around. But as I say, use this as a last resort, there are plenty of other methods you can use without annoying the users of your website.
You can use CAPTCHA to block robot spammers
put your if() condition into for loop which contains range 70 and then send email only. if it will over by 70 then put it over else part with some suitable message
Thanks

how to email database and form values

I have a website created using PHP to store form values in a database, and have a page that displays the values for certain users based on the selections. I am trying to send these values from the database to an email address, but I can not separate the values on each line of the subject. I am using the PHP built in function to email and storing the values in the subject variable.
I have tried add each variable separately by using
$subject . "content";
for each variable but it still is all in one line.
I am now thinking of making a newsletter type, but can't figure out how to make one or make it work how I want. So if anyone could help out on how I can send these values either in this way or in a different language.
Here is the code I have so far.
$query= sql query
$resultt= sql result
$roww=mysql_fetch_assoc($resultt);
extract($roww);
$emmessage = "User Form Information";
$emmessage = $emmessage . " " . "values extracted from the database from $roww
$send = #$_POST['send'];
$subject = strip_tags(#$_POST['subject']);
$reciever= strip_tags(#$_POST['email']);
$message = $emmessage;
// Start email processing
if ($send)
{
// Send the message
mail($reciever, $subject, $message, "From: $email");
$emessage="Your message has been sent";
include("forme.php");
You can used br2nl() function. Or if you are used \n directly as suggested before you should use nl2br() function so that in HTML page you will get a new line instead of a \n.
You can seperate lines in e-mails using \n.
$content = "Value 1 \n Value2";

Sending Email to Multiple Recipients from MySql Recordset

I want to send an email to multiple recipients using PHP mail() function. The email message is simply a reminder that a membership is due to expire so email addresses will come from MySql database query. There would anywhere from 2-10 at any given time. I found the following code but it generate errors. The problem is not with my query as it generates an accurate recordset. This is the code I have: Hopefully someone can help. By the way, I am very much a novice so need easy straight forward explanation. Thanks in advance:
<?php
$recipients = ("SELECT email FROM tblMembers WHERE search criteria=criteria");
$email_list = $db->query($recipients);
foreach($email_list as $row) {
$to = $row['email'];
$subject = "Membership Renewal";
$headers = "From: Membership Coordinator <membership#myaddress.net>\r\n";
$message = "THIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY""\n""etc, etc, etc";
if ( mail($to,$subject,$headers,$message) ) {
echo "Email was sent successfully";
} else {
echo "Email delivery has failed!";
}
}
?>
As far as I know, then $headers comes after $message, so you should just change the order in mail() and be more aware in future.
Change
$message = "THIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY""\n""etc, etc, etc";
to
$message = "THIS IS AN AUTOMATED EMAIL. PLEASE DO NOT REPLY\netc, etc, etc";
There is the syntax error, because " will end the string. You would need a . to concatenate the next string.
But you could also leave the two " out at this point, becase in a double quoted string, PHP will replace \n by a newline character.

Categories