TDsouza
TDsouza

Reputation: 938

php mail notification

Ok I'm new to php coding and am working on a form mail php ( a form who's content's are mailed to an email address) the code works fine. Now I'd like to know if there's a way to notify the visitors about the success or failure of the mail. ( dynamically add a line to the form page which mentions "success" or "failure try again". Hope I've been clear enough.

Here's the code

    <?php

    $name = $_POST["name"]; 

    $company = $_POST["company"]; 

    $email = $_POST["email"]; 

    $contact =$_POST["contact"]; 

    $require = $_POST["requirement"]; 


    $message = "Name :".$name."\n"."Company  :".$company."\n"."Email id".$email."\n"."Contact no :".$contact."\n"."Requirement : ".$require;

    $subject ="Subject Matter Here";

    $to = "[email protected]";

    if(mail($to, $subject,$message)){
echo "We Received Your enquiry, We'll get back to you soon";
     } 
      else{ 
echo "there were some errors sending enquiry, please try again";
     }




      ?>

Upvotes: 3

Views: 5233

Answers (3)

baek
baek

Reputation: 450

You could try looking here and here

and use it like so:

if (!mail(...)) {// Reschedule for later try or panic appropriately!}

mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Upvotes: 0

Sjoerd
Sjoerd

Reputation: 75588

The mail function returns a boolean value indicating success:

$result = mail(...)
if ($result) {
    echo 'Success';
} else {
    echo 'Failure try again';
}

If mail() returns false, you know for sure the e-mail won't be sent. If mail() returns true, your mail server has accepted the e-mail message, but it may still not be delivered. Your mail server tries to send it to another mail server, which may reject the message. You will get a bounce message, but because this does not happen instantly you can not show the result on your form.

If you want to make (more) sure the e-mail is delivered, you have to connect to the destination host yourself. Get the MX record for the domain, connect to the STMP server and send it directly to the destination server.

Upvotes: 0

user399666
user399666

Reputation: 19879

$send = mail($to, $subject, $message);

if(!$send){
    echo 'Failed to send!';
}

The mail function:

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

Upvotes: 3

Related Questions