Jump to content

mail() function not working in php


mrzebra81

Recommended Posts

Hello,

 

The mail function works on my one website but it won't on my other site. On the site that it won't work on, I'm trying to email the contents of the cart. I tested it out and it diplays the contents of the cart properly  if I  echo $email. I don't get any errors on the site, it seems to process but I don't receive any emails. here is my code for process.php:

 

<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','1');
require_once("connect.php");
?>

<?php 


$check=$_POST['check'];

/* code that outputs the cart contents and totals up the cart */

$cartOutput="";
$total="0";




// start the foreach loop to go through each item in the cart array
$i=0;
foreach($_SESSION["cart_array"] as $each_item) {

	$item_id=$each_item['id'];
	$prod_id=$each_item['product_id'];



	// distiguish between a regular item and a gift box
	switch ($prod_id) {




		// if it is a gift box
		case "12":	


			// Get product name and price
			$sql=mysql_query("SELECT products.productPrice, products.productName FROM products WHERE productId='$prod_id' LIMIT 1");
			while ($row=mysql_fetch_array($sql)) {
				$product_name=$row["productName"];
				$price=$row["productPrice"];

			}



			$subtotal=$price*$each_item['quantity'];
			$total=$subtotal + $total;

			if(isset ($_SESSION["cart_array"] )) {		
				// dynamic checkout button assembly
                                         
				// dynamic cart item display			
				$cartOutput.="<tr align=center>
							<td width=5%><font face=arial color='#999999'>".substr($each_item['id'],0,7)."</font></td>
							<td width=25%><font face=arial color='#999999'>".$product_name."</font></td>
							<td width=40% valign=center><font face=arial color='#999999'>Wrap my products in a Gift Box</font></td>
							<td valign=center width=5%><font face=arial color='#999999'>".$each_item['quantity']."


								</td>
							<td width=5%><font color='#999999'>$".$price.".00</font></td>
							<td width=10%><font color='#999999'>$".$subtotal.".00</font></td>

						</tr>
						<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";	

			} // end if statement to see if cart is empty			

		break;	


		// if it is a Reed Diffuser Bottle
		case "17":	


			// Get product name and price
			$sql=mysql_query("SELECT products.productPrice, products.productName, products.productDesc FROM products WHERE productId='$prod_id' LIMIT 1");
			while ($row=mysql_fetch_array($sql)) {
				$product_name=$row["productName"];
				$price=$row["productPrice"];
				$desc=$row['productDesc'];
			}



			$subtotal=$price*$each_item['quantity'];
			$total=$subtotal + $total;

			if(isset ($_SESSION["cart_array"] )) {		

				// dynamic checkout button assembly
                    					
				// dynamic cart item display			
				$cartOutput.="<tr align=center>
							<td width=5%><font face=arial color='#999999'>".substr($each_item['id'],0,7)."</font></td>
							<td width=25%><font face=arial color='#999999'>".$product_name."</font></td>
							<td width=40% valign=center><font face=arial color='#999999'>".$desc."</font></td>
							<td valign=center width=5%><font face=arial color='#999999'>".$each_item['quantity']."</td>
							<td width=5%><font face=arial color='#999999'>$".$price.".00</font></td>
							<td width=10%><font face=arial color='#999999'>$".$subtotal.".00</font></td>

						</tr>
						<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";	

				} // end if statement to see if cart is empty			

		break;	



		// if it is a salve
		case "3":


			$salveId=substr($each_item['id'],2,4);
			$prodId=substr($each_item['id'],0,1);


			// Get product name and price
			$sql=mysql_query("SELECT * FROM salves WHERE productId='$salveId' LIMIT 1");
			while ($row=mysql_fetch_array($sql)) {

				$scent=$row['productName'];


			}

			$sql=mysql_query("SELECT * FROM products WHERE productId='$prodId' LIMIT 1");
			while ($row=mysql_fetch_array($sql)) {
				$price=$row['productPrice'];
				$product_name=$row['productName'];


			}


			if(isset ($_SESSION["cart_array"] )) {		




			$subtotal=$price*$each_item['quantity'];
			$total=$subtotal + $total;


				// dynamic cart item display			
				$cartOutput.="<tr align=center>
							<td width=5%><font face=arial color='#999999'>".$each_item['id']."</font></td>
							<td width=25%><font face=arial color='#999999'>".$product_name."</font></td>
							<td width=40%><font face=arial color='#999999'>".$scent."</font></td>
							<td valign=center width=5%><font face=arial color='#999999'>".$each_item['quantity']."

								</td>
							<td width=5%><font face=arial color='#999999'>$".$price.".00</font></td>
							<td width=10%><font face=arial color='#999999'>$".$subtotal.".00</font></td>

						</tr></form>
						<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";	


			} // end if statement to see if cart is empty



			break;


		// if it is not a gift box or sale/clearance item	
		default :

			$scent=substr($each_item['scent'],0,4);



			// Get product name and price
			$sql=mysql_query("SELECT products.productPrice, products.productName FROM products WHERE productId='$prod_id' LIMIT 1");
			while ($row=mysql_fetch_array($sql)) {
				$product_name=$row["productName"];
				$price=$row["productPrice"];
			}

			// get scent for regular product
			$sql2=mysql_query("SELECT productscents.scentName FROM productscents WHERE scentId='$scent' LIMIT 1");
			while ($row2=mysql_fetch_array($sql2)) {
				$scent=$row2['scentName'];
			}



			$subtotal=$price*$each_item['quantity'];
			$total=$subtotal + $total;

			if(isset ($_SESSION["cart_array"] )) {		

				// dynamic cart item display			
				$cartOutput.="<tr align=center>
							<td width=5%><font face=arial color='#999999'>".substr($each_item['id'],0,7)."</font></td>
							<td width=25%><font face=arial color='#999999'>".$product_name."</font></td>
							<td width=40%><font face=arial color='#999999'>".$scent."</font></td>
							<td valign=center width=5%><font face=arial color='#999999'>".$each_item['quantity']."</td>
							<td width=5%><font face=arial color='#999999'>$".$price.".00</font></td>
							<td width=10%><font face=arial color='#999999'>$".$subtotal.".00</font></td>

						</tr></form>
						<tr> <td colspan=6><hr size=1 width=50% color='#CBB659'/></td></tr>";	


			} // end if statement to see if cart is empty
			break;

	} // end switch	


} // end foreach that goes through each item in the cart				

