0

This is my php code to send email. I got the massage 'Message was sent, you can send another one' But email is not sending.

     <h4>Please fill out the following form and we will be in touch with you soon.</h4>
<form action="mytest.php" method="post" id="contactform">
  <ol>
    <li>
      <label for="name">Your Name <span class="red">*</span></label>
      <input id="name" name="name" class="text" />
    </li>
    <li>
      <label for="email">Your email <span class="red">*</span></label>
      <input id="email" name="email" class="text" />
    </li>
    <li>
      <label for="subject">Subject</label>
      <input id="subject" name="subject" class="text" />
    </li>
    <li>
      <label for="message">Message <span class="red">*</span></label>
      <textarea id="message" name="message" rows="6" cols="50"></textarea>
    </li>
    <li class="buttons">
      <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
      <div class="clr"></div>
    </li>
  </ol>
</form> 


<?php

if(!$_POST) exit;

$email = $_POST['email'];

$errors=0;

if($errors==1) echo $error;
else{
    $email_from = $_POST['email'];
$email_from = "from@gmail.com";
$headers = "From: " . strip_tags( $_POST['name'] ) . "\r\n";

$mail_to_send_to = "to@gmail.com";
$your_feedbackmail = "from@gmail.com";
$sendflag = 'send';                       
if ( $sendflag == "send" )
        {
                $email = $_REQUEST['email'] ;
                $message = $_REQUEST['message'] ;
                $headers = "From: $your_feedbackmail" . "\r\n" . "Reply-To: $email" . "\r\n" ;
                $a = mail( $mail_to_send_to, "Feedback Form Results", $message, $headers );
                if ($a)
                {
                     print("Message was sent, you can send another one");
                } else {
                     print("Message wasn't sent, please check that you have changed emails in the bottom");
                }
        }
}
?>

I'm Using Cpanel to host my web site. Is there any special configurations to do this? I'm new to php. Please help me.

amnk
  • 66
  • 1
  • 9
  • Keep in mind in mail function, even if the email was accepted for delivery, it does NOT mean the email is actually sent and received! and it will return address to `$a` so you are getting message that message sent – Abbas Nov 26 '15 at 05:36
  • Do check your spam folder once.. where the email is being received... You will get $a as true as @AbbasGabru said even if the mail is accepted for delivery. – Akshit Arora Nov 26 '15 at 05:55

2 Answers2

1

mail function doesn't provide authentication functionality. Your have to use Mail class from Mail Pear package. See here for an example:

example

Codeone
  • 1,173
  • 2
  • 15
  • 40
0

I am assuming you are on shared hosting based on the fact that you are using Cpanel, some shared hosting solutions don't play nicely with php's mail() function, I have found that using phpmailer https://github.com/PHPMailer/PHPMailer works better and provides more functionality

to use phpmailer : download from the link, place the files in your a folder accessible to your web application.

code :

$email_from = $_POST['email'];
$email_from = "from@gmail.com";  // this overwrites the $_POST['email'] value, check this
$email_from_name = "Nishanthi";
$gmailUsername = "from@gmail.com";
$gmailPassword = "mysecretpassword";
$mail_to_send_to = "to@gmail.com";
$your_feedbackmail = "from@gmail.com";

$emailSubject = "Place Subject Here";
$emailContent = "This message can contain <b>HTML</b>";

require '[path_to_your_phpmailer_files]/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP 
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;                                 //Enable SMTP debugging 
$mail->Debugoutput = 'html';                          //Ask for HTML-friendly debug output
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = $gmailUsername;                 // SMTP username
$mail->Password = $gmailPassword;                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom($email_from, $email_from_name);
$mail->addAddress($mail_to_send_to);       
$mail->addReplyTo($your_feedbackmail);

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = $emailSubject;
$mail->Body    = $emailContent; 

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message was sent, you can send another one';
}

?>
  • Thanks for your answer. I used your code.I have PHPMailer, PHPMailerOAuth and PHPMailerOAuthGoogle files. Now i got this error Fatal error: Class 'PHPMailer' not found in C:\wamp\www\mytest\PHPMailerOAuth.php on line 27 – amnk Nov 26 '15 at 06:35
  • 1
    Can you post the php code as well as the folder structure so that I can better understand what is causing the error please – Craig Bezuidenhout Nov 26 '15 at 08:56
  • Here the folder Structure index.php contact.php PHPMailer.php PHPMailerOAuth.php PHPMailerOAuthGoogle.php – amnk Nov 26 '15 at 09:39
  • I don't see a file named : PHPMailerAutoload.php in there ? – Craig Bezuidenhout Nov 26 '15 at 12:57
  • thank for replying me. I just Added it now. But same error shows – amnk Nov 26 '15 at 13:19
  • I added class.pop3,class.smtp also. But same error – amnk Nov 26 '15 at 13:22
  • I would suggest downloading PHPmailer from sourcefourge again as a zip, extract it to a subfolder in you web application then using the code I place above and test again. – Craig Bezuidenhout Nov 26 '15 at 13:30