Sending E-Mail Error Html Form Using Php Script

Hey,

I Have Form In File Named Contact.php

            <div>
                <h5>Contact Form</h5>
                <form id="contact-form" method="post" action="MailHandler.php" >
                    <div class="success"> Contact form submitted! <strong>We will be in touch soon.</strong> </div>
                    <fieldset>
                            <div class="form-div-1">
                                <label class="name">
                                <input type="text" value="Name*:"  name="name"/>
                                <br>
                                <span class="error">*This is not a valid name.</span> <span class="empty">*This field is required.</span> </label>
                            </div>
                            <div class="form-div-2">
                                <label class="email">
                                <input type="email" value="Email*:" name="email"/>
                                <br>
                                <span class="error">*This is not a valid email address.</span> <span class="empty">*This field is required.</span> </label>
                            </div>
                            <div class="form-div-3">
                                <label class="phone notRequired">
                                <input type="tel" value="Phone:" name="contactno"/>
                                <br>
                                <span class="error">*This is not a valid phone number.</span> <span class="empty">*This field is required.</span> </label>
                            </div>
                            <label class="message">
                            <textarea name="msg">Message*:</textarea>
                            <br>
                            <span class="error">*The message is too short.</span> <span class="empty">*This field is required.</span>                            </label>
                         
                       
                       <div class="btns">
                         <br/>  
                   <input data-type="submit" class="btn btn-primary btn2" type="submit" value="Submit" name="submit" style="width:100px;">
                  <!--    <a  data-type="submit" class="btn btn-primary btn2" name="submit">Submit</a>-->
                            <p>*required fields</p>
                        </div>
                    </fieldset>
                </form>
                				
							
            </div>

php script On Seperate File named MailHandler.php

<?php
							
							if(isset($_POST['submit']))
							{
							 $to 	  = '*********@gmail.com';
	$name	  = $_POST['name'];
	$email    = $_POST['email'];
	$phone    = $_POST['contactno'];	
	$subject  = 'Enquiry';
    $comment  = $_POST['msg'];
        
	$headers = "From:" .  $email .  "\r\n";
$head .= "MIME-Version: 1.0\r\n"
  ."Content-Type: text/plain; charset=utf-8\r\n"
  ."From: =?UTF-8?B?". base64_encode($from_name) ."?= <$from_address>\r\n"
  ."X-Mailer: PHP/". phpversion();
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = "<html><body>";
$message .= '<table border="0" cellpadding="10">';
$message .= "<tr><td><strong> Name:</strong> </td><td>" . $name . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . $email . "</td></tr>";
$message .= "<tr><td><strong>Mobile Number:</strong> </td><td>" . $phone . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . $comment . "</td></tr>";


$message .= "</table>";
$message .= "</body></html>";


     $mailsent = mail($to,$subject,$message,$headers);
	 
	
								
	 if($mailsent)
	 {
		 echo '<div id="success" class="success box">
                                Thank you. We have received your message and will contact you back shortly.
                            </div>';
	 }
	 else
	 {
		 echo '<div id="error" class="error box">
                                Something went wrong. Please contact us via email or phone. We truly apologize for the inconvenience.
                            </div>';
	 }
	 
	 
	 }
							
							?>

I get Successful Message On Clicking Submit Button But Mail Never Reaches My Email Id In Script When Hosted In Live Server.

What errors if any are reported by PHP? You might need to crank up the error reporting level to report everything, and log any errors to an error log file

Actually When I click On Submit Button I get A Message Mail Sent Successfuly But Mail Never Reaches The Email Id Mentioned In Php Script.Thats the Error.

Maybe Some Code I Am Going Wrong In Php Script.Please I Would Be Grateful To You If You Help Me Sorting This Out.

Where is $head initialized?

Looks like it should be $headers:

$headers .= “MIME-Version: 1.0\r\n”

How To Link Seperate Php Script Named MailHandler.php Present In bat Folder With contact.php

I Think Like This Is It Fine??

form id=“contact-form” method=“post” action=“MailHandler.php”

Tried Initialising $headers Never Worked Same As Old.I Get Successful Message Mail Never Reaches.

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

Return Values

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Do you think it would reach the destination with a header like

From: ?UTF-8?B?undefined?=undefined

Hey My New PHP Script MailHandler.php I altered To contact.php. Still Mail is Not Reaching Destination.

