I am at the end of my rope and would really really appreciate any help you could spare...
I have the following code and although it works on an individual basis, I can for the life of me not make it work so that all email addresses are sent at the same time using PHPMailer.
I have scoured StackOverflow for the past few months, trying an exhaustive amount of combinations without success.
There are a number of discussions on this forum and although I have tried all the solutions on here, I can still not make it work. If I offend anyone by potentially duplicating the question, please accept my apologies in advance.
<?php
// Script Error Reporting
//error_reporting(0);
// Require the form_functions to process the form
require('databaseConnect.php');
// Require the Email Class Functions
require("mailApp/class.phpmailer.php");
require("mailApp/class.smtp.php");
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPKeepAlive=true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->setFrom("No_Reply#girrawaagames.com", "Girrawaa Games"); // Valid email address from sender and Company name. Only Company name will be displayed
$mail->Subject='CASH Trader | The Spirit Stone'; // This adds the subject title in the subject line field
// Create a connection to MySQL and the database:
$con = mysqli_connect($hostname, $username, $password, $database);
// Check if the connection is active:
if(!$con) {
header('Location: alternate.php');
}
$result = mysqli_query($con, "SELECT fname, email FROM emails WHERE condition = 'condition'");
/* fetch associative array */
while ($row = $result->fetch_array()) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
$mail->AddAddress($user['email']);
$mail->Body="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>CASH Trader</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
</head>
<body yahoo bgcolor=\"#ccffff\" style=\"margin:0; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px;\">
<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\" style=\"border-collapse:collapse\">
<tr>
<td style=\"padding-top:20px; padding-right:0px; padding-bottom:30px; padding-left:0px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\">
<tr>
<td class=\"header\" align=\"center\" bgcolor=\"#333333\" style=\"padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<img src=\"http://www.girrawaagames.com/img/logo.jpg\" style=\"color:#FFFFFF\" alt=\"Cash Trader Logo\" style=\"display:block; max-width:100%; height:auto;\" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=\"#ffffff\" style=\"padding-top:40px; padding-right:30px; padding-bottom:20px; padding-left:30px;\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
<tr>
<td align=\"center\" style=\"color:#153643; font-family:Arial, Helvetica, sans-serif; font-size:24px;\">
<b>Hi " . $user['fname'] . "</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>";
print_r($user['fname']);
print_r($user['email']);
if ($mail->send()) {
$updateCampaign = "UPDATE emails SET column = 'value' WHERE email = '" . mysqli_real_escape_string($row['email']) . "'";
$results = mysqli_query($con, $updateCampaign);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
mysqli_free_result($result);
mysqli_close($con);
You need to concatenate the $useremail for it to have multiple emails.
$userEmail = '';
foreach($rows as $row) {
$userFname = $row["fname"];
$userEmail .= $row["email"] . ',';
}
It would probably be easier to pair these in an array..
foreach($rows as $row) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
Then you don't need the explode function later.
....
or better yet set it in the while loop, dont need foreach at all.
while($row = $result->fetch_array()){
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
Update:
<?php
// Script Error Reporting
//error_reporting(0);
// Require the form_functions to process the form
require('databaseConnect.php');
// Require the Email Class Functions
require("mailApp/class.phpmailer.php");
require("mailApp/class.smtp.php");
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPKeepAlive=true; // SMTP connection will not close after each email sent, reduces SMTP overhead
$mail->setFrom("No_Reply#girrawaagames.com", "Girrawaa Games"); // Valid email address from sender and Company name. Only Company name will be displayed
$mail->Subject='CASH Trader | The Spirit Stone'; // This adds the subject title in the subject line field
// Create a connection to MySQL and the database:
$con = mysqli_connect($hostname, $username, $password, $database);
// Check if the connection is active:
if(!$con) {
header('Location: alternate.php');
exit();
}
$result = mysqli_query($con, "SELECT fname, email FROM emails WHERE condition = 'condition'");
/* fetch associative array */
while ($row = $result->fetch_array()) {
$user['fname'][] = $row["fname"];
$user['email'][] = $row["email"];
}
foreach($user['email'] as $key => $email) {
$mail->AddAddress($email);
$mail->Body="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>CASH Trader</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
</head>
<body yahoo bgcolor=\"#ccffff\" style=\"margin:0; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px;\">
<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\" style=\"border-collapse:collapse\">
<tr>
<td style=\"padding-top:20px; padding-right:0px; padding-bottom:30px; padding-left:0px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"600\">
<tr>
<td class=\"header\" align=\"center\" bgcolor=\"#333333\" style=\"padding-top:10px; padding-right:10px; padding-bottom:10px; padding-left:10px; color:#153643; font-size:28px; font-weight:bold; font-family:Arial, Helvetica, sans-serif;\">
<img src=\"http://www.girrawaagames.com/img/logo.jpg\" style=\"color:#FFFFFF\" alt=\"Cash Trader Logo\" style=\"display:block; max-width:100%; height:auto;\" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=\"#ffffff\" style=\"padding-top:40px; padding-right:30px; padding-bottom:20px; padding-left:30px;\">
<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">
<tr>
<td align=\"center\" style=\"color:#153643; font-family:Arial, Helvetica, sans-serif; font-size:24px;\">
<b>Hi " . $user['fname'][$key] . "</b>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>";
if ($mail->send()) {
$updateCampaign = "UPDATE emails SET column = 'value' WHERE email = '" . mysqli_real_escape_string($row['email']) . "'";
$results = mysqli_query($con, $updateCampaign);
}
// Clear all addresses and attachments for next loop
$mail->clearAddresses();
mysqli_free_result($result);
mysqli_close($con);
}
You say you've looked everywhere... apart from code you already have! There is a mailing list example provided with PHPMailer that does exactly what you ask, correctly and efficiently.
In your code you're trying to send one message to 50 people, not 50 to 50, and it would also expose all addresses to all recipients, so this is generally a bad idea.
You've based your code on an obsolete example and are probably using an old version of PHPMailer, so get the latest.
Related
Need some help.
I am trying to create a script that sends an html email to a shopper with a list of all the orderd items. I want the items fetched from the database to be stored in a single variable so that I can embed it in the message variable of mail() function. Or Just the Items fetched to be embeded in the email.
Have tried the code below but it only displays 1 item and not all that are in the loop. How can I do it?
I'll be grateful for your help.
$message3 = "
</td>
</tr>
<tr bgcolor='#A8CE58'><td><b>ITEMS ORDERED</b></td></tr>
<tr>
<td>
<table border='' style='margin-bottom: 10px; margin-top:10px; padding: 1px;border-right: : 1px solid #000;'>
<tr align='left'>
<th>---</th>
<th>Name</th>
<th>Qty</th>
<th>Total</th>
</tr>";
$ress= mysqli_query($server, "SELECT * FROM ordered_products WHERE session='$session'");
mysqli_data_seek($ress, 0);
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM))
{
$message4 =
"
<tr align='left'>
<td><img src='e_images/$colms[8]' height='auto' width='50px'></td>
<td>$colms[2]</td>
<td >$colms[9]</td>
<td>ksh.$colms[5]</td>
</tr>";
}
$message = "$message3". $message4;
You need to concatenate the string
$message4 = '';
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM)) {
$message4 .= //your html
}
What you doing now is overwrite the value of $message4 each time in the loop
With
.=
You solve this. Don't forget to declare the variable before the loop, else you will get a warning from PHP.
I am unable to generate auto mail on daily bases. The issue is it is not able to generate it automatically. Where as i have a same sort of code which does generates it everyday. With all the same details. The code is exactly same except the database query.
Here is my code:
<?php
session_start();
require("../connect.php");
/* require files for MAIL */
require("class.phpmailer.php");
require("class.smtp.php");
/* require files for MAIL ENDS */
/******** Get the list of audits done in week risk category falls under High Risk and during the previous visit also the location was under High risk category ----mail on every Saturday**********/
$current_date = date('Y-m-d');
//$current_date ='2015-08-22';;
/*$start_time = time() - (6*86400);
$start_dt = date('Y-m-d',$start_time);*/
$pagent_array = array();
$query="Select * from `location_details` ld
Left Join `answer_general_qc` ans_general on ans_general.lid=ld.id
Left Join `audit_general` general on general.qtno = ans_general.qt
Left Join `audit_general_option` general_opt on general_opt.opt_id = ans_general.ans_remark
where ld.completed ='1' and ld.priority != 'qc' and general.qtno ='1' and ans_general.opt = 'no' and
STR_TO_DATE(ld.`date_audited`,'%d-%m-%Y') = '".$current_date."'";
$result=mysql_query($query);
$th_style="border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede";
$td_style="border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff";
$table_result .="<table align='center' rules='all' style='font-family: verdana,arial,sans-serif;font-size:11px;color:#333333;border-width: 1px;border-color: #666666;border-collapse: collapse;' border='1';>
<thead>
<tr>
<th style='".$th_style."'>Sr No</th>
<th style='".$th_style."'>Location Code</th>
<th style='".$th_style."'>Location Name</th>
<th style='".$th_style."'>PAgent</th>
<th style='".$th_style."'>Address 1</th>
<th style='".$th_style."'>Address 2</th>
<th style='".$th_style."'>District</th>
<th style='".$th_style."'>State</th>
<th style='".$th_style."'>Priority</th>
<th style='".$th_style."'>Remark by TO</th>
<th style='".$th_style."'>Remark by TO</th>
</tr>
</thead>
<tbody>";
$sr_no=1;
if(mysql_num_rows($result) != 0)
{
while($row=mysql_fetch_array($result))
{
if($row['option_text'] == "")
{
$option_text="Other";
}
else
{
$option_text=$row['option_text'];
}
$table_result .= "
<tr>
<td style='".$td_style."'>".$sr_no."</td>
<td style='".$td_style."'>".$row['location']."</td>
<td style='".$td_style."'>".$row['agent']."</td>
<td style='".$td_style."'>".$row['pagent']."</td>
<td style='".$td_style."'>".$row['address1']."</td>
<td style='".$td_style."'>".$row['address2']."</td>
<td style='".$td_style."'>".$row['district']."</td>
<td style='".$td_style."'>".$row['state']."</td>
<td style='".$td_style."'>".$row['priority']."</td>
<td style='".$td_style."'>".$option_text."</td>
<td style='".$td_style."'>".$row['qc_remark']."</td>
</tr>";
$sr_no++;
}
}
else
{
$table_result .="<tr><td colspan='9' align='center'>No Locations Found</td></tr>";
}
$table_result .="</tbody>
</table>";
// echo $table_result; exit;
/* Code to send MAIL */
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "host";
$mail->SMTPAuth = true;
//$mail->SMTPSecure = "ssl";
$mail->Port = 25;
$mail->Username = "Username";
$mail->Password = "Password";
$mail->From = "From";
$mail->FromName = "Name";
$mail->AddAddress("Email");
$mail->AddBCC("Email CC");
$body = "Dear All,<br /><br />For the review conducted on ".date('d-m-Y',strtotime($current_date))." in the following locations we have found deviation in question 1.1 (ie Does this location exist exactly at the address provided?) <br /><br /><br /><br />";
$body .= $table_result;
$body .= "<br /><br />Warm Regards <br /><br />
Note : Please do not reply to this email as this is an automated mail generated
";
//end body
$mail->IsHTML(true);
//$mail->Subject = "Ref : RC1/HR/".strtoupper($pa)."/".date('F Y',(time()-(5*86400)));
$mail->Subject = "Ref : Lrr 1.1 deviation";
$mail->Body = $body;
//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
/* Code to send MAIL Ends */
//mail($to,$subject,$message,$headers);
echo 'Mail sent to '.$pa.'<br /><br />';
/****** END of sending mail **********/
?>
If you want to send mails on a regular or daily basis the ideal way to do that would be to use cron job for sending the mails.By using cron you can specify what application do you want the cron to run on.Also you will be able to specify what time exactly do you want the mail to be sent.
Refer here https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job .
Hope it helps.
I need some Help. I'm new to PHP and the last couple of days trying to solve this issue. I am trying to parse data from my database to a styled HTML Table and I am not able to find any tutorials on this. I did do the tutorial for parsing to a table that is created with PHP. I would like to use the tables I did include in this file. If anyone would be so kind to show me how to do this and also explain it I would be very happy.
This is the PHP file I did try to work with. Only one close I could find from tutorials.
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Navn</th> <th>Årgang</th> <th>NR</th> <th>Navn</th> <th>Navn</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</table>";
?>
This is the stylesheet I would like to use:
/* ------------------
styling for the tables
------------------ */
body
{
line-height: 1.6em;
}
#hor-zebra
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 60px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
#hor-zebra th
{
font-size: 14px;
font-weight: normal;
padding: 10px 8px;
color: #039;
}
#hor-zebra td
{
padding: 8px;
color: #669;
}
#hor-zebra .odd
{
background: #e8edff;
And this is the HTML file where I would like the data from my database to show:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th> //Name off table in DB
<th scope="col">age</th> //Name off table in DB
<th scope="col">issue</th> //Name off table in DB
<th scope="col">Description</th> //Name off table in DB
<th scope="col">quality</th> //Name off table in DB
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr class="odd">
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</tbody>
</table>
</body>
</html>
After adding the code my PHP do return the result. The only problem is it it not showing my styled tables from my style.css and I also get the error "
Notice: Undefined variable: i in C:\Program Files (x86)\EasyPHP-5.3.9\www\Tables\Datamodtagelse.php on line 25
And under that it returns my output: (This is the php page.)
name age issue Description quality
Anders And & Co. 1949 1 Dette er en beskrivelse af en tegneserie. Very Fine.
When I open my html file it doesn't display anything at all.
I will add my file :
Datamodtagelse.php
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo '<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th>
<th scope="col">age</th>
<th scope="col">issue</th>
<th scope="col">Description</th>
<th scope="col">quality</th>
</tr>
</thead>
<tbody>';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if( $i++ % 2 == 0 ) {
$class = " class='odd'";
} else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
Showcomic.html:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>
Style.css
body
{
line-height: 1.6em;
}
#hor-zebra
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
margin: 60px;
width: 480px;
text-align: left;
border-collapse: collapse;
}
#hor-zebra th
{
font-size: 14px;
font-weight: normal;
padding: 10px 8px;
color: #039;
}
#hor-zebra td
{
padding: 8px;
color: #669;
}
#hor-zebra .odd
{
background: #e8edff;
}
My database name is: tegneserier
My table in the database is: årgang
My attributes in the table is:
id int(11) AUTO_INCREMENT
name varchar(255) utf8_danish_ci
age int(11)
issue int(11)
Description text utf8_danish_ci
When looking at the code I think the problem is the HTML file nor importing the stylesheet and the data from the .php file?
The .php file and the .css file and the .html file is located in the same folder.
Any help is welcome.
And sorry this is probably just a easy beginner mistake. (We all need to start somewhere.)
Try something like this:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());
// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang")
or die(mysql_error());
echo '<table id="hor-zebra" summary="Datapass">
<thead>
<tr>
<th scope="col">name</th> //Name off table in DB
<th scope="col">age</th> //Name off table in DB
<th scope="col">issue</th> //Name off table in DB
<th scope="col">Description</th> //Name off table in DB
<th scope="col">quality</th> //Name off table in DB
</tr>
</thead>
<tbody>';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if( $i % 2 == 0 ) {
$class = " class='odd'";
} else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['name'];
echo "</td><td>";
echo $row['age'];
echo "</td><td>";
echo $row['issue'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['quality'];
echo "</td></tr>";
}
echo "</tbody></table>";
?>
And in your HTML file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
#import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>
you are almost there (if the code you showed us runs).
in your php file, you need to give your table the id "hor-zebra", so that the style will be applied to it and the th's and td's within it.
you also want to add a counter to get the .odd thing right:
var $i = 0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr";
if( $i++ % 2 == 0 ) echo(" class='odd'");
echo "><td>";
...
you also might want to set the scope="col" in your th's to match the original table as closely as possible
AND last but not least: Don't forget the elements thead and tbody in your echo-output.
if you do all this, you should see the same table twice in your final html (check the source, ctrl+U)
Remember you can echo your data in div tags too, use
<li></li>
to allow row after row of sql data to get displayed. I mention this because I find the div elements easier to work with than tables.
I've got a project where by I send email using PHP's inbuilt mail() function, I'm only sending one email at a time with a small amount of HTML and very limited CSS (two tables and a little CSS in the head for styling), but the server seems to be doing it really slowly (so much so that the page upon which an admin sends the email frequently times out)
So my question is this; does mail() put a high workload on the server (not sure if that is the right term) or is it just that the server I'm using is rubbish?
Is it worth me looking into projects like http://pear.php.net/package/Mail for this kind of thing?
EDIT:
Here is the code in question:
$query = "SELECT email FROM $a_table WHERE id='$Id'";
$result = mysql_query($query) or die("Query failed: ".mysql_error());
$mail_to = mysql_fetch_row($result);
$mail_to = $mail_to[0];
// multiple recipients
$to = $mail_to;
// subject
$subject = 'notification';
// message
$message = '<html>
<head>
<title>title goes here</title>
<style type="text/css">
table { border: 1px solid #000;}
table tr th { background-color: #d8d8d8; border-bottom: 1px solid #000}
table tr th, table tr td { padding: 4px; text-align: center; }
</style>
</head>
<body>
<h1>header goes here</h1>
<table cellspacing="0">
<tr>
<th>th1</th><td>'.$var.'</td>
</tr>
<tr>
<th>th2</th><td>'.$var2.'</td>
</tr>
<tr>
<th>th3</th><td>'.$var3.'</td>
</tr>
</table>
<p> </p>
<table cellspacing="0">
<tr>
<th colspan="13">Key</th>
</tr>
<tr>
<th>G</th>
<th>I</th>
<th>L</th>
<th>M</th>
<th>N</th>
<th>O</th>
<th>Q</th>
<th>R</th>
<th>S</th>
<th>V</th>
<th>W</th>
<th>C</th>
<th>?</th>
</tr>
<tr>
<td>G</td>
<td>I</td>
<td>L</td>
<td>M</td>
<td>U</td>
<td>O</td>
<td>Q</td>
<td>R</td>
<td>S</td>
<td>V</td>
<td>W</td>
<td>C</td>
<td>R</td>
</tr>
</table>
</body>
</html>
// 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 .= 'From: admin<admin#admin.com>' . "\r\n"; // might need to get rid of this soon
// Mail it
mail($to, $subject, $message, $headers);
}
The mail() function is usually very fast. I've used it in the past for mass email systems and that was processing hundreds of emails per second.
I'd recommend checking how your system is configured to send email. mail() generally utilizes your system's sendmail install (or postfix). You should think about checking the logs to see if there is an problem there.
I have a results page and I would like there to be a bit at the top of the page where it says how many results were returned. How do I do this?
My code is
<?php
if(strlen(trim($_POST['search'])) > 0) {
$search = "%" . $_POST["search"] . "%";
$searchterm = "%" . $_POST["searchterm"] . "%";
mysql_connect ("3", "", "");
mysql_select_db ("");
if (!empty($_POST["search_string"]))
{
}
$query = "SELECT name,location,msg FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'";
$result = mysql_query ($query);
if ($result) {
while ($row = mysql_fetch_array ($result)) { ?>
<center>
<table height="20" width="968" cellpadding="0" cellspacing="0">
<tr>
<td>
<table height="20" width="223" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="helvetica" size="2" color="#045FB4"><?php echo $row[0]; ?></font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
</td>
</tr>
</table>
</td>
<td>
<table height="20" width="745" cellpadding="0" cellspacing="0">
<tr>
<td>
<font face="helvetica" size="2" color="black"><?php echo $row[1]; ?>
<?php echo $row[2]; ?></font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
<td align="right">
<font face="helvetica" size="2" color="red">See More...</font>
<hr size="1" color="#e6e6e6" width="100%"></hr>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
}
}
?>
</center>
THANKS!
James
If you're going to keep it to one page then mysql_num_rows() will do the trick and you're good to go. If you use pagination on the other hand, then your SELECT query will have a LIMIT clause and a second query can be constructed using COUNT(*) on the same tables with the same WHERE clause.
$total_query = "SELECT COUNT(*) FROM contact WHERE name LIKE '%$search%' AND
location LIKE '%$searchterm%'"
I’m not trying to compete with the other fine answers. I’m posting this as an answer because it’s too big for a comment.
Since you asked in a comment what could be done to improve your HTML, I refactored your code a bit just to illustrate some things you could do. I also included mysql_num_rows($result) mentioned by the others for the sake of completion.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
<style type="text/css">
table {
font-family: helvetica;
font-size: small;
width: 968px;
}
td {
border-bottom: 1px solid #e6e6e6;
}
.name {
color:#045FB4;
}
.msg {
color:black;
}
.more {
color:red;
}
</style>
</head>
<body>
<?php
if(strlen(trim($_POST['search'])) > 0):
$search = mysql_real_escape_string($_POST["search"]);
$searchterm = mysql_real_escape_string($_POST["searchterm"]);
mysql_connect ("localhost", "root", "");
mysql_select_db ("sample");
if (!empty($_POST["search_string"]))
{
$search_string = mysql_real_escape_string($_POST["search_string"]);
// more code here
}
$query = "SELECT name,location,msg FROM contact
WHERE name LIKE '%$search%'
AND location LIKE '%$searchterm%'";
$result = mysql_query ($query);
if ($result):
$num_rows = mysql_num_rows($result);
?>
<p>Found <?php echo $num_rows; ?> results.</p>
<table>
<?php
while ($row = mysql_fetch_array ($result)):
?>
<tr>
<td class="name"><?php echo $row['name']; ?></td>
<td class="msg"><?php echo $row['location'], ' ', $row['msg']; ?></td>
<td class="more">See More...</td>
</tr>
<?php
endwhile;
endif;
endif;
?>
</table>
</body>
</html>
Normally, I would put the CSS in a separate file, but this is only an example.
Some things to notice:
There’s only one table
There’s no style information in the HTML
It uses mysql_real_escape_string. Ideally you'd also want to use prepared statements, but I’ll leave that as a personal exercise for you. :)
Even this can be improved quite a bit, but it's a start.
Try using mysql_num_rows($query). I am no PHP expert but, from memory, that should do the trick. Just echo this wherever you want it.
$num_rows = mysql_num_rows($result);
Check out the documentation: http://php.net/manual/en/function.mysql-num-rows.php
Also you can use SQL_CALC_FOUND_ROWS and FOUND_ROWS():
$query = "SELECT SQL_CALC_FOUND_ROWS name,location,msg FROM contact WHERE name LIKE '%$search%' AND location LIKE '%$searchterm%'";
$result = mysql_query($query);
if ($result)
{
$rs_count = mysql_query("SELECT FOUND_ROWS();");
$counted = (int)mysql_result($rs_count, 0);
}