Making a conditional statement to send email alert - php

I'm trying to make a conditional statement to stop an email alert when the fail.php is called. Right now I'm getting an email alert for both good and fail results.
I do not want to receive an email if the the result failed. Should I make two scripts or is there a way t make this work together?
Thanks
Here is the section I'm referring to along with the whole script.
if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);
$reg = $_REQUEST['reg'] ;
$first_name = $_REQUEST['first_name'];
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name));
}
else {
header("location: reg_add_fail.php");
exit(); // as sugested by John Conde
}
<?
$to = 'newreg#41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>
<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
<tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";
mail($to, $subject, $msg, $headers);
date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ;
$order = "INSERT INTO reg_add (submit_date,
connect_date,
reg,
first_name,
)
VALUES
('$submit_date',
'$_POST[connect_date]',
'{$_POST[reg]}nv',
'$_POST[first_name]')";
$result = mysql_query($order);
if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);
$reg = $_REQUEST['reg'] ;
$first_name = $_REQUEST['first_name'];
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name));
}
else {
header("location: reg_add_fail.php");
exit(); // as sugested by John Conde
}
?>

Remove the first instance of mail($to, $subject, $msg, $headers);.
Then, for good measure, check the number of rows affected, rather than true/false (although both should work).
if (mysql_affected_rows($result) > 0) {
}

If you check your code
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Homeless' . "\r\n";
mail($to, $subject, $msg, $headers);
date_default_timezone_set('America/Los_Angeles');
This code is already sending the mail regard less of the result.
You just need to remove this line from top code
mail($to, $subject, $msg, $headers);
and your code will work fine.

Final code, kindly test ite
<?
$to = 'newreg#41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>
<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
<tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";
date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ;
$order = "INSERT INTO reg_add (submit_date,
connect_date,
reg,
first_name,
)
VALUES
('$submit_date',
'$_POST[connect_date]',
'{$_POST[reg]}nv',
'$_POST[first_name]')";
$result = mysql_query($order);
if (mysql_affected_rows($result) > 0) {
mail($to, $subject, $msg, $headers);
$reg = $_REQUEST['reg'] ;
$first_name = $_REQUEST['first_name'];
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name));
}
else {
header("location: reg_add_fail.php");
exit(); // as sugested by John Conde
}
?>

