Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send mail by php mail() function [duplicate]

Tags:

html

php

email

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  = "[email protected]";
    $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 configPHP configPHP config

like image 724
Beacze Avatar asked Jan 11 '23 03:01

Beacze


2 Answers

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

<?php
$email_to="[email protected]";
$email_subject="It works";
$email_message="Hello. I can send mail!";
$headers = "From: Beacze\r\n".
"Reply-To: [email protected]\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.

like image 198
Floris Avatar answered Jan 16 '23 20:01

Floris


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/

like image 41
abe Avatar answered Jan 16 '23 21:01

abe