<?php
							
							if(isset($_POST['submit']))
							{
							 $to 	  = '*********@gmail.com';
	$name	  = $_POST['name'];
	$email    = $_POST['email'];
	$phone    = $_POST['contactno'];	
	$subject  = 'Enquiry';
    $comment  = $_POST['msg'];
        
	$headers .= 'From: Rithesh <*******************@gmail.com>' . "\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-Type: text/plain; charset=utf-8' ."\r\n";
    $headers .= 'X-Mailer: PHP/'. phpversion();
    $headers .= 'Content-Type: text/html; charset=ISO-8859-1'. "\r\n";

$message .= '<html><body>';
$message .= '<table>';
$message .= '<tr><td><strong> Name:</strong> </td><td>' . $name . "</td></tr>";
$message .= '<tr><td><strong>Email:</strong> </td><td>' . $email . "</td></tr>";
$message .= '<tr><td><strong>Mobile Number:</strong> </td><td>' . $phone . "</td></tr>";
$message .= '<tr><td><strong>Message:</strong> </td><td>' . $comment . "</td></tr>";
$message .= '</table>';
$message .= '</body></html>';


$mailsent = mail($to,$email,$phone,$comment,$subject,$message,$headers);
	 
	
if($mailsent)
	 {
		 echo '<div id="success" class="success box">
                Thank you. We have received your message and will contact you back shortly.
               </div>';
	 }
	 else
	 {
		 echo '<div id="error" class="error box">
                Something went wrong. Please contact us via email or phone. We truly apologize for the inconvenience.
               </div>';
	 }
	 
	 
	 }
							
?>

Are you over-riding the native mail() function?
http://php.net/manual/en/function.mail.php

Yes, your mail() function should only have 4 bits in it, like so:

mail($to,$subject,$message,$headers);  

So $to is the email to whom the data is being sent, the $subject is the subject line of the email, the $message is what appears in the body of the email, and the $headers is what you already have.

Normally your $message variable would be composed of various bits. E.g.

$message = "Name: $name\n" .
"Phone: $Phone\n" .
"Email: $Email\n" .
"Message: $comment";

mail($to,$subject,$headers,$message);

Tried This Still Mail Not Reaching With Those Messages of mailsent function on clicking submit button Other Than success Message In HTML Form.I Feel Something Is Missing Still.So Mail not Reaching The Destination I Need.

Hehe, are you dislexic? You need to keep the order shown above (and which you had in your original post.) Maybe there are other issues, but you need to get this bit right. :slight_smile:

I’ve just noticed that you declare two different content types:

$headers .= ‘Content-Type: text/plain; charset=utf-8’ .“\r\n”;
$headers .= ‘X-Mailer: PHP/’. phpversion();
$headers .= ‘Content-Type: text/html; charset=ISO-8859-1’. “\r\n”;

Just get rid of that line in red, as it’s the bottom line you want.

I Added $headers .= ' To: Prajwal <*********@gmail.com>'. "\r\n"; but Never Worked How To Send To Many In General This Is Somehwere I am going Wrong I Feel.

<?php
							
							if(isset($_POST['submit']))
							{
							 $to 	  = '*********@gmail.com';
	$name	  = $_POST['name'];
	$email    = $_POST['email'];
	$phone    = $_POST['contactno'];	
	$subject  = 'Enquiry';
    $comment  = $_POST['msg'];
        
	$message .= '<html><body>';
    $message .= '<table>';
    $message .= '<tr><td><strong> Name:</strong> </td><td>' . $name . "</td></tr>";
    $message .= '<tr><td><strong>Email:</strong> </td><td>' . $email . "</td></tr>";
    $message .= '<tr><td><strong>Mobile Number:</strong> </td><td>' . $phone . "</td></tr>";
    $message .= '<tr><td><strong>Message:</strong> </td><td>' . $comment . "</td></tr>";
    $message .= '</table>';
    $message .= '</body></html>';
	
	
	$headers .= 'To: Prajwal <*********@gmail.com>'. "\r\n";
	$headers .= 'From: Rithesh <*******************@gmail.com>' . "\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'X-Mailer: PHP/'. phpversion();
    $headers .= 'Content-Type: text/html; charset=iso-8859-1'. "\r\n";




$mailsent = mail($to,$subject,$message,$headers);
	 
	
if($mailsent)
	 {
		 echo '<div id="success" class="success box">
                Thank you. We have received your message and will contact you back shortly.
               </div>';
	 }
	 else
	 {
		 echo '<div id="error" class="error box">
                Something went wrong. Please contact us via email or phone. We truly apologize for the inconvenience.
               </div>';
	 }
	 
	 
	 }
							