I currently see some sql injections plus invalid query first_name, extra , at the end, constants used in posts array keys, a mix of request and post, large html block of code and no checks on validity of the values passed.
If you check for valid values then you can determine if the script should continue to the mail and update the database parts:
Heres a clean up of your code hope it helps:
<?php
$to = 'newreg#41q.org';
$subject = 'New Homeless Connection';
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['first_name']) && strlen($_POST['first_name'])>1){
$first_name=$_POST['first_name'];
}
if(isset($_POST['reg']) && strlen($_POST['reg'])>1){
$reg=$_POST['reg'];
}
if(isset($_POST['connect_date']) && strlen($_POST['connect_date'])>1){
$connect_date=$_POST['connect_date'];
}
if(!isset($first_name) || !isset($reg) || !isset($connect_date)){
header("location: reg_add_fail.php");
exit();
}
}else{
//the page the post from
header("location: reg_form.php");
exit();
}
$msg=<<<EMAIL
<html>
<head>
<title>New Homeless Connection</title>
</head>
<body>
<table cellspacing="0" cellpadding="10" border="1" align="left">
<tr>
<td align="left" width="150px">Registery No.:</td>
<td align="left">$reg</td>
</tr>
<tr>
<td align="left">First Name:</td>
<td align="left">$first_name </td>
</tr>
<tr>
<td align="left">Connection Date:</td>
<td align="left">$connect_date</td>
</tr>
<tr>
<td align="left" colspan="2">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
EMAIL;
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";
mail($to, $subject, $msg, $headers);
date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ;
$order = "INSERT INTO reg_add (submit_date,connect_date, reg, first_name)
VALUES ('{$submit_date}',".mysql_real_escape_string($connect_date)."','".mysql_real_escape_string($reg)."nv','".mysql_real_escape_string($first_name)."')";
$result = mysql_query($order);
header("Location: ./reg_add_success.php?reg=".urlencode($reg)."&first_name=".urlencode($first_name));
die;
?>

Related

How to send mail using php by insert html into mail content? [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
How to send mail using php by insert html into mail content ?
I tried to insert html code inner $message, When i test it's show error
like this Parse error: syntax error, unexpected 'margin' (T_STRING)
How can i do ?
<?PHP
include("connect.php");
$email = "test_mail#hotmail.com";
$to = $email;
$subject = "test subject";
$message = "
<body style="margin: 0; padding: 0;">
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<img src="http://i.stack.imgur.com/Jy9QUm.jpg"/>
</td>
</tr>
<tr>
<td>
test text
</td>
</tr>
</table>
</body>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>
You have an issue with wrapping your $body string try this
<?PHP
include("connect.php");
$email = "test_mail#hotmail.com";
$to = $email;
$subject = "test subject";
$message = "
<body style='margin: 0; padding: 0;'>
<table border='1' cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td>
<img src='http://i.stack.imgur.com/Jy9QUm.jpg'/>
</td>
</tr>
<tr>
<td>
test text
</td>
</tr>
</table>
</body>
";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>

PHP Mail message from database

I want the email message which contain the order details of customers.This code is ok for only one order but I don't know how to send message if customer select more than one products.
Here is my code :
include('connect.php');
if(!isset($_SESSION["id"]))
{
header("location:order_login.php");
}
$id=$_SESSION["id"];
$emp_name=$_SESSION['name'];
$result=$mysqli->query("select order_id from orders where user_id='$id' ORDER BY order_id desc limit 1");
$obj=$result->fetch_object();
$order_id=$obj->order_id;
$results=$mysqli->query("select * from order_details where order_id=$order_id");
while($obj=$results->fetch_object())
{
$product_name=$obj->product_name;
$product_code=$obj->product_code;
$qty=$obj->qty;
$subtotal=$obj->sub_total;
}
$to="demo#gmail.com";
$bcc_mail="demo#gmail.com";
$subject= "Order Confirmation";
$message ='
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td>Product Code</td>
<td>Quantity</td>
<td>Sub Total</td>
</tr>
<tr>
<td>'.$product_name.'</td>
<td>'.$product_code.'</td>
<td>'.$qty.'</td>
<td>'.$subtotal.'</td>
</tr>
</table>';
echo $message;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: test#gmail.com'."\r\n";
$headers .= "Bcc: $bcc_mail\r\n";
mail($to, $subject, $message, $headers);
unset($_SESSION['products']);
?>
please check my code thanks.
You should create a new tr section for each product:
include('connect.php');
if(!isset($_SESSION["id"]))
{
header("location:order_login.php");
}
$id=$_SESSION["id"];
$emp_name=$_SESSION['name'];
$result=$mysqli->query("select order_id from orders where user_id='$id' ORDER BY order_id desc limit 1");
$obj=$result->fetch_object();
$order_id=$obj->order_id;
$to="demo#gmail.com";
$bcc_mail="demo#gmail.com";
$subject= "Order Confirmation";
$message ='
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td>Product Code</td>
<td>Quantity</td>
<td>Sub Total</td>
</tr>';
$results=$mysqli->query("select * from order_details where order_id=$order_id");
while($obj=$results->fetch_object())
{
$product_name=$obj->product_name;
$product_code=$obj->product_code;
$qty=$obj->qty;
$subtotal=$obj->sub_total;
$message .='
<tr>
<td>'.$product_name.'</td>
<td>'.$product_code.'</td>
<td>'.$qty.'</td>
<td>'.$subtotal.'</td>
</tr>';
}
$message .='
</table>';
echo $message;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: test#gmail.com'."\r\n";
$headers .= "Bcc: $bcc_mail\r\n";
mail($to, $subject, $message, $headers);
unset($_SESSION['products']);
?>
Please try this code hope this helps:
$result=$mysqli->query("select order_id from orders where user_id='$id' ORDER BY order_id desc limit 1");
$obj=$result->fetch_object();
$order_id=$obj->order_id;
$results=$mysqli->query("select * from order_details where order_id=$order_id");
$to="demo#gmail.com";
$bcc_mail="demo#gmail.com";
$subject= "Order Confirmation";
$message .='<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td>Product Code</td>
<td>Quantity</td>
<td>Sub Total</td>
</tr>';
while($obj=$results->fetch_object())
{
$product_name=$obj->product_name;
$product_code=$obj->product_code;
$qty=$obj->qty;
$subtotal=$obj->sub_total;
$message .= '
<tr>
<td>'.$product_name.'</td>
<td>'.$product_code.'</td>
<td>'.$qty.'</td>
<td>'.$subtotal.'</td>
</tr>';
}
$message .= '</table>';
echo $message;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: test#gmail.com'."\r\n";
$headers .= "Bcc: $bcc_mail\r\n";
mail($to, $subject, $message, $headers);
unset($_SESSION['products']);
?>
What I did here is just looping the tr which contains data from the db. I haven't tried it though but sure it'll work. Enjoy!

php email from name header based on user name input

What am I doing wrong here. When a user submits the form, it shows up in my inbox with their name as their email address and I would like it to be the name that they input in the form. here is my code.
<?php
//error_reporting(0);
//include("connection.php");
$n=$_POST['name'];
$e=$_POST['email'];
$p=$_POST['phone'];
$t1=$_POST['tot_eth'] ;
$t2=$_POST['tot_tax'];
$t3=$_POST['tot_acct'];
$t4=$_POST['tot_tot'];
$i=1;
while($i<13)
{
${'date' . $i} = $_POST["date$i"] ;
${'seminar'.$i}=$_POST["seminar$i"] ;
${'sponser'.$i}=$_POST["sponser$i"] ;
${'ethics'.$i}=$_POST["ethics$i"] ;
${'tax'.$i}=$_POST["tax$i"] ;
${'acct'.$i}=$_POST["acct$i"] ;
${'t'.$i}=$_POST["total$i"] ;
$i++;
}
//session_start();
$to = "mj#ntatax.com";
$from=$n $e;
$subject = "2015 CPE Verification Form";
// compose headers
$headers = 'From: '. $_POST['name'] . ' <' . $e . '>\r\n' .
'Reply-To:' .$from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// compose message
$message ='<html>
<body>
<p>
From Name : '.$n.'<br>
From Email : '.$e.'<br>
Phone Number : '.$p.'<br><br>
</p>
<table cellpadding="5"> <tr>
<td>Date </td>
<td>Seminar Attended and Location </td>
<td>Sponsor </td>
<td>Ethics Hours </td>
<td>Tax Hours </td>
<td>Acct Hours</td>
<td>Total Hours</td>
</tr>
';
for ($i=1;$i<=13;$i++){
$message .='<tr>
<td>'.${'date' . $i}.'</td>
<td>'.${'seminar'.$i}.'</td>
<td>'.${'sponser'.$i}.'</td>
<td>'.${'ethics'.$i}.'</td>
<td>'.${'tax'.$i}.'</td>
<td>'.${'acct'.$i}.'</td>
<td>'.${'t'.$i}.'</td>
</tr>';
}
$message .=' <tr></tr>
<tr>
<td colspan=2>
<td><p style="text-align: right">Total</p></td>
<td>'.$_POST['tot_eth'].'</td>
<td>'.$_POST['tot_tax'].'</td>
<td >'.$_POST['tot_acct'].'</td>
<td >'.$_POST['tot_tot'].'</td> </td>
</tr> </table>
</body>
</html>';
$message = wordwrap($message, 270);
// send email
mail($to,$subject, $message, $headers,"'".'-f '.$from."'");
// mail($to,$subject, $message, $headers,"'".'-f '.$from."'");
session_start();
//$_SESSION['send1']="Thanks For Contacting Us,Your Query Will Be Replied At the Earliest.";
header("location:http://www.waainc.org");
?>

How to send values from html form to mail in table PHP

This what I have I tried to put it in a table just like:
<table>
<tr><td>$_POST['onderwerp']</td></tr>
</table>
This is what I have it sends the mail but it's to messy:
<?php
$to = 'example#gmail.com';
$subject = 'Vraag via de website';
$message = 'Onderwerp:'. $_POST['onderwerp'].'<br /><br />'.$_POST['vraag'].'<br /><br />'.'Telefoonummer:'. $_POST['tel'].'<br /><br />'.'Email:'. $_POST['email'] ;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:<eexample#gmail.com>' . "\r\n";
$headers .= 'The shop<example#gmail.com>' . "\r\n";
mail($to, $subject, $message, $headers);
header('Location: contact.html');
?>
I just want to send the variables in a table so that I don't have to search through all the text.
<?php
$to = 'user#example.com';
$subject = 'Vraag via de website';
$msg = "<html>
<head>
<title>Title of email</title>
</head>
<body>
<table cellspacing=\"4\" cellpadding=\"4\" border=\"1\" align=\"center\">
<tr>
<td align=\"center\">Onderwerp</td>
<td align=\"center\"> vraag</td>
<td align=\"center\">Telefoonummer</td>
<td align=\"center\">Email</td>
</tr>
<tr>
<td align=\"center\">".$_POST['onderwerp']."</td>
<td align=\"center\">".$_POST['vraag']."</td>
<td align=\"center\">".$_POST['tel']."</td>
<td align=\"center\">".$_POST['email']."</td>
</tr>
</table>
</body>
</html>";
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Site Name <me#mysite.com>' . "\r\n";
mail($to, $subject, $msg, $headers);
?>
you could try this using your variables.
$var = 'test';
$var2 = 'test2';
echo '<table border="1">';
echo '<tr><td>' . $var . '</td></tr>';
echo '<tr><td>' . $var . '</td></tr>';
echo '</table>';
this will show the variables in a table, then you can edit the table using css how you want. :)
I suggest that you include swiftmailer.
Swiftmailer makes sure emails get delivered in the Inbox and you can easily include HTML markup in your emails:
Swiftmailer HTML in email
Just download the Swiftmailer Library, include and configure it like this example:
Sending an email in swiftmailer
Let me know if this helps you out!

Trying to stop email alert on FAIL inside PHP script when a fail occurs.

I just discovered that I'm still receiving an email EVEN when there is an error (reg_add_fail.php). Is it possible to stop the script from emailing me IF the client is directed to reg_add_fail.php? Confused...
I simplified the script to condense.
Many thanks.
Erik
<?
$to = 'newreg#41q.org';
$subject = 'New Homeless Connection';
$msg = "<html>
<head>
<title>New Homeless Connection</title>
</head>
<body>
<table cellspacing=\"0\" cellpadding=\"10\" border=\"1\" align=\"left\">
<tr>
<td align=\"left\" width=\"150px\">Registery No.:</td>
<td align=\"left\"> $reg</td>
</tr>
<tr>
<td align=\"left\">First Name:</td>
<td align=\"left\">$first_name </td>
</tr>
<tr>
<td align=\"left\">Connection Date:</td>
<td align=\"left\"$>$connect_date</td>
</tr>
<tr>
<td align=\"left\" colspan=\"2\">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>
";
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";
mail($to, $subject, $msg, $headers);
date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ;
$order = "INSERT INTO reg_add (submit_date,
connect_date,
reg,
first_name,
)
VALUES
('$submit_date',
'$_POST[connect_date]',
'{$_POST[reg]}nv',
'$_POST[first_name]')";
$result = mysql_query($order);
if ($result) {
mail($to, $subject, $msg, $headers);
$reg = $_REQUEST['reg'] ;
$first_name = $_REQUEST['first_name'];
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name));
}
else {
header("location: reg_add_fail.php");
exit(); // as sugested by John Conde
}
?>
Put exit() after the redirect
header("location: reg_add_fail.php");
exit();
Just because you call header() doesn't mean the script stops executing immediately. Calling exit() will.
In your code, the PHP mail() function is being called outside the IF statement, so, you always get the email.
To send the email only when the query runs without errors, place the mail() inside the if statement!
PHP
if ($result) {
mail($to, $subject, $msg, $headers);
$reg = $_REQUEST['reg'] ;
$first_name = $_REQUEST['first_name'];
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name));
}
else {
header("location: reg_add_fail.php");
exit(); // as sugested by John Conde
}
EDITED TO SHOW THE ENTIRE CODE:
<?php
// Email Recipient
$to = 'newreg#41q.org';
// Email Subject
$subject = 'New Homeless Connection';
// Email Message
$msg = '
<html>
<head>
<title>New Homeless Connection</title>
</head>
<body>
<table cellspacing="0" cellpadding="10" border="1" align="left">
<tr>
<td align="left" width="150px">Registery No.:</td>
<td align="left">'.$reg.'</td>
</tr>
<tr>
<td align="left">First Name:</td>
<td align="left">'.$first_name.'</td>
</tr>
<tr>
<td align="left">Connection Date:</td>
<td align="left">'.$connect_date.'</td>
</tr>
<tr>
<td align="left" colspan="2">http://www.41q.org/admin/</td>
</tr>
</table>
<br>
<br>
</body>
</html>';
// Email Headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Homeless' . "\r\n";
date_default_timezone_set('America/Los_Angeles');
$submit_date = date("m/d/y g:i A") ;
// Prepare Database Query
$order = "
INSERT INTO reg_add (
submit_date,
connect_date,
reg,
first_name
)
VALUES (
'".$submit_date."',
'".$_POST['connect_date']."',
'".$_POST['reg']."nv',
'".$_POST['first_name']."'
)";
// Query Database
$result = mysql_query($order);
// Check If the result is valid
if ($result) {
// send email
mail($to, $subject, $msg, $headers);
// prepare and direct the user to the reg_add_success Page
$reg = $_REQUEST['reg'] ;
$first_name = $_REQUEST['first_name'];
header("location: reg_add_success.php?reg=" . urlencode($reg) . "&first_name=" . urlencode($first_name));
}
else {
// send the user to the reg_add_fail Page
header("location: reg_add_fail.php");
// exit from the script
exit();
}
?>

Categories