Jump to content

PHP Mail Doesnt Send Email


music_fan01

Recommended Posts

When I test my php, all of the errors work, but when I actually want to send an email the form doesnt go through and I get the error message 'Please enter a valid e-mail address'? I think I may have something wrong in my mail function. :confused:

 

<?php
if (empty($_POST['author']))
{
$errors[] = 'Please enter a name';
}
if (empty($_POST['subject']))
{
$errors[] = 'Please enter a subject';
}

if (empty($_POST['text']))
{
$errors[] = 'Please enter your comments.';
}

if (empty($_POST['email']))
{
$errors[] = 'Please enter an e-mail';
}
else if (!preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$ ^", $_POST['email']))
{
$errors[] = 'Please enter a valid e-mail address';
}
if (count($errors) == 0)
{
// Process form
mail("[email protected]","Subject: subject",
text,"From: author <email>");

echo "Thank you for using our mail form.<br/>";
echo "Your email has been sent.";
}
else
{
echo $errors[0];
}

?>

Link to comment
https://forums.phpfreaks.com/topic/247352-php-mail-doesnt-send-email/
Share on other sites

		$headers = "MIME-Version: 1.0\r\n";
		$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
		$headers.= "From: XXXXX\r\nReply-To: [email protected]";	

		$subject = "Your SUBJECT";//
		$final_message = "MESSAGE  ";  
		 mail($to, $subject, $final_message, $headers);

http://www.w3schools.com/PHP/php_ref_mail.asp

Just to make sure I am doing this right, is this the way my final php markup should be?

 

<?php
if (empty($_POST['author']))
{
$errors[] = 'Please enter a name';
}
if (empty($_POST['subject']))
{
$errors[] = 'Please enter a subject';
}

if (empty($_POST['text']))
{
$errors[] = 'Please enter your comments.';
}

if (empty($_POST['email']))
{
$errors[] = 'Please enter an e-mail';
}
else if (!preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$ ^", $_POST['email']))
{
$errors[] = 'Please enter a valid e-mail address';
}
if (count($errors) == 0)
{
// Process form
$headers = "MIME-Version: 1.0\r\n";
		$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";
		$headers.= "From: XXXXX\r\nReply-To: [email protected]";	

		$subject = "Your SUBJECT";//
		$final_message = "MESSAGE  ";  
		 mail($to, $subject, $final_message, $headers);
echo $errors[0];
}

?>

1. Can you able to send mail?

2. extra code - using if statement.

if (count($errors) == 0)
{
	// Process form
	$headers = "MIME-Version: 1.0\r\n";			
	$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";			
	$headers.= "From: XXXXX\r\nReply-To: [email protected]";							
	$subject = "Your SUBJECT";
	$final_message = "MESSAGE  ";  			 


	if (mail($to, $subject, $final_message, $headers) )
	{	echo " Thankyou! - Your feedback has been sent! ";	}
	else
	{	echo " Thankyou! - We could not send email. Please try later! ";	}
}

just make sure receiver email address included

	if (count($errors) == 0)	
	{		
	 //receiver email address
	 $to=$_POST['email'];

	// Process form		
	$headers = "MIME-Version: 1.0\r\n";					
	$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";					
	$headers.= "From: XXXXX\r\nReply-To: [email protected]";									
	$subject = "Your SUBJECT";		$final_message = "MESSAGE  ";  			 						

	if (mail($to, $subject, $final_message, $headers) )		
	{	echo " Thankyou! - Your feedback has been sent! ";	}		
	else		
	{	echo " Thankyou! - We could not send email. Please try later! ";	}	

	}

Now when I test it, I get a blank page.  :confused:

 


<?php
if (empty($_POST['author']))
{
$errors[] = 'Please enter a name';
}
if (empty($_POST['subject']))
{
$errors[] = 'Please enter a subject';
}

if (empty($_POST['text']))
{
$errors[] = 'Please enter your comments.';
}

if (empty($_POST['email']))
{
$errors[] = 'Please enter an e-mail';
}
else if (!preg_match("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$ ^", $_POST['email']))
{
$errors[] = 'Please enter a valid e-mail address';
}
	if (count($errors) == 0)	
	{		
	 //receiver email address
	 $to=$_POST['email'];

	// Process form		
	$headers = "MIME-Version: 1.0\r\n";					
	$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";					
	$headers.= "From: XXXXX\r\nReply-To: [email protected]";									
	$subject = "Your SUBJECT";		$final_message = "MESSAGE  ";  			 						

	if (mail($to, $subject, $final_message, $headers) )		
	{	echo " Thankyou! - Your feedback has been sent! ";	}		
	else		
	{	echo " Thankyou! - We could not send email. Please try later! ";	}	

	}

?>

 

 

 

<!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>Contact</title>
<link href="style.css" rel="stylesheet" type="text/css" />


</head>
<body>

<div id="templatemo_wrapper"> 

<div id="templatemo_header">


        
        <ul id="social_box">
                    <li><a href="http://www.facebook.com/"><img src="images/facebook.png" alt="facebook" /></a></li>
            <li><a href="http://www.twitter.com/"><img src="images/twitter.png" alt="twitter" /></a></li>              
        </ul>
        
      	<div id="site_title">
            <h1><a href="default.htm"><img src="images/logo2.png" alt="logo" /><span></span></a></h1>
        </div> <!-- end of site_title -->
        
      
    </div> <!-- end of templatemo_header -->
    


<!-- end of templatemo_menu -->
    
    <div id="templatemo_content_wrapper">
    	<div id="templatemo_content_top"></div>
        <div id="templatemo_content">
        
            <h2>Contact</h2>


        
            <div class="cleaner_h50"></div>
            
            	<div id="contact_form">
            
                    <h4>Quick Contact</h4>
		<p align="left">* Required fields.</p>
                    
                    <form method="post" name="ContactForm" id="contact" action="email.php" ">
                        
                        <div class="col_w340 float_l">
                        
                            <label for="name">* Name:</label> <input name="name" type="text" class="input_field" id="name" maxlength="40" />
                          	<div class="cleaner_h10"></div>

<label for="subject">* Subject:</label> <input name="subject" type="text" class="input_field" id="subject" maxlength="40" />
                          	<div class="cleaner_h10"></div>

                            
                            <label for="email">* Email:</label> <input name="email" type="text" class="input_field" id="email" maxlength="40" />
                          	<div class="cleaner_h10"></div>
                            
                                          

</div>
                        


                        <div class="col_w340 float_r">
                        
                            <label for="text">* Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>
                            <div class="cleaner_h10"></div>
                            
                            <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />
                            <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />

                            
                        </div>
                        
                   </form>

            </div> 

            <div class="cleaner"></div>
            
        </div>
        <div id="templatemo_content_bottom"></div>
</div>
    
    <div id="templatemo_sp_box">
    
    	<div class="col_w340 float_l">
         
		</div>
        </div>
        
        <div class="col_w340 float_r">
        
		</div>
        </div>
    
    </div>
    
    <div id="templatemo_footer">

      
        Copyright © 2011 <a href="www.twitter.com/">Starr</a><br/> 
        <a href="http://www.iwebsitetemplate.com" target="_parent">Website Templates</a> 
        by <a href="http://www.templatemo.com" target="_parent">Free CSS Templates</a>
    
    </div> <!-- end of templatemo_footer -->
    
</div> <!-- end of templatemo_wrapper -->    

</body>
</html>


Number of points I would like to tell you.

 

1.  Naming error – input name and if (empty($_POST['author']))

if (empty($_POST['author']))

label for="name">* Name:</label> <input name="name" type="text" class="input_field" id="name" maxlength="40" />

 

2. Error in email preg_match

 

Now try this code (please enter your email address in the $to variable)

<?php
if(isset($_POST['submit']))
{
if (empty($_POST['name']))
{$errors[] = 'Please enter a name';}

if (empty($_POST['subject']))
{$errors[] = 'Please enter a subject';}

if (empty($_POST['text']))
{$errors[] = 'Please enter your comments.';}

if (empty($_POST['email']))
{$errors[] = 'Please enter an e-mail';}
elseif (!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $_POST['email']))
{$errors[] = 'Please enter a valid e-mail address';}		


if (count($errors) == 0)			
{				 
	// Your OR Receiver email address		 
	$to=  your email address;				

	// Process form				
	$headers = "MIME-Version: 1.0\r\n";							
	$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";							
	$headers.= "From: XXXXX\r\nReply-To: [email protected]";											

	$subject = $_POST['subject'];	
	$final_message = "	Name - " . $_POST['name']. " email - ".$_POST['email']. " Message-".$_POST['text']; 			 										

	if (mail($to, $subject, $final_message, $headers) )				
	{	echo " Thankyou! - Your feedback has been sent! ";	}				
	else				
	{	echo " Thankyou! - We could not send email. Please try later! ";	}					
}
else
{ 	var_dump($errors);	}
}
else
{
?>
<form method="post" action="email.php">
 <label for="name">* Name:</label> 
 <input name="name" type="text" class="input_field" id="name" maxlength="40" />                          	
 <div class="cleaner_h10"></div>

 <label for="subject">* Subject:</label> 
 <input name="subject" type="text" class="input_field" id="subject" maxlength="40" />                          	
 <div class="cleaner_h10"></div>

 <label for="email">* Email:</label>
  <input name="email" type="text" class="input_field" id="email" maxlength="40" />                          	
  <div class="cleaner_h10"></div>
  
   <div class="col_w340 float_r">                                                    
   <label for="text">* Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>                            		       <div class="cleaner_h10"></div>                                                        
   <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />                            
   <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />                                                  
</form>
<?
}
?> 


 

PS :This is an example you can do any changes you like.

This is the php that I used. I am not too sure, but I think I wasnt suppose to put my email address in the Reply-To. I still keep getting that error.  :(

 


<?php
if(isset($_POST['submit']))
{
if (empty($_POST['name']))
{$errors[] = 'Please enter a name';}

if (empty($_POST['subject']))
{$errors[] = 'Please enter a subject';}

if (empty($_POST['text']))
{$errors[] = 'Please enter your comments.';}

if (empty($_POST['email']))
{$errors[] = 'Please enter an e-mail';}
elseif (!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $_POST['email']))
{$errors[] = 'Please enter a valid e-mail address';}		
{

if (count($errors) == 0)			
{				 
	// Your OR Receiver email address		 
	$to=  [email protected];				

	// Process form				
	$headers = "MIME-Version: 1.0\r\n";							
	$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";							
	$headers.= "From: XXXXX\r\nReply-To: [email protected]";											

	$subject = $_POST['subject'];	
	$final_message = "	Name - " . $_POST['name']. " email - ".$_POST['email']. " Message-".$_POST['text']; 			 										

	if (mail($to, $subject, $final_message, $headers) )				
	{	echo " Thankyou! - Your feedback has been sent! ";	}				
	else				
	{	echo " Thankyou! - We could not send email. Please try later! ";	}					
}
else
{ 	var_dump($errors);	}
}
else
{
?>
<form method="post" action="email.php">
 <label for="name">* Name:</label> 
 <input name="name" type="text" class="input_field" id="name" maxlength="40" />                          	
 <div class="cleaner_h10"></div>

 <label for="subject">* Subject:</label> 
 <input name="subject" type="text" class="input_field" id="subject" maxlength="40" />                          	
 <div class="cleaner_h10"></div>

 <label for="email">* Email:</label>
  <input name="email" type="text" class="input_field" id="email" maxlength="40" />                          	
  <div class="cleaner_h10"></div>
  
   <div class="col_w340 float_r">                                                    
   <label for="text">* Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>                            		       <div class="cleaner_h10"></div>                                                        
   <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />                            
   <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />                                                  
</form>
<?
}
?> 

longer version for you. just copy , enter your email address and run it

 

<?php
if(isset($_POST['submit']))
{
if (empty($_POST['name']))
{$errors[0] = 'Please enter a name'; echo 'Please enter a name'; }

if (empty($_POST['subject']))
{$errors[1] = 'Please enter a subject'; echo 'Please enter a subject';}

if (empty($_POST['text']))
{$errors[2] = 'Please enter your comments.'; echo 'lease enter your comments.';}

if (empty($_POST['email']))
{$errors[3] = 'Please enter an e-mail'; echo 'Please enter an e-mail';}
elseif (!preg_match("/^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}$/i", $_POST['email']))
{$errors[4] = 'Please enter a valid e-mail address'; echo 'Please enter a valid e-mail address';}		


if (count($errors) == 0)			
{				 
	// Your OR Receiver email address		 
	$to='[email protected]';				

	// Process form				
	$headers = "MIME-Version: 1.0\r\n";							
	$headers.= "Content-type: text/html; charset=iso-8859-1\r\n";							
	$headers.= "From: XXXXX\r\nReply-To: [email protected]";											

	$subject = $_POST['subject'];	
	$final_message = "	Name - " . $_POST['name']. " email - ".$_POST['email']. " Message-".$_POST['text']; 			 										

	if (mail($to, $subject, $final_message, $headers) )				
	{	echo " Thankyou! - Your feedback has been sent! ";	}				
	else				
	{	echo " Thankyou! - We could not send email. Please try later! ";	}					
}
else
{ 	var_dump($errors);	}
}
else
{
?>
<form method="post" action="#">
 <label for="name">* Name:</label> 
 <input name="name" type="text" class="input_field" id="name" maxlength="40" />                          	
 <div class="cleaner_h10"></div>

 <label for="subject">* Subject:</label> 
 <input name="subject" type="text" class="input_field" id="subject" maxlength="40" />                          	
 <div class="cleaner_h10"></div>

 <label for="email">* Email:</label>
  <input name="email" type="text" class="input_field" id="email" maxlength="40" />                          	
  <div class="cleaner_h10"></div>
  
   <div class="col_w340 float_r">                                                    
   <label for="text">* Message:</label> <textarea id="text" name="text" rows="0" cols="0" class="required"></textarea>                            		       <div class="cleaner_h10"></div>                                                        
   <input type="submit" class="submit_btn float_l" name="submit" id="submit" value="Send" />                            
   <input type="reset" class="submit_btn float_r" name="reset" id="reset" value="Reset" />                                                    </      div>                                           
</form>
<?
}
?>	 

 

for your reading

http://www.kirupa.com/web/php_contact_form2.htm

http://www.phpeasystep.com/phptu/8.html

I went ahead and change some things around. I found an example that writes out the error in red right in the form. So I went ahead tried to combine examples, but in doing so, I think I may have made things worse. I get the error : Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/a9577046/public_html/mail.php on line 39

 

 

<?php
if(isset($_POST['submit']))
{
if ($_POST['name'] != "") {  
                $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);  
                if ($_POST['name'] == "") {  
                    $errors .= 'Please enter a valid name.<br/><br/>';  
                }  
            } else {  
                $errors .= 'Please enter your name.<br/>';  
            }  
      
            if ($_POST['email'] != "") {  
                $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);  
                if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {  
                    $errors .= "$email is <strong>NOT</strong> a valid email 

address.<br/><br/>";  
                }  
            } else {  
                $errors .= 'Please enter your email address.<br/>';  
            }  
            
            if ($_POST['message'] != "") {  
                $_POST['message'] = filter_var($_POST['message'], FILTER_SANITIZE_STRING);  
                if ($_POST['message'] == "") {  
                    $errors .= 'Please enter a message to send.<br/>';  
                }  
            } else {  
                $errors .= 'Please enter a message to send.<br/>';  
            }  

if (count($errors) == 0)			
{				 
	// Your OR Receiver email address		 
	$to='[email protected]';				

	// Process form				
	$headers = "MIME-Version: 1.0\r\n";						


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


	$headers.= "From:$_POST['name']\r\n Reply-To: $_POST['email'}";			



	$subject = $_POST['subject'];	
	$message = "	Name - " . $_POST['name']. " email - ".$_POST['email']. " 

Message-".$_POST['text']; 			 						




            if (!$errors) {  
                $mail_to =  'Email: ' . $_POST['email'] . "\n";  
                $subject = 'New Mail from Form Submission';  
                $message  = 'From: ' . $_POST['name'] . "\n";  
                $message .= 'Email: ' . $_POST['email'] . "\n";  
                $message .= 'Homepage: ' . $_POST['homepage'] . "\n";  
                $message .= "Message:\n" . $_POST['message'] . "\n\n";  
                mail($to, $subject, $message);  

	if (mail($to, $subject, $message, $headers) )				
	{	echo " Thank you! - Your feedback has been sent! ";	}		


           echo "Thank you for your email!<br/><br/>";  
            } else {  
                echo '<div style="color: red">' . $errors . '<br/></div>';  
            }  
          
else
{ 	var_dump($errors);	}
}
else
{
?>
<form method="post" action="#">
 <label for="name">* Name:</label> 
 <input name="name" type="text" class="input_field" id="name" maxlength="40" />      

                    	
 <div class="cleaner_h10"></div>

                     	 <div class="cleaner_h10"></div>

 <label for="email">* Email:</label>
  <input name="email" type="text" class="input_field" id="email" maxlength="40" />   

                       	
  <div class="cleaner_h10"></div>
  
   <div class="col_w340 float_r">                                                    
   <label for="text">* Message:</label> <textarea id="text" name="text" rows="0" 

cols="0" class="required"></textarea>                            		       <div 

class="cleaner_h10"></div>                                                        
   <input type="submit" class="submit_btn float_l" name="submit" id="submit" 

value="Send" />                            
   <input type="reset" class="submit_btn float_r" name="reset" id="reset" 

value="Reset" />                                                    </      div>             

                              
</form>
<?
}
?>	 



Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.