/* Code that generates the email */

	$ans=$_SESSION['check'];
$check=$_POST['check'];
// To send HTML mail, the Content-type header must be set

$headers = 'MIME-Version: 1.0' . "\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 



	// customer information	
	$firstName = strip_tags($_POST['firstName']);
	$lastName = strip_tags($_POST['lastName']);
	$address = strip_tags($_POST['address']);
	$city = strip_tags($_POST['city']);
	$prov = strip_tags($_POST['prov']);
	$postalCode = strip_tags(strtoupper($_POST['postal']));
	$emailAddress = strip_tags($_POST['email']);

	$payment = $_POST['payment'];
	$ship = $_POST['shipping'];

	// email subject
	$subject= "Online Order Request";


	// email address the form will be submitted to
	$myemail="orders@coppercreekbathandbody.ca";


	if ($firstName!="" && $lastName!="" && $address!="" && $city!="" && $prov!="" && $postalCode!="" && $emailAddress!="" && $ans==$check) {

			$email="<html><center>

				<table border=0 width=100% cellspacing=0 cellpadding=5 style='background:#1d1d1d' >
				<tr>
					<td colspan=6><center><h2><font color=#999999>$subject</font></h2></center></td>
				</tr>	

				<tr valign=center >
					<td colspan=6 align=center valign=center><font face=arial color=#999999>
						$firstName $lastName<br>
						$address<br>
						$city, $prov $postalCode<br>
					    $emailAddress
						</font><br><br>
					</td>
				</tr>



						<tr align=center style='background: #333'>
							<td width=8%><font face=arial  color=#999999>Item #</font></td>
							<td width=25%><font face=arial color=#999999>Product</font></td>
							<td width=40%><font  face=arial color=#999999>Description</font></td>
							<td width=4%><font face=arial color=#999999>Quantity</font></td>
							<td width=4%><font face=arial color=#999999>Price</font></td>
							<td width=10%><font face=arial color=#999999>Total Cost</font></td>

						</tr>



												<tr><font face=arial>
												$cartOutput														
												</font></tr>	


						<tr style='background: #333'>

							<td colspan=5 align=right><font face=arial color=#999999>Total Price:</font></td>
							<td align=center><font face=arial color=#999999>$".$total.".00 </font></td>
						</tr>


						<tr>
							<td colspan=4 align=right valign=center>	    
								<font face=arial color=#999999>Payment Method: </font>
							</td>
							<td colspan=2 align=right valign=center>	    
								<font face=arial color=#999999>$payment</font>
							</td>	
						</tr>					

							<tr>
							<td colspan=4 align=right valign=center>	    
								<font face=arial color=#999999>Shipping: </font>
							</td>
							<td colspan=2 align=right valign=center>	    
								<font face=arial color=#999999>$ship</font>
							</td>	
						</tr>		


					</table></center></body></html>";


			mail($myemail, $subject, $email, $headers);
			unset($_SESSION['check']);
			header("location: success.php");
		}

	else {
		unset($_SESSION['check']);


	}

 