?>

Actually I Dont Code Php Use Wordpress But As My Friend Having Deadline Helping Him Out Trying Every Possible Ways But Sad None Is Working Out there.

I Am Grateful You Have Helped On Many Parts.

Your headers should not contain another TO header; that is what the $to variable is for.

You do not have a linebreak after your X-Mailer header, but you should if you’re putting the content type after it.

If it still doesnt work, you may be using a SMTP server that is expecting \n instead of \r\n. Try replacing that.

Tried This Still Unable To Fix This.

<?php
							
							if(isset($_POST['submit']))
							{
							 $to 	  = '*********@gmail.com';
	$name	  = $_POST['name'];
	$email    = $_POST['email'];
	$phone    = $_POST['contactno'];	
	$subject  = 'Enquiry';
    $comment  = $_POST['msg'];
        
	$message .= '<html><body>';
    $message .= '<table>';
    $message .= '<tr><td><strong> Name:</strong> </td><td>' . $name . "</td></tr>";
    $message .= '<tr><td><strong>Email:</strong> </td><td>' . $email . "</td></tr>";
    $message .= '<tr><td><strong>Mobile Number:</strong> </td><td>' . $phone . "</td></tr>";
    $message .= '<tr><td><strong>Message:</strong> </td><td>' . $comment . "</td></tr>";
    $message .= '</table>';
    $message .= '</body></html>';
	
	$headers .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-Type: text/html; charset=iso-8859-1'. "\r\n";
	$headers .= 'From: Rithesh <*******************@gmail.com>' . "\r\n";
    $headers .= 'X-Mailer: PHP'. phpversion();
    

	$mailsent = mail($to,$subject,$message,$headers);
	 
	
if($mailsent)
	 {
		 echo '<div id="success" class="success box">
                Thank you. We have received your message and will contact you back shortly.
               </div>';
	 }
	 else
	 {
		 echo '<div id="error" class="error box">
                Something went wrong. Please contact us via email or phone. We truly apologize for the inconvenience.
               </div>';
	 }
	 
	 
	 }
							
?>

Tried As You Said But None Worked This Is The Finalised Script I am Using But Not Getting Results.Lol Seems Like It Will Be A Great Fix To Me Trying Hard Without PHP Knowledge.

<?php
							
							if(isset($_POST['submit']))
							{
							 $to 	  = '*********@gmail.com';
	$name	  = $_POST['name'];
	$email        = $_POST['email'];
	$phone       = $_POST['contactno'];	
	$subject     = 'Enquiry';
        $comment  = $_POST['msg'];
    
    $message .= '<html><body>';
    $message .= '<table>';
    $message .= '<tr><td><strong> Name:</strong> </td><td>' . $name . "</td></tr>";
    $message .= '<tr><td><strong>Email:</strong> </td><td>' . $email . "</td></tr>";
    $message .= '<tr><td><strong>Mobile Number:</strong> </td><td>' . $phone . "</td></tr>";
    $message .= '<tr><td><strong>Message:</strong> </td><td>' . $comment . "</td></tr>";
    $message .= '</table>';
    $message .= '</body></html>';
	
	$headers .= 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-Type: text/html; charset=iso-8859-1'. "\r\n";
	$headers .= 'From: Rithesh <*******************@gmail.com>' . "\r\n";
        $headers .= 'X-Mailer: PHP/'. phpversion();
    

	$mailsent = mail($to,$subject,$message,$headers);
	 
	
if($mailsent)
	 {
		 echo '<div id="success" class="success box">
                Thank you. We have received your message and will contact you back shortly.
               </div>';
	 }
	 else
	 {
		 echo '<div id="error" class="error box">
                Something went wrong. Please contact us via email or phone. We truly apologize for the inconvenience.
               </div>';
	 }
	 
	 
	 }
							
?>

Some web hosting providers have the mail() function disabled and you have to use SMTP calls instead - could this be the reason your mail() call isn’t working?

Yes, that’s what I was wondering.

@bhatpajju13 I tested your form code on my own server and it worked fine—email sent and all.

As an aside, though, I wouldn’t put that form online without adding some PHP security to it. At the moment, it’s an open door to criminals.