The below code gives the output as how in screenshot. I want to send this content via php mail function . When i try to add this code into $message , It doesnt work as it contains php code(to retrieve data from table) in between html code. Kindly help on how this can be achieved ?
This is the code i was trying out for email .
<?php
include('db.php'); //connection to database
$to = 'example#gmail.com';
$subject = "test php mail" ;
$message = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta charset="utf-8">
<title> Fetching data </title>
</head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:#DC143C;
}
td {
color:#0000FF;
}
</style>
<body>
<font size="4" face="Courier New" >
<table border="1" style="width:50%" align="center">
<tr bgcolor="#2ECCFA">
<th style="padding: 20px" bgcolor="#E6E6FA"
color="#DC143C" color="red">Word of the Day</th>
</tr>
<!-- I used while loop to fetch data and display rows of date on html
table -->
<?php
$records = mysql_query("SELECT * FROM hindiday order by
rand(curdate())
limit 1 ");
while ($course = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' '
.$course['id']." </td>"; "</tr>" ;
}
?>
</table>
</body>
</html> ' ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: abc#gmail.com.com>' . "\r\n";
// Send email
if(mail($to,$subject,$message,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
I would suggest looping outside the string, and the concatenate the result like so :
$string = ''; // The variable used to contain your data
$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
//Filling that variable
$string .= "<tr>";
$string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;
}
//Using the variable in your message
$message = '
...
'. $string .'
...
';
That way wherever you want to add the $string you just concatenate it, as if you try to execute PHP code inside a string it'll be considered as a string and the PHP in question won't be executed.
So the final result would look like this :
<?php
include('db.php'); //connection to database
$to = 'example#gmail.com';
$subject = "test php mail" ;
$string = ''; // The variable used to contain your data
$records = mysql_Something is wrong("SELECT * FROM hindiday order by rand(curdate()) limit 1 ");
while ($course = mysql_fetch_assoc($records)){
//Filling that variable
$string .= "<tr>";
$string .= "<td>".$course['hindiword'] . ' ' .$course['phonetic'] . ' ' .$course['id']." </td>"; "</tr>" ;
}
$message = '
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta charset="utf-8">
<title> Fetching data </title>
</head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th{
color:#DC143C;
}
td {
color:#0000FF;
}
</style>
<body>
<font size="4" face="Courier New" >
<table border="1" style="width:50%" align="center">
<tr bgcolor="#2ECCFA">
<th style="padding: 20px" bgcolor="#E6E6FA"
color="#DC143C" color="red">Word of the Day</th>
</tr>
<!-- I used while loop to fetch data and display rows of date on html
table -->
'. $string .'
</table>
</body>
</html> ' ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: abc#gmail.com.com>' . "\r\n";
// Send email
if(mail($to,$subject,$message,$headers)):
$successMsg = 'Email has sent successfully.';
else:
$errorMsg = 'Email sending fail.';
endif;
?>
I have an online application That is working but I am getting some double database entries. Not every submission creates a double entry but many are. If anyone sees the reason in my code and can tell me It would be appreciated:
<?php
#$upload_Name = $_FILES['Resume']['name'];
#$upload_Size = $_FILES['Resume']['size'];
#$upload_Temp = $_FILES['Resume']['tmp_name'];
#$upload_Mime_Type = $_FILES['Resume']['type'];
function RecursiveMkdir($path)
{
if (!file_exists($path))
{
RecursiveMkdir(dirname($path));
mkdir($path, 0777);
}
}
// Validation
// check only if file
if( $upload_Size > 0)
{
if( $upload_Size == 0)
{
header("Location: error.html");
}
if( $upload_Size >200000)
{
//delete file
unlink($upload_Temp);
header("Location: error.html");
}
if( $upload_Mime_Type != "application/msword" AND $upload_Mime_Type != "application/pdf" AND $upload_Mime_Type != "application/vnd.openxmlformats- officedocument.wordprocessingml.document")
{
unlink($upload_Temp);
header("Location: error.html");
}
}//end wrapper of no file
// Where the file is going to be placed
$target_path = “../../XXXX/uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['Resume']['name']);
if(move_uploaded_file($_FILES['Resume']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['Resume']['name']).
" has been uploaded";
} else{
echo "";
}
?><?php
if(isset($_POST['email'])) {
require_once 'Mail.php'; // PEAR Mail package
require_once 'Mail/mime.php';
$email_to = “name#yoursite.com”; //Enter the email you want to send the form to
$email_subject = "Employment Application"; // You can put whatever subject here
$host = "mail.yourdomain.com"; // The name of your mail server. (Commonly mail.yourdomain.com if your mail is hosted with xxx)
$username = "yoursite.com"; // A valid email address you have setup
$from_address = "name#yoursite.com"; // If your mail is hosted with Site this has to match the email address above
$password = “XXX”; // Password for the above email address
$reply_to = “XXX#yoursite.com"; //Enter the email you want customers to reply to
$port = "50"; // This is the default port. Try port 50 if this port gives you issues and your mail is hosted with Site
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// Validate expected data exists
if(!isset($_POST['Position_Applying']) || !isset($_POST['Position_type']) || !isset($_POST['First_name']) || !isset($_POST['Last_name']) || !isset($_POST['Street']) || !isset($_POST['City']) || !isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$hdw_id = $_POST['hdw_id'];
$hdw_Country = $_POST['hdw_Country'];
$hdw_IP = $_POST['hdw_IP'];
$hdw_Referer = $_POST['hdw_Referer'];
$hdw_ServerTime = $_POST['hdw_ServerTime'];
$hdw_Browser = $_POST['hdw_Browser'];
$hdw_UserAgent = $_POST['hdw_UserAgent'];
$Position_Applying = $_POST['Position_Applying'];
$Position_one = $_POST['Position_one'];
$Position_two = $_POST['Position_two'];
$Position_three = $_POST['Position_three'];
$Position_type = $_POST['Position_type'];
$Shift_type = $_POST['Shift_type'];
$First_name =$_POST['First_name'];
$Middle_name = $_POST['Middle_name'];
$Last_name = $_POST['Last_name'];
$Street = $_POST['Street'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$One_Phone = $_POST['One_Phone'];
$crlf = "n";
// required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid. <br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$First_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Employment Application Details Below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Position Applying: ".clean_string($Position_Applying)."\n";
$email_message .= "Position Type: ".clean_string($Position_type)."\n";
$email_message .= "\n";
$email_message .= "First name: ".clean_string($First_name)."\n";
$email_message .= "Last Name: ".clean_string($Last_name)."\n";
$email_message .= "\n";
$email_message .= "Street: ".clean_string($Street)."\n";
$email_message .= "City: ".clean_string($City)."\n";
$email_message .= "State: ".clean_string($State)."\n";
$email_message .= "email: ".clean_string($email)."\n";
$email_message .= "Phone: ".clean_string($One_Phone)."\n";
$email_message .= "\n";
$email_message .= "Referred By: ".clean_string($Referred_by )."\n";
$email_message .= "Older than 18: ".clean_string($eighteen )."\n";
$email_message .= "US Citizen: ".clean_string($US_citizen)."\n";
$email_message .= "Crime Conviction: ".clean_string($Crime_convict)."\n";
$email_message .= "NYS Professional License: ".clean_string($NYS_professional_lic)."\n";
$email_message .= "Other License: ".clean_string($Other_professional_lic)."\n";
$email_message .= "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($Resume,'application/pdf');
// This section creates the email headers
$auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password);
$headers = array('From' => $from_address, 'To' => $email_to, 'Subject' => $email_subject, 'Reply-To' => $reply_to);
// This section send the email
$smtp = Mail::factory('smtp', $auth);
$mail = $smtp->send($email_to, $headers, $email_message);
// This section creates the email headers
$auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password);
$headers = array('From' => $from_address, 'To' => $email, 'Subject' => $email_subject, 'Reply-To' => $reply_to);
// This section send the email
$smtp = Mail::factory('smtp', $auth);
$mail = $smtp->send($email, $headers, $email_message);
if (PEAR::isError($mail)) {?>
<!-- include your own failure message html here -->
Unfortunately, the message could not be sent at this time. Please try again later.
<!-- Uncomment the line below to see errors with sending the message -->
<!-- <?php echo("<p>". $mail->getMessage()."</p>"); ?> -->
<?php } else { ?>
<!-- include your own success message html here -->
<?php } } ?>
<style type="text/css">
<!--
.style2 {font-size: 14px}
.style3 { font-size: 14px;
font-family: Verdana;
}
-->
</style>
<link href=“XXXDatabaseB/js.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body {
background-image: url(XXXDatabaseB/images/green100px.jpg);
background-color: #FFF09F;
}
.style4 {color: #A20246}
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #FFFFFF;
font-weight: bold;
padding: 10px;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #FFFFFF;
}
a:hover {
text-decoration: underline;
color: #FFF09F;
}
a:active {
text-decoration: none;
color: #FFFFFF;
}
.style5 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: small;
color: #FFFFFF;
}
.style6 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #FFFFFF;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- ImageReady Slices (xxx_square_slice.psd) -->
<table width="830" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" id="Table_01">
<tr valign="top">
<td height="258" colspan="2"><?php include 'header.php'; ?></td>
</tr>
<tr>
<td width="100%" valign="top">
<?php
$host = “xxxxx.net";
$username = “xxxxx”;
$password = “xxxxx”;
$dbname = “xxxxxx”;
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$hdw_id = $_POST['hdw_id'];
$hdw_Country = $_POST['hdw_Country'];
$hdw_IP = $_POST['hdw_IP'];
$hdw_Referer = $_POST['hdw_Referer'];
$hdw_Browser = $_POST['hdw_Browser'];
$hdw_UserAgent = $_POST['hdw_UserAgent'];
$Position_Applying = $_POST['Position_Applying'];
$Position_one = $_POST['Position_one'];
$Position_two = $_POST['Position_two'];
$Position_three = $_POST['Position_three'];
$Position_type = $_POST['Position_type'];
$Shift_type = $_POST['Shift_type'];
$First_name =$_POST['First_name'];
$sql = "INSERT INTO `new_app`(`hdw_id`, `hdw_Country`, `hdw_IP`, `hdw_Referer`, `hdw_ServerTime`, `hdw_Browser`, `hdw_UserAgent`, `Position_Applying`, `Position_one`, `Position_two`, `Position_three`, `Position_type`, `Shift_type`, `First_name`)
VALUES ('$hdw_id', '$hdw_Country', '$hdw_IP', '$hdw_Referer', CURRENT_TIMESTAMP, '$hdw_Browser', '$hdw_UserAgent', '$Position_Applying', '$Position_one', '$Position_two', '$Position_three', '$Position_type', '$Shift_type', '$First_name')";
$result=mysql_query($sql);
if($result){
echo "";
}
else {
echo "ERROR";
}
mysql_close();
?>
<table width="100%" height="508" border="0" align="left" cellpadding="20" cellspacing="5">
<tbody>
<tr>
<td colspan="2" valign="middle">
<h2>Thank you for your Application!</h2>
</p>
</td></tr></tbody></table></td>
</tr>
<tr>
<td colspan="2" valign="top"><?php include 'footer.php'; ?></td>
</tr>
</table>
<!-- End ImageReady Slices -->
</body>
</html>
The code looks ok to me the insert is not executed twice and is no where near a loop of any kind. So it makes me wonder that maybe its user error. I would look at how the file upload section seems to allow the rest of the code to run even if it fails. If it fails the user will change something with the file and resubmit. This happens because you need to exit; after your headers.
I also like to end my insert statements with a LIMIT 1 just because it is limited to one - but I doubt that the problem.
I have made the changes I suggested in the code below:
<?php
#$upload_Name = $_FILES['Resume']['name'];
#$upload_Size = $_FILES['Resume']['size'];
#$upload_Temp = $_FILES['Resume']['tmp_name'];
#$upload_Mime_Type = $_FILES['Resume']['type'];
function RecursiveMkdir($path)
{
if (!file_exists($path))
{
RecursiveMkdir(dirname($path));
mkdir($path, 0777);
}
}
// Validation
// check only if file
if( $upload_Size > 0)
{
if( $upload_Size == 0)
{
header("Location: error.html");
exit;
}
if( $upload_Size >200000)
{
//delete file
unlink($upload_Temp);
header("Location: error.html");
exit;
}
if( $upload_Mime_Type != "application/msword" AND $upload_Mime_Type != "application/pdf" AND $upload_Mime_Type != "application/vnd.openxmlformats- officedocument.wordprocessingml.document")
{
unlink($upload_Temp);
header("Location: error.html");
exit;
}
}//end wrapper of no file
// Where the file is going to be placed
$target_path = “../../XXXX/uploads/";
/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['Resume']['name']);
if(move_uploaded_file($_FILES['Resume']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['Resume']['name']).
" has been uploaded";
} else{
echo "";
}
?><?php
if(isset($_POST['email'])) {
require_once 'Mail.php'; // PEAR Mail package
require_once 'Mail/mime.php';
$email_to = “name#yoursite.com”; //Enter the email you want to send the form to
$email_subject = "Employment Application"; // You can put whatever subject here
$host = "mail.yourdomain.com"; // The name of your mail server. (Commonly mail.yourdomain.com if your mail is hosted with xxx)
$username = "yoursite.com"; // A valid email address you have setup
$from_address = "name#yoursite.com"; // If your mail is hosted with Site this has to match the email address above
$password = “XXX”; // Password for the above email address
$reply_to = “XXX#yoursite.com"; //Enter the email you want customers to reply to
$port = "50"; // This is the default port. Try port 50 if this port gives you issues and your mail is hosted with Site
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// Validate expected data exists
if(!isset($_POST['Position_Applying']) || !isset($_POST['Position_type']) || !isset($_POST['First_name']) || !isset($_POST['Last_name']) || !isset($_POST['Street']) || !isset($_POST['City']) || !isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$hdw_id = $_POST['hdw_id'];
$hdw_Country = $_POST['hdw_Country'];
$hdw_IP = $_POST['hdw_IP'];
$hdw_Referer = $_POST['hdw_Referer'];
$hdw_ServerTime = $_POST['hdw_ServerTime'];
$hdw_Browser = $_POST['hdw_Browser'];
$hdw_UserAgent = $_POST['hdw_UserAgent'];
$Position_Applying = $_POST['Position_Applying'];
$Position_one = $_POST['Position_one'];
$Position_two = $_POST['Position_two'];
$Position_three = $_POST['Position_three'];
$Position_type = $_POST['Position_type'];
$Shift_type = $_POST['Shift_type'];
$First_name =$_POST['First_name'];
$Middle_name = $_POST['Middle_name'];
$Last_name = $_POST['Last_name'];
$Street = $_POST['Street'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$One_Phone = $_POST['One_Phone'];
$crlf = "n";
// required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid. <br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$First_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Employment Application Details Below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Position Applying: ".clean_string($Position_Applying)."\n";
$email_message .= "Position Type: ".clean_string($Position_type)."\n";
$email_message .= "\n";
$email_message .= "First name: ".clean_string($First_name)."\n";
$email_message .= "Last Name: ".clean_string($Last_name)."\n";
$email_message .= "\n";
$email_message .= "Street: ".clean_string($Street)."\n";
$email_message .= "City: ".clean_string($City)."\n";
$email_message .= "State: ".clean_string($State)."\n";
$email_message .= "email: ".clean_string($email)."\n";
$email_message .= "Phone: ".clean_string($One_Phone)."\n";
$email_message .= "\n";
$email_message .= "Referred By: ".clean_string($Referred_by )."\n";
$email_message .= "Older than 18: ".clean_string($eighteen )."\n";
$email_message .= "US Citizen: ".clean_string($US_citizen)."\n";
$email_message .= "Crime Conviction: ".clean_string($Crime_convict)."\n";
$email_message .= "NYS Professional License: ".clean_string($NYS_professional_lic)."\n";
$email_message .= "Other License: ".clean_string($Other_professional_lic)."\n";
$email_message .= "\n";
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($Resume,'application/pdf');
// This section creates the email headers
$auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password);
$headers = array('From' => $from_address, 'To' => $email_to, 'Subject' => $email_subject, 'Reply-To' => $reply_to);
// This section send the email
$smtp = Mail::factory('smtp', $auth);
$mail = $smtp->send($email_to, $headers, $email_message);
// This section creates the email headers
$auth = array('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password);
$headers = array('From' => $from_address, 'To' => $email, 'Subject' => $email_subject, 'Reply-To' => $reply_to);
// This section send the email
$smtp = Mail::factory('smtp', $auth);
$mail = $smtp->send($email, $headers, $email_message);
if (PEAR::isError($mail)) {?>
<!-- include your own failure message html here -->
Unfortunately, the message could not be sent at this time. Please try again later.
<!-- Uncomment the line below to see errors with sending the message -->
<!-- <?php echo("<p>". $mail->getMessage()."</p>"); ?> -->
<?php } else { ?>
<!-- include your own success message html here -->
<?php } } ?>
<style type="text/css">
<!--
.style2 {font-size: 14px}
.style3 { font-size: 14px;
font-family: Verdana;
}
-->
</style>
<link href=“XXXDatabaseB/js.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
body {
background-image: url(XXXDatabaseB/images/green100px.jpg);
background-color: #FFF09F;
}
.style4 {color: #A20246}
a {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 12px;
color: #FFFFFF;
font-weight: bold;
padding: 10px;
}
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
color: #FFFFFF;
}
a:hover {
text-decoration: underline;
color: #FFF09F;
}
a:active {
text-decoration: none;
color: #FFFFFF;
}
.style5 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: small;
color: #FFFFFF;
}
.style6 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
color: #FFFFFF;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<!-- ImageReady Slices (xxx_square_slice.psd) -->
<table width="830" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" id="Table_01">
<tr valign="top">
<td height="258" colspan="2"><?php include 'header.php'; ?></td>
</tr>
<tr>
<td width="100%" valign="top">
<?php
$host = “xxxxx.net";
$username = “xxxxx”;
$password = “xxxxx”;
$dbname = “xxxxxx”;
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$hdw_id = $_POST['hdw_id'];
$hdw_Country = $_POST['hdw_Country'];
$hdw_IP = $_POST['hdw_IP'];
$hdw_Referer = $_POST['hdw_Referer'];
$hdw_Browser = $_POST['hdw_Browser'];
$hdw_UserAgent = $_POST['hdw_UserAgent'];
$Position_Applying = $_POST['Position_Applying'];
$Position_one = $_POST['Position_one'];
$Position_two = $_POST['Position_two'];
$Position_three = $_POST['Position_three'];
$Position_type = $_POST['Position_type'];
$Shift_type = $_POST['Shift_type'];
$First_name =$_POST['First_name'];
$sql = "INSERT INTO `new_app`(`hdw_id`, `hdw_Country`, `hdw_IP`, `hdw_Referer`, `hdw_ServerTime`, `hdw_Browser`, `hdw_UserAgent`, `Position_Applying`, `Position_one`, `Position_two`, `Position_three`, `Position_type`, `Shift_type`, `First_name`)
VALUES ('$hdw_id', '$hdw_Country', '$hdw_IP', '$hdw_Referer', CURRENT_TIMESTAMP, '$hdw_Browser', '$hdw_UserAgent', '$Position_Applying', '$Position_one', '$Position_two', '$Position_three', '$Position_type', '$Shift_type', '$First_name') LIMIT 1";
$result=mysql_query($sql);
if($result){
echo "";
}
else {
echo "ERROR";
}
mysql_close();
?>
<table width="100%" height="508" border="0" align="left" cellpadding="20" cellspacing="5">
<tbody>
<tr>
<td colspan="2" valign="middle">
<h2>Thank you for your Application!</h2>
</p>
</td></tr></tbody></table></td>
</tr>
<tr>
<td colspan="2" valign="top"><?php include 'footer.php'; ?></td>
</tr>
</table>
<!-- End ImageReady Slices -->
</body>
</html>
Trying to build contact page. I would like to be able to send multiple emails in one time. email addresses have been pulled in from database. I am managing getting them all with do while. However to add "bcc" all addresses I need them to be in one variable for example $emails. I cannot seem to figure out how to do that. This is what I have so far:
$username = $_POST['Username'];
$email = $_POST['email'];
$to="email goes here";
$from= $_POST['email'];
$subject= "Raiding Team Announcement";
$footer="© Copyright 2015 All Rights Reserved ";
$message .= '<html><body>';
$message .= '<div style="width:100%; background-color:#333;">';
$message .= '<h1 style="font-size:50px; color:#FFCC00; text- align:center; display:block; padding-bottom:15px; border-bottom:1px solid #AA0114; ">HellscreamsFury</h1>';
$message .='<h2 style="font-size:32px;color:#f37e0e; text-align:center;">' .$_POST["Username"].' says:</h2>';
$message .='<div style="margin:30px; padding:10px; border:1px solid #404040; margin-bottom:50px;">';
$message .='<p style="font-size:18px; color:#ccc;">' .$_POST["message"]. '</p>';
$message .= '</div>';
$message .= '<div style="border-top:1px solid #AA0114;">';
$message .='<p style="font-size:12px; color:#fff; text-align:center; padding: 20px 0 50px 0;">' .$footer. '</p>';
$message .= '</div>';
$message .= '</div>';
$message .= '</body></html>';
$headers .= "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail($to,$subject,$message,$headers)){
header("Location: ../announcments.php?aid=43");
}
else{
header("Location: ../announcments.php?aid=44");
}
This snippet pulls in my emails:
<?php do { ?>
<?php echo $row_rs_contact_team['email']; ?>
<?php } while ($row_rs_contact_team = mysql_fetch_assoc($rs_contact_team)); ?>
I would need them to sit in one variable - let's say $emails. How do I do that? Also emails are pulled in while checking user_id, but sometimes user_id repeats - is there a way to pull that email in once instead of pulling let's say three times?
Do can do it by iterating it into a while loop and concat it and adding a comma near to it.
$CountingQuery = "SELECT * FROM users";
$ExecutingCountingQuery = $Connection->query($CountingQuery);
$Bcc = ''; // Declaring a Variable
while($row = $ExecutingCountingQuery->fetch_array())
{
$Bcc .=$row['email'].',';
}
echo $Bcc; // Now $Bcc is your expected output
//The output will be something like
//sa#sysaxiom.com,test#sysaxiom.com,demo#sysaxiom.com,admin#sysaxiom.com,
Note :
I don't know what name you use to store the email coloumn. So i used the name email for the email column.
Also I have used * where you can replace it for your need.
This question already has answers here:
Send HTML in email via PHP
(8 answers)
Closed 7 years ago.
I am setting up a confirmation email script for our customers to receive an email when they complete the online form. I uploaded my script and tested but I received an email with just the raw HTML. I have been looking at some tutorials trying to do this as well. I validated my HTML and I passed all the tests. Is there something that I have to include in my PHP for my body string to be interpreted as HTML?
Here is my script
<?php
//Script for sending a confirmation email
$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>Demystifying Email Design</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>
</head>
<body style=\"margin: 0; padding: 0; background-color: #ecf0f1;\">
<table cellpadding=\"0\" cellspacing=\"0\" width=\"60%\" align=\"center\" style=\"border: 1px solid #bdc3c7;\">
<tr bgcolor=\"#3498db\">
<td align=\"center\">
<img alt=\"Logo\" src=\"http://www.****.com/logo-lg.jpg\" style=\"width: 50%; height: auto; padding-top: 5%; padding-bottom: 5%;\" />
</td>
</tr>
<tr bgcolor=\"#ffffff\" align=\"center\">
<td>
<h3 style=\"padding-top: 5%; padding-bottom: 5%;\">Worldwide innovator in flexible liquid packaging</h3>
</td>
</tr>
<tr align=\"center\" bgcolor=\"#ffffff\">
<td>
<h4 style=\"padding-top: 5%; padding-bottom: 5%;\">Thank you for contacting customer service. We've received your sample request; one of our team members will be in contact with you in the very near future. Thanks again for your time and interest.</h4>
</td>
</tr>
</table>
</body>
</html>";
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
if (mail($to,$subject,$body)){
echo "Mail was sent";
} else{
echo "Failed";
}
This is the email I received
You need to also set a header indicating this is HTML, e.g.
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <webmaster#example.com>' . "\r\n";
$headers .= 'Cc: myboss#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
Source (see Example 3).
Correct code will be:
$to = "*******#****.com";
$subject = "Thanks for Reaching out";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
if (mail($to,$subject,$body,$headers)){
echo "Mail was sent";
}
How can I make the user stay on the same page if the required fields in the form aren't filled? Any answer would be greatly appreciated.
<?php
header('Location: COMING SOON.html');
$name = $_POST['name'];
$surname = $_POST['last_name'];
$cons = $_POST['cons'];
$password = $_POST['pass'];
$email = $_POST['email'];
$message = $_POST['message'];
$recipient = "pixiedustmed#yahoo.com";
$to = 'pixiedustmed#yahoo.com';
$subject = "Contact Message from: $name $surname - $email";
$headers = "From: $name $surname \r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "CC: avedis#avedis.ga\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<div class="about-center"><h1 style="font-family: Lato-Light; font-size: 2em">You have received a contact message from Your website.</h1></div>';
$message .= '<table rules="all" style="border-color: #666; font-family: Segoe UI; font-size: 16px;" cellpadding="12">';
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Name: </p> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr style='background: #eee;'><td><p style='color: #2c3e50; font-weight:bold'>Surname: </p> </td><td>" . strip_tags($_POST['last_name']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Email: </p> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr style='background: #71bdf4;'><td><p style='color: #ffffff'>Message: </p> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "<tr style='background: #34495e;'><td><p style='color: #ffffff'>Consultation Info: </p> </td><td style='color: #ffffff'>" . strip_tags($_POST['cons']) . "</td></tr>";
$message .= "<tr><td><p style='color: #2c3e50; font-weight:bold'>Password: </p> </td><td>" . strip_tags($_POST['pass']) . "</td></tr>";
$message .= "</table>";
$message .= '<br><h1 style="font-family: Lato-Light; font-size: 14px; color:#c0392b;">Click here to open avedis.ga</h1>';
$message .= '<h1 style="font-family: Segoe UI; font-size: 13px; color:#eee;">This is an autogenerated message designed by S.G.</h1>';
$message .= '<div class="about-center">-</div>';
$message .= "</body></html>";
mail($recipient, $subject, $message, $headers, $password) or die("Error!");
echo "Thank You!";
?>
So, as you see, I have put header('Location: COMING SOON.html'); which evantually works.
Plus, in my css, I have put a required="" field which didn't let the user click the submit button and displayed a message. But now that I put a header, the PHP disregards everything.
My site can be accessed by clicking here; scroll down to the end of the page and try in the contact form, and. Let me know what you think about the design and all!
if(empty($name) || empty($surname) || empty($cons) || empty($password) || empty($email) || empty($_POST['email']) || empty($message)){
// do something if a field is empty
} else {
// do something if fields are all filled
}
Put this after you declare the variables like $name, $surname, and $cons.
Use some javascript to ensure that the user has filled all the fields, or just include the php inside the index page and if the user has filled all the forms send the mail, otherwise show an error around the unfilled fields.
Example using jQuery: Check all input fields have been filled out with jQuery
You should check and validate the feilds first and if the feilds are correctly submitted, then only u should redirect the user using the header().
ps: what you asked was actually not clear.
Hope it helps thankz!