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.
Related
I need help for this PHP script:
<?php
$link = mysql_connect('localhost', 'Username', 'PW');
$sql = "SELECT Driver, Mail FROM mytable";
$query = mysql_query($sql);
$emailBody="";
$Subject="test";
$to ="xxx";
while($row = mysql_fetch_assoc($query))
{
$emailBody .= "Driver: ".$row['Driver']."\n";
}
$to = str_replace('xxx', $row['Mail'], $to);
mail($to, $Subject, $emailBody);
I tried many things:
mail($to, $Subject, $emailBody);
mail($row['Mail'], $Subject, $emailBody);
I cannot set the field value of row['Mail'] as the receiver address.
The field values have been tested with "mymail#mail.com" and 'mymail#mail.com' and mymail#mail.com .
The replace function was just another trial.
$to =str_replace('xxx',$row['Mail'],$to);
Nothing works except hardcoding for test only..
mail("mymail#mail.com", $Subject, $emailBody);
Thanks!
$row only exists inside your loop (i.e. inside the { and }). But your $to line exists outside the loop, therefore it cannot access any values from $row.
If, as you mention in the comments, you wish to generate a separate email for each row of your query results then
a) you need to move the code which sets the recipient and sends the mail inside the loop so it can access the row data, and execute once per row
and
b) you need to not concatenate the body with data from every row, otherwise every email after the first will contain details from all the previous rows as well as the current one.
This should work better:
$link = mysql_connect('localhost', 'Username', 'PW');
$sql = "SELECT Driver, Mail FROM mytable";
$query = mysql_query($sql);
$Subject="test";
while($row = mysql_fetch_assoc($query))
{
$emailBody = "Driver: ".$row['Driver']."\n";
mail($row['Mail'], $Subject, $emailBody);
}
P.S. As I mentioned in the comments, you need to urgently replace the obsolete mysql_ calls with mysqli or PDO, and urgently upgrade to a supported version of PHP. I'd also recommend using a library like PHPMailer to handle your email sending - it's easier to work with than mail() and is more likely to generate valid emails which won't get accidentally blocked as spam etc. It can also support SMTP where mail() only supports local mailservers via sendmail.
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";
Just looking for advice on how to apply the urlencode into this bit of code. It is actually working, the issue when the email is received, the urlencode doesn't seem to work.
function reset_password($email) {
$query = "DELETE from reset_password where email = $email";
$deletepass = mysql_query($query);
$code = substr(base64_encode(crypt('', '')), 0, 32);
$query2 = "INSERT into reset_password values ($email, '$code', " . time() . ")";
$insertval = mysql_query($query2);
$f = "SELECT userEmail from gn_users where email = $email";
$from = "***"; // sender
$f['userEmail']; // recepient
$message =
"From: *** <***>\r\n" . // email headers
"To: {$f['userEmail']} <{$f['userEmail']}>\r\n" .
'Subject: Reset Password' . "\r\n" .
"\r\n" .
"Hello\r\n" . // email imap_body(imap_stream, msg_number)
"\r\n" .
"A request has been made to reset your example.com web site password.\r\n" .
"\r\n" .
"To complete the request, click on the following link within 48 hours of the transmision of this email and follow the on screen instructions.\r\n" .
"\r\n" .
"index.php?page=reset-password&email=" . urlencode($email) . "&code=" . urlencode($code) . "\r\n" .
"\r\n" .
"Kind regards,\r\n" .
"\r\n" .
"The example.com Web Site";
$to = "$email";
$subject = "Test mail";
$message = "$message";
$from = "***";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";}
I'll try to help you out with some tips to write one yourself, without having to rely on that awful code.
You need to break the task down in multiple, bite sized, easy to do tasks.
Here we go:
The user needs a page where to request a password reset. It's a form with an email field (and/or username).
We have the user email, and if it exists, we need to generate a reset password link that can't be guessed, so that not everyone can reset someones password.
So you need to generate a unique hash for this request, an option would be uniqid(), but there are many options here. So you generate a link that looks like: http://test.com/reset.php?uid=443&hash=33rr3344rree22. It can't really be guessed because you need to know both the user id and the has. But to make sure, we will make it expire in an hour or day.
Next, we ensure that this link will work. We have to create a table for password reset requests that contains the following columns: id, email, hash, date_added, and insert it (the date can be TIMESTAMP with a default of CURRENT_TIMESTAMP).
Now it's time to send the email. You can add any text you want, as long as you mention the url you generated a bit earlier.
Now the user clicks the link. You get the user id and hash, and check if such an entry exists. If it does, and the request is not older than 1 day, we generate a new password, update the users table, and send him a confirmation mail.
This is optional, but recommended. Create a cron job that clears the password reset request table for entries older than 1 day.
Each of these steps are fairly easy to do, or you can find a lot of information about them around the web. If you take your time to understand each step, sanitize everything properly, and do things by the book, you will learn a lot.
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";
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.