4

I have problem with sending mail message by php mail() function. I'm not sure if it's problem with code coz I have read that some hosting servers are not allowing to sends mail but I'm trying to send this mail also when website is on localhost and it still doesn't work - after click "Send" I see the information: "Your mail is sent", but when I'm checking on my postbox there is no mails (also in spam).

For me code looks good but maybe I'm missing something. The second option which I'm considering is that also my localhost is not allowing to send mails.

<form id="contact" action="mail.php" method="POST">
    <div class="field">
        <label class="fixed_width" for="name">Name:</label><input id="name" name="name" value="Name"/>
    </div>
    <div class="field">
        <label class="fixed_width" for="surname">Surname:</label><input id="surname" name="surname" value="Surname"/>
    </div>
    <div class="field">
        <label class="fixed_width" for="mail">E-mail:</label><input id="mail" name="mail" value="E-mail"/>
    </div>
    <div class="field" id="message">
        <label class="fixed_width" id="message_width" for="mail">Message:</label>
        <textarea id="message" name="message" />Type your message...</textarea>
    </div>
    <div>
        <input class="width" type="submit" value="Send" />
    </div>
</form>

<?php

    srand((double)microtime()*1000000);
    $marker = md5(uniqid(rand()));

    $receiver  = "address@gmail.com";
    $title = "Mail";
    $sender  = $_POST['name'];
    $sender .= $_POST['surname'];
    $sender_mail = $_POST['mail'];

    $message = $_POST['message'];

    $headers  = "From: $sender <$sender_mail>\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed;\n";
    $headers .= "\tboundary=\"___$marker==\"";

    $content ="--___$marker==\n";
    $content .="Content-Type: text/plain; charset=\"iso-8859-2\"\n";
    $content .="Content-Transfer-Encoding: 8bit\n";
    $content .="\n$message\n";

    if (mail($receiver,$title,$content,$headers))
    {
        print "Your message is sent.";
    } else {
        print "Your message is not sent.
        <br>Please go <a href=\"javascript:history.back();\">back</a> and send again.";
    }
?>

Pictures with my php conf:

PHP config PHP config PHP config

Priyanka Maurya
  • 385
  • 1
  • 10
Beacze
  • 534
  • 3
  • 8
  • 24
  • Do you have error reporting turned on? Have you tried a different e-mail than gmail? Is there a reason you are using boundaries (not necessary)? You don't appear to have an "-f" parameter, which helps deliverability... – random_user_name Feb 22 '14 at 23:06
  • 1
    If I were you, I'd change the 'value' attributes of your inputs to 'placeholder'. This will save you having to rely on JS to check if those fields have their values set to defaults and, if so, clearing them when they receive focus and setting them when they're empty on blur. Seeing as you've already given labels to these fields, setting values is redundant. Additionally, I'd put strip_tags() or filter_var() calls around your $_POST variables. (Don't ever trust user input without some form of sanitation.) – Agi Hammerthief Feb 22 '14 at 23:09
  • @cale_b OP has specified that this is on localhost. – Agi Hammerthief Feb 22 '14 at 23:09
  • I've never had luck sending e-mails from a localhost install... – random_user_name Feb 22 '14 at 23:10
  • It's localhost, I have tried also on other e-mail than gmail. – Beacze Feb 22 '14 at 23:15
  • I wonder if sendmail is installed or configured correctly on the server. –  Feb 23 '14 at 03:09
  • I also think that it can be problem with server but I don't know how to change it, above pictures with mym php conf: http://imageshack.com/a/img842/6421/z7fr.png http://imageshack.com/a/img842/4315/hjdv.png http://imageshack.com/a/img46/9827/qgkw.png – Beacze Feb 23 '14 at 13:42

2 Answers2

7

To test that sending of email works, try this really short program:

<?php
$email_to="address@gmail.com";
$email_subject="It works";
$email_message="Hello. I can send mail!";
$headers = "From: Beacze\r\n".
"Reply-To: address@gmail.com\r\n'" .
"X-Mailer: PHP/" . phpversion();
mail($email_to, $email_subject, $email_message, $headers);  
echo "mail sent!"
?>

I have used one "just like it" in the past as the starting point for testing a configuration. If this doesn't work it's most likely your server configuration.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • 1
    The same situation. I'm getting information "mail sent!" but on my post box is no mails. – Beacze Feb 22 '14 at 23:14
  • Have you tried refreshing you inbox? Sometimes it can take a while. Also, does the server you're sending through (eg, if using google's mail server) require authentication? – Agi Hammerthief Feb 22 '14 at 23:23
  • 1
    Yes, I have refreshed few times. No I hadn't any authentication. – Beacze Feb 22 '14 at 23:37
2

You can use SMTP (phpmailer)
Example:

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';

$mail->Host       = "mail.example.com"; // SMTP server example
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "username"; // SMTP account username example
$mail->Password   = "password";        // SMTP account password example

You can find more about PHPMailer here: https://code.google.com/a/apache-extras.org/p/phpmailer/

abe
  • 624
  • 8
  • 14
  • Hmm... I want to write only simple contact form, so I don't know from where I can take username and password. – Beacze Feb 22 '14 at 23:19
  • 2
    The username and password are for the smtp mail server through which you send the emails, if the mail server requires authentication. – Agi Hammerthief Feb 22 '14 at 23:24