I have a format simple certificate. The certificate needs to be populated with values from the database and emailed. Below is the quick fix I did. The problem is the certificates sent are not of one person instead of those queried.
$query ="SELECT r.email, r.LastName, r.OpNo,
r.QuizNo From tbl_cert where Pass =1 AND Printed is null;
$result=mysql_query($query);
while( ($row= mysql_fetch_array($result)) ){
$subject = "CPD Certificate";
$email = $row['email'];
$LastName = $row['LastName'];
$OpNo = $row['OpNo'];
$TestNo = $row['QuizNo'];
$headers = "From: online#example.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html;
charset=iso-8859-1' . "\r\n";
$mail_body ='<html>...Here its were my my
html comes with values from table...</html>';
if (mail($to, $subject, $mail_body, $headers)){
$query = "UPDATE `tbl_cert` SET Printed = 1
WHERE CertificateNo = " . $CertificateNo;
$certresult=mysql_query($query);
if ($certresult) {
header('Location:tsCertlist.php');
}
The problem is how to get the $mail_body to have both html and php within the while loop. My generated form or certificate was the same for the 4 users who had Passed and had not printed their certificate.
create a separate template file which contains your HTML and the places you want to insert a variable / replace something put in something like %REPLACE_FIRSTNAME%
Then use str_replace() to change your replacement vars with the db/input/whatever variables with the data you require.
$mail_body = file_get_contents("templates/emailtemplate.htm");
$mail_body = str_replace("%REPLACE_FIRSTNAME%",value_from_DB,$template);
Sorted and if you ever want to change your layout just edit the template file accordingly.
$certNo = 0;
//get $certNo from data base
$mail_body ='<html>'.$certNo.'</html>';
//send
Related
I am trying to write some PHP code that emails every user in my database (around 500) their usernames. I have successfully managed to pull the email addresses and usernames for each user and store them in an array. However, I cannot figure out how to email each individual user with their individual usernames. I am pretty sure I need to use a foreach loop to do this, but I have had no luck.
Here is what I have.
<?php
include('databaseConn.php');
$query = mysql_query("SELECT * FROM staff");
$emailArray;
while ($row = mysql_fetch_array($query)) {
$emailArray[] = array($row['email']=>$row['username']);
}
print_r($emailArray); //This associative array now contains each username along with their respective email address.
?>
==***********===
WITH MAIL FUNCTION
<?php
include('functions/core.php');
$query = mysql_query("SELECT * FROM users");
$emailArray;
while ($row = mysql_fetch_array($query)) {
$emailArray[] = array($row['email']=>$row['username']);
}
foreach($emailArray as $email => $username) {
echo $username; // outputs the indexes.
$subject = 'Accoutn Details';
$message = 'This email contains your login details.<br/><br/>
<b>Username: '.$username.'</b><br/>
<br/><br/>Kind regards,<br/>';
$headers = 'From: noreply#xxxxx.co.uk' . "\r\n" .
'Reply-To: noreply#xxxxx.co.uk' . "\r\n" .
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($emailAddress, $subject, $message, $headers);
}
//print_r($emailArray);
?>
bad idea to use mail in a loop unless its a one off, which it sounds like it, also most shared hosts would not allow 500 emails to be sent at once.
$subject = 'Account Details';
$headers = 'From: noreply#xxxxx.co.uk' . "\r\n" .
'Reply-To: noreply#xxxxx.co.uk' . "\r\n" .
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
while ($row = mysql_fetch_array($query)) {
$message = 'This email contains your login details.<br/><br/>
<b>Username: '.$row['username'].'</b><br/>
<br/><br/>Kind regards,<br/>';
mail("$row['email']", $subject, $message, $headers);
}
If your question is how to use foreach... http://www.php.net/manual/en/control-structures.foreach.php
For example:
foreach ($emailArray as $email => $username) {
// Send an email to $email with their $username
}
#Dagon is right, there's no need to iterate twice if there's nothing else you need the associative array for. Even if you do need an array, it doesn't necessarily have to be associative; it could be the raw rows from the DB, which you could iterate over. The only time you need an associative array is when you need to look up the values (usernames) by the keys (email addresses), which, as far as I can tell, you don't need to do.
I'm really new to PHP, so this is probably a pretty dumb question.
I'm using PHP to submit an email form, and would like the email to contain the values of some of the form's inputs. Here's a stripped down version:
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message =
//here are the values that the email will send me
"<p>".$_POST('some-name')."</p>
<p>".$_POST('some-name')."</p>" ;
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
} ?>
The value of input some-name is, say, 10-widgets posted from the form.
Here's the question: instead of listing "10-widgets" twice (as the above code will do), how do I list the first part in the first <p> (so it would be "10") and the second part in the second <p> (so it would be "widgets")?
Something like the following seems promising:
$wholeVal = $_POST('some-name');
$partVal = explode("-",$wholeVal);
and then, somewhere, $_POST($partVal[0]); and $_POST($partVal[1]);
But I don't know where this should take place, and anywhere I put it seems to make the whole thing break.
Thanks for your help.
First, you should access $_POST with brackets, like $_POST['some-name']. explode returns an array. So in your example, explode('-', '10-widgets')[0] will return '10' and explode('-', '10-widgets')[1] will return 'widgets'
So your code will be something like this:
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$parts = explode('-', $_POST['some-name']);
//here are the values that the email will send me
$message =
"<p>".$parts[0]."</p>
<p>".$parts[1]."</p>";
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
}
?>
I know you're looking for functionality at the moment but keep in mind that if you're splitting a string by a "-" it'd be really easy for a script kiddie (or even a well intentioned person for that matter) to use a hyphenated word and your script will break.
Just a heads up :D
Try that
$wholeVal = $_POST('some-name');
$list($ammount, $type) = explode("-",$wholeVal);
Then into your email:
$message = "<p>" . $ammount . "</p><p>" . $type . "</p>" ;
<?php
if(isset($_POST['submit'])) {
$to = 'address#gmail.com' ;
$subject = 'Subject';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message =
$wholeVal = $_POST('some-name');
$partVal = explode("-",$wholeVal);
//here are the values that the email will send me
"<p>".$partVal[0]."</p>
<p>".$partVal[1]."</p>" ;
mail($to, $subject, $message, $headers);
header('Location: ../estimate.html');
} ?>
just done a little bit of code to send out a newsletter based on a sql table.
first one with 70/80 subscribers went off fine, now when i've moved to the second one that has around 250, the body_message of the email is repeated inside the email equally to the number of people on the mailing list, in this case I was sending emails with 250 duplicates of the content inside...
not sure whats wrong with the code, have stripped it down as much as i could and was wondering if someone could talk a look and hopefully point out the issue
<?php
$i=1;
if (isset($_POST['submit_btn'])) {
connect_newsletter();
$result = mysql_query("SELECT id, mail FROM test") or die('Could not connect. ' . mysql_error());
while ($row = mysql_fetch_array($result)) {
$email = $row['mail'];
$nid = $row['id'];
$ip=$_SERVER['REMOTE_ADDR'];
$ref="http://www.domain.co.uk";
$body_message ='newsletter html code';
$y_email="noreply#domain.co.uk";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers4=$y_email;
$headers .="Reply-to: $headers4\n";
$headers .= "From: $headers4\n";
$headers .= "Errors-to: $headers4\n";
$subject="subject";
mail($email,$subject,$body_message,$headers);
echo $i." sent to ".$email;
echo "<br>";
$i++;
}
}
?>
Watching your code that is not possibile because in the loop you safely reset the value of $body and $subject
The problem could be somewhere else. Check your sendmail log
On my site I have a contactUser.php that sends mail and a replyMail.php. I have put PHPMail on my contact user to send email notifications which is working great. When I put the same code chunk on my replyMail (which mail goes to the same place as the contact user mail,the inbox) that shares the same variables its giving me a internal server error. I have tried different GET vars as well as echoed die($vars) and echoed queries. Any ideas what the problem might be. Here is my code:
$prof = new User($_GET['id']);
$query = "SELECT `Email` FROM `Users` WHERE `id` = '" . $prof->id . "'";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$Email = $result['Email'];
$to = $Email;
$subject = "$auth->first_name $auth->last_name sent you a message";
$message = "$auth->first_name $auth->last_name sent you a message:<br /> <br /> <a href='http://www.blah.org/Inbox.php'>Click here to view</a><br /><br /> The Team";
$from = "blah<noreply#blah.org>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:$from";
mail($to, $subject, $message, $headers);
I really suggest wrapping it up into a function. From what you've posted, I get the sense that you have all the code in the global namespace, in which case any number of auto-prepends or includes could easily be messing with variables.
Something like:
// Function that accepts a user object and a database connection, then sends a notice email.
function email_message_notice($prof, $connection){
$query = "SELECT `Email` FROM `Users` WHERE `id` = '" . $prof->id . "'";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$Email = $result['Email'];
$to = $Email;
$subject = "$auth->first_name $auth->last_name sent you a message";
$message = "$auth->first_name $auth->last_name sent you a message:<br /> <br /> <a href='http://www.blah.org/Inbox.php'>Click here to view</a><br /><br /> The Team";
$from = "blah<noreply#blah.org>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:$from";
$res = mail($to, $subject, $message, $headers);
return $res;
}
$prof = new User($_GET['id']);
$sent = email_message_notice($prof, $connection);
Since there's a database involved, this is untested code, so you'll want to test that it works and want to debug any further issues yourself, but otherwise it should be a good way to compartmentalize and share the code. If you're doing exactly the same thing in both scripts, you can just include the email_user function in a separate include.
Look into the server log. Should tell you what is wrong. Maybe file permissions.
Is that the whole script? I can't see anything there that could cause a 500 error. Can you post the whole page or a link to the source code of the whole page?
Is this a part of some loop or mass mailing that gets called repeatedly?
Is there no way to get hold of the Apache error logs? Only they will give you a definite answer what goes wrong.
I am trying to send notification emails(which is working fine) but have added the html headers to try to send links etc...for some reason nothing is showing up at all, just blank space where the desired links are supposed to be. Here is my code:
if(isset($_POST['commentBlogSubmit']) && $auth) {
$query = "SELECT `Email` FROM `Users` WHERE `id` = '" . $prof->id . "'";
$request = mysql_query($query,$connection) or die(mysql_error());
$result = mysql_fetch_array($request);
$Email = $result['Email'];
$to = $Email;
$subject = "Someone sent you left you a comment";
$message = "You have a new blog comment <br />".
" <a href='http:www.blah.org/indexNew.php'></a>";
$from = "info#blah.org";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from";
mail($to, $subject, $message, $headers);
}
Perhaps because you have no text inside the link tag?
Because the PHP email function generally sends plain text.
Rather than trying to do this yourself, you should probably use Mail_Mime
Also, though your headers are probably correct, you have nothing between the <a> and </a> tags.