0

Am trying to send mail from my html website using php function but didn't receive mail when website is in live. Is this correct way SMTP connection & remaining code ? Please help me to solve this. HTML Code(index.html)

    <div class="done">
    <div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert">×</button>
            Your message has been sent. Thank you!
        </div>
    </div>
    <form method="post" action="contact.php" id="contactform">
        <div class="form">
            <input class="col-md-12" type="text" name="name" placeholder="Name">
            <input class="col-md-12" type="text" name="email" placeholder="E-mail">
            <textarea class="col-md-12" name="comment" rows="3" placeholder="Message"></textarea>
            <input type="submit" id="submit" class="btn btn-default" value="Send">
        </div>
    </form>

PHP Code (contact.php)

<?php
    ob_start();

    $to = "username@gmail.com";
    $subject = "Quick Message- Subject";

    $Name=$_POST["name"];
    $Email=$_POST["mail"];
    $Message=$_POST["message"];

    $message = "Name : ".$Name.PHP_EOL.
    "Email Id : ".$Email.PHP_EOL.
    "Message : ".$Message.PHP_EOL;                  

    $from = "username@gmail.com";

    $headers = "From : " . $from;
    mail($to,$subject,$message,$headers);

    header('Location: http://newhtmlwebsite.net'); 
?>
Sri
  • 159
  • 1
  • 6
  • 14

1 Answers1

1

You're mixing PHPMailer with the default php mailer.

First:

On your $mail variable from PHPMailer You're failing to add the address. This is done like the following:

$mail->addAddress('username@gamil.com', 'User Name');

You're also failing to add the subject to the $mail object

$mail->Subject = $subject;
$mail->Body = $Message;

Also the $mail From

$mail->From = 'your-mail-addres@gmail.com';

And finally, do remove the line

 $result=mail($to,$subject,$message,$header);

That line by itself is sending an email using the simple mail() function (not recommended)

Do remember to apply some kind of filtering to your $_POST variables before adding them straight into a mail

For more info on this visit: PHP Variable Filtering

Sandman21dan
  • 100
  • 1
  • 9
  • Hi Thanks for your time, I edited php code can you check now that code is works ?? – Sri Jun 10 '15 at 11:17
  • The code I see on your question is still the same, so it doesn't work yet. – Sandman21dan Jun 10 '15 at 11:19
  • Sorry can you check now.. It updated now... in that php code from mail id & to mail id are same... is that any problem occurs ? – Sri Jun 10 '15 at 11:20
  • Well, you've got rid of the PHPMailer which yields better results, but should the code as you have it should be working, Not sure why you have an output buffer `ob_start();` at the beginning of your file. And be aware that this kind of mail sent by the `mail()` function has a tendency to go to the Spam folder quite often. So remember to check that. – Sandman21dan Jun 10 '15 at 11:29