customerInformation.php


<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','1');
require_once("connect.php");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script type="text/javascript" src="./js/functions.js"></script>
<link rel="stylesheet" type="text/css" href="./css/navigationStyles.css"/>
<script language=javascript type=text/javascript>



	function stopRKey(evt) {
   			var evt = (evt) ? evt : ((event) ? event : null);
   			var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
   			if ((evt.keyCode == 13) && (node.type=="text")) {
   	   			return false;
   	   		}
	}

	document.onkeypress = stopRKey;

</script>




</head>
<body>
<div id='wrapper'>
<div id='main'>	



			<table border=0 width=70% cellspacing=10 cellpadding=5 id=table>
				<tr>
					<td colspan=6>
						<center><img src="./images/customerInfoLogo.gif"><br>

						<table border=0 width=90%>

							<tr>
								<td width=33% align=right>
									<a href='index.php'>HOME</a>
								</td>
								<td width=34%></td>
								<td width=33% align=left>
									<a href='cart.php'>VIEW CART</a>
								</td>
							</tr>

						</table>
						<br><br>
						</center>
					</td>
				</tr>


				<tr>
					<td colspan=6  align=center valign=center>
						<form action="process.php" name='order' method="post" onsubmit="document.getElementById('button').disabled=true; document.getElementById('button').value='Submitting, please wait...';">
						<!-- REQUEST ORDER TABLE -->

							<table width=90% border=0 cellspacing=5 cellpadding=5>


								<!-- CLIENT NAME -->
								<tr valign=center>
									<td width=50% align=right>
										First Name:
									</td>	

									<td width=50% align=left>
										<input type=text maxlength=40 width=40px class=contactBox id=firstName name="firstName" onBlur="validate(this.id);">
									</td>

								</tr>

								<tr valign=center>
									<td width=50% align=right>
										Last Name:
									</td>	

									<td width=50% align=left>
										<input type=text maxlength=40 width=40px onBlur='validate(this.id);' class=contactBox id=lastName name="lastName">
									</td>	
								</tr>

								<!-- ADDRESS -->
								<tr valign=center>
									<td width=50% align=right>
										Address:
									</td>	

									<td width=50% align=left>
										<input type=text width=40px id=address class=contactBox name="address" onBlur='validate(this.id);'>
									</td>	
								</tr>

								<!-- CITY -->
								<tr valign=center>
									<td width=50% align=right>
										City:
									</td>	

									<td width=50% align=left>
										<input type=text width=40px id=city class=contactBox name="city" onBlur='validate(this.id);'>
									</td>	
								</tr>

								<!-- Province -->
								<tr valign=center>
									<td width=50% align=right>
										Province:
									</td>	

									<td width=50% align=left>
										<select name="prov">
											<option value="AB">Alberta</option>
											<option value="BC">British Columbia</option>
											<option value="MB">Manitoba</option>
											<option value="NB">New Brunswick</option>
											<option value="NL">Newfoundland & Labrador</option>
											<option value="NT">Northwest Territories</option>
											<option value="NS">Nova Scotia</option>
											<option value="NU">Nunavut</option>
											<option value="ON">Ontario</option>
											<option value="PE">Prince Edward Island</option>
											<option value="QC">Quebec</option>
											<option value="SK" selected>Saskatchewan</option>
											<option value="YT">Yukon</option>

										</select>
									</td>	
								</tr>

								<!-- POSTAL CODE -->
								<tr valign=center>
									<td width=50% align=right>
										Postal Code:
									</td>	

									<td width=50% align=left>
										<input type=text width=40px id=postal class=contactBox name="postal" maxlength=7'>
									</td>	
								</tr>

								<!-- EMAIL ADDRESS -->
								<tr valign=center>
									<td width=50% align=right>
										Email:
									</td>	

									<td width=50% align=left>
										<input type=text width=60px id=email class=contactBox name="email" onBlur='validateEmail(this.id);'>
									</td>	
								</tr>

								<!-- PAYMENT TYPE -->
								<tr valign=center>
									<td width=50% align=right>
										Payment Type:
									</td>	

									<td width=50% align=left>
										<select name="payment">
											<option value="Email Money Transfer" selected>Email Money Transfer</option>
											<option value="Cash">Cash</option>
											<option value="Money Order">Money Order</option>
										</select>

									</td>	
								</tr>

								<!-- SHIPPING	-->
								<tr valign=center>
									<td width=50% align=right>
										Need your order shipped:
									</td>	

									<td width=50% align=left>
										<select name="shipping">
											<option value="Don't Ship" selected>Don't Ship</option>
											<option value="Ship Order">Ship Order</option>

										</select>

									</td>	
								</tr>

								<tr>
									<td colspan=2>
										<img src='captcha.php'> <input type='text' class=cap size=4 width=3 maxlength=4 onKeyPress='return check_qty(event);' name='check'>
									</td>
								</tr>

								<tr>	
								<td colspan=2 valign=top>	
									<input type=submit value='SEND ORDER' class='updateButton' id=button>
									<input type=reset value='CANCEL ORDER' class='updateButton' id=button onClick="location.href='home.php'">

								</td>

				</tr>		
						</table>
						</form>

					</td>
				</tr>					


			</table>	

		</div>	


