0

Please I need help with this. My contact form is not sending message to my gmail. I have checked my spam also and nothing came. Not sure if it's something to do with my code. Thanks in advance. I have realized that the message is going into my godaddy cpanel. The subject is

mail failure - malformed recipient address

The message body says

A message that you sent contained a recipient address that was incorrectly constructed: from: missing or malformed local part (expect word or "<") The message has not been delivered to any recipient.

Please also ignore the $ip field that part of my code wasn't added.

    <?php
$emailErr = "";
$commentErr = "";
if(isset($_POST['submit'])){
    //declares variable
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    if(empty($_POST['email'])){
        $emailErr = "Please enter your email";
    }
    if(empty($_POST['comment'])){
        $commentErr = "comment field can't be empty";
    }
}
if(!empty($_POST['email']) && !empty($_POST['comment'])){
    // Send the email
    $to = "myname@gmail.com";
    $email = "From: $email";
    $comment = "Message: $comment";
    $message = "$message" . "\n\n\n==-   Sent from the website with IP Address: " . $ip . "   -==";
    $headers = "From: $email,";
    $send_contact = mail($to,$email,$comment,$message,$headers);
    header("Location: index.php");
}
?>
chris85
  • 23,846
  • 7
  • 34
  • 51
mindblee
  • 55
  • 6
  • 4
    `$comment = $_POST[‘comment'];` That's not a single quote, it is an MS smart quote. You have them scattered throughout your code. Correct them all, turn on error reporting. – chris85 Jun 29 '15 at 00:03
  • are you really writing code in word? –  Jun 29 '15 at 00:05
  • look at the from header it wrong –  Jun 29 '15 at 00:06
  • plus `“` then `$headers = "From: $email,";` many syntax errors – Funk Forty Niner Jun 29 '15 at 00:10
  • you're also using 5 parameters in your mail function. – Funk Forty Niner Jun 29 '15 at 00:11
  • Sorry guys ignore the quotes, I didn't copy directly from my text editor. I copied it into ms before posting it here. The site wanted me to sort of indent the code properly – mindblee Jun 29 '15 at 00:15
  • Use Sublime 2 Text Editor. Its amazing for code. – Tech Savant Jun 29 '15 at 00:25
  • Are these form submissions your doing so you know for sure its broken? If it's coming from web, that's just normal junk you get. – Tech Savant Jun 29 '15 at 00:26
  • I'll pass on this one. Plus, you already posted a similar question http://stackoverflow.com/questions/30971535/php-form-processor-not-working – Funk Forty Niner Jun 29 '15 at 00:31
  • `$email = "From: $email";` and `$headers = "From: $email,";` makes your header say `From: From: $_POST['value']`, or something like that echo it. You also have 5 parameters in the mail function as previously noted. http://php.net/manual/en/function.mail.php – chris85 Jun 29 '15 at 00:39

2 Answers2

0

This should work.

<?php
    $emailErr = "";
    $commentErr = "";
    if(isset($_POST['submit'])){

        //declares variable
        $email = $_POST['email'];
        $comment = $_POST['comment'];

        if(empty($_POST['email'])){
           $emailErr = "Please enter your email";
        }
        if(empty($_POST['comment'])){
           $commentErr = "comment field can't be empty";
        }
     }

    if(!empty($_POST['email']) && !empty($_POST['comment'])){
        // Send the email
        $to = "myname@gmail.com";
        $title = "Message from my website";

        $message = "Comment: {$comment}" . "\r\n";
        $message .= "Sent from the website with IP Address: {$ip}" . "\r\n";

        $headers = "From: " . strip_tags($email) . "\r\n";
        $headers .= "Reply-To: ". strip_tags($email) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
        mail($to,$title,$message,$headers);

        header("Location: index.php");
   }
?>

I Advice you learn how to use the mail function here http://php.net/manual/en/function.mail.php

Olaitan Mayowa
  • 703
  • 6
  • 10
  • 1
    Thank you all for your contribution. @OlaitanMayowa- the code you gave me did the job. Thank you very much. – mindblee Jun 29 '15 at 01:37
0

Heyy Good Morning,

If you are working in offline mode maybe you need phpmailer.

First you need download phpmailer from here https://github.com/PHPMailer/PHPMailer/archive/master.zip

Then paste in your folder. If my coding doesn't clear you, you can check from

https://github.com/PHPMailer/PHPMailer

<?php
require 'PHPMailerAutoload.php'; // Your Path

$mail = new PHPMailer;

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

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // Your mail 
$mail->Password = 'secret';                           // Your mail password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;     

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML



$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

//Check Condition
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Second way.

If you working testing in online mode(Have own domain and hosting), you can just randomly copy and paste.

Doesnt required phpmailer.

<?php

$error = [];

$receipientName="Fido";
$receipientEmail ="receipientmail.gmail.com";
$ccEmail ="";

//declares variable
if(isset($_POST['name'])) $name = $_POST['name'];
else $name = "";

if(isset($_POST['email'])) $email = $_POST['email'];
else $email = "";



function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) {


    $headers = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $headers .= "X-Priority: 1\n";
    $headers .= "X-MSMail-Priority: High\n";
    $headers .= "X-Mailer: php\n";
    $headers .= "From: \"".$myname."\" <".$myemail.">\r\n";
    return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers));
}

if(isset($Submit) && $Submit=="Go") {

     $emailContent ='';


    $sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent);
    if($sent) {
      echo $emailContent;

        header('Location: contact.php');
    }else{
        echo "Failed";
        exit;
    }

}


?>
Fiido93
  • 1,918
  • 1
  • 15
  • 22