<div id="sidebar" >
		<?php require ("menu.html"); ?> 





	</div>		

</div>
</body>
</html>

 

I have no idea what is happening. It should be displaying an html formatted email. Any help is greatly appreciated.

Link to comment
Share on other sites

Looks like your quotes here are killing it.

<font face=arial color=#999999>$".$total.".00 </font>

I would think you would be fine without them.

<font face=arial color=#999999>$$total.00 </font>

 

I'm not sure about the 'kill' it.. The double quotes are OK there.

Also be careful there, you think $$total.00 will be same as "$".$total.".00" ?

 

 

@mrzebra81:

Are your two websites you mentioned, on the same server? Is it a dedicated or shared server?

Link to comment
Share on other sites

Yes they are on the same server...one email finally came through, in the proper html format. So it looks like it may be working with the code I posted above. I will test it out again.

 

The only thing it says, is the email is from noreply@onlinerserver.cc  How do i change that?

Link to comment
Share on other sites

The only thing it says, is the email is from noreply@onlinerserver.cc  How do i change that?

 

Have you tried setting the from address in the headers portion? More information can be found under example 2 in the PHP manual for mail():

http://php.net/manual/en/function.mail.php

 

 

plain text emails get sent instantly, but these html emails aren't. I've only had one go through out of close to 20 test emails.  godaddy.com is my hosting company.

 

Not always. I've had plain-text e-mails take hours before they're sent by the host...although it usually doesn't take that long. Maybe the mail servers are overloaded at the moment...  :shrug:

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.