Hi folks,

just registered here in the hope to get some help. I'm new to PHP and have following problem: a simple form used to get the field inputs into my email box. But although i get a "successful" page upon sending, nothing drops in my mailbox.
Here's the full code:

<?
    // set your own preferences here
#############################################################
// Your email address
$youremail =  'my adres';
// Your web site title (John Doe's Site)
$websitetitle =  'website title';
// Path to "thanks for the message" page
$thankyoupage =  '....../Contact_success1.php';
// Send notification to sender (use false if not required)
$sendnotification = true;
   // Continue with your error checking, output of form, et cetera.
$contact_form_action = $_SERVER['PHP_SELF'];
if ((isset($_POST["sendcontact"])) && ($_POST["sendcontact"] == "contactsent")) {
    $contacter_form_error = array();
    if (empty($_POST['empresa_name'])){
        $contacter_form_error[] = 'favor preencher nome da empresa';
    }
    if (empty($_POST['contato_name'])){
        $contacter_form_error[] = 'favor preencher nome do contato';
    }
    if (empty($_POST['fone'])){
        $contacter_form_error[] = 'favor preencher número do telefone';
    }
    if (empty($_POST['contato_email'])){
        $contacter_form_error[] = 'favor preencher seu e-mail';
    }
    if (empty($_POST['produto'])){
        $contacter_form_error[] = 'favor preencher o produto';
    }
    if (empty($_POST['origem'])){
        $contacter_form_error[] = 'favor preencher origem';
    }
    if (empty($_POST['destino'])){
        $contacter_form_error[] = 'favor preencher destino';
    }
    if (empty($_POST['quantidade'])){
        $contacter_form_error[] = 'favor preencher quantidade';
    }
    if (empty($_POST['peso'])){
        $contacter_form_error[] = 'favor preencher peso';
    }
    if (empty($_POST['comprimento'])){
        $contacter_form_error[] = 'favor preencher comprimento';
    }
    if (empty($_POST['altura'])){
        $contacter_form_error[] = 'favor preencher altura';
    }
    if (empty($_POST['largura'])){
        $contacter_form_error[] = 'favor preencher largura';
    }
    else {
        $empresa_name = stripslashes($_POST['empresa_name']);
        $contato_email = stripslashes($_POST['contato_email']);

    $body =<<<EOB
Empresa: $_POST[empresa_name]
Contato: $_POST[contato_name]
Fone: $_POST[fone]
Fax: $_POST[fax]
Email: $_POST[email]
Produto: $_POST[produto]
Origem: $_POST[origem]
Destino: $_POST[destino]
Quantidade: $_POST[quantidade]
Peso: $_POST[peso]
Comprimento: $_POST[comprimento]
Altura: $_POST[altura]
Largura: $_POST[largura]
EOB;


        $subjectline = "$websitetitle | Orçamento de empresa";

   mail($contato_email, $subjectline, $body); 


        if($sendnotification == true) {
            $notification_message = "Obrigado por nos contatar, $contato_name. Recebemos sua mensagem e entraremos em contato em breve";
            $notification_subject = "Obrigado por sua mensagem para $websitetitle.";

            mail($contato_email, $notification_subject, $notification_message, "From: $youremail");;
        }
        header("Location:$thankyoupage");
    }
}
?>

Any help is appreciated

TIA

Luc

I don't see anywhere that your code checks to see if there is anything in the error array and display it if needed. It just goes on to attempt to send the mail and it also doesn't check the return value of the mail function. Make sure you are actually displaying any errors in your error message array and you may want to try to echo your mail parameters prior to sending, to make sure those look ok to you. Capture the return value from mail() and check that as well.

ezzaral, is this the error checking you refer to?

<?
// Print form field errors if present
if (count($contacter_form_error)>0){
    print '<p id="bottom"><strong>Algo está errado:</strong></p>'."\n";
    print '<ul>'."\n";
    foreach($contacter_form_error as $form_err) {
        print "<li class=\"error\">$form_err</li>\n";
    }
    print '</ul>'."\n";
}
?>

Yes, your first post did not show any code to check for those entries and went straight to the mail() statement.

Yeah, quick copy and paste lol... anyways it doesn't work, meaning i still get nothing in my mailbox.

Post the entire piece of code and use the code tags.

yes, also how do you have the mailserver set up? (you have got a mail server set up, right? youd be amazed the number of n00bs who just expect PHP to be able to magically send mail)

The following is the necessary code to store, check that no info was empty and email the user.
You will need to change the $to variable to suit your needs.
You will also need to make <input name="form_from"> for the Text Box that stores the users email address
And the same need applied to the subject and message input boxes.

<?php
if(isset($_POST['your_submit_button_name_here'])) {


    $from = $_POST['form_from'];
    $subject = $_POST['form_message'];
    $message = $_POST['form_subject'];

    if(strlen($from) > 0 AND strlen($subject) > 0 AND strlen($message) > 0) {
        if(emailUser($from, $subject, $message))
            echo "Email Sent";
        else
            echo "Email Not Sent";
    }


    function emailUser ($from, $subject, $message) {
    
        $to = 'you@youremail.com';            
        $headers = "From: " . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
        return mail ($to, $subject, $message, $headers);
    }

} else {
?>


<!-- Do your HTML Here -->



<?php
}
?>

Cheers.
Enjoy.

Hi folks,

about the server: i used my providers' one since it supports php fully.

But it's resolved now:
I had a variable set // Send notification to sender (use false if not required)
$sendnotification = true;

then I checked against that before sending. Since the variable appears to be always set to 'true' no need to 'if' it before sending, right? Since that variable is hard-coded (meaning the value isn't set by a form field, for example) then why 'if' it?

Taking the 'if' statement away from the mail functions worked :-)

but i'm confronted with a new problem: the data get's delivered, except the email from the sender:
Empresa: TEST
Contato: LUC
Fone: 989
Fax: 090
Email:
Produto: TEST
Origem: TEST
Destino: TEST
Quantidade: TEST
Peso: TEST
Comprimento: TEST
Altura: TEST
Largura: TEST

Also the notification mail to the sender leaves his name out:
Obrigado por nos contatar, (here should be his name). Recebemos sua mensagem e entraremos em contato em brev

The blank email is solved:
Changing Email: $_POST[email]

to

Email: $_POST[contato_email]

did the trick.

But i'm stumped why the notification mail to the sender leaves his name out:
Obrigado por nos contatar, (here should be his name). Recebemos sua mensagem e entraremos em contato em breve.

Taking the 'if' statement away from the mail functions worked :-)

The mail() function returns TRUE or FALSE upon sendin the email.
In order for you to write a decent script, you will have to verify that this mail was sent corrently.

Therefore you will need to wrap your mail() in an IF statement.
Otherwise your just firing an email out there and not getting any response back that it was successfull.

You can also do this to catch error messages instead of doing an if()

@mail(..);

The @ symbol would be an alternative to using the IF statement

Tnx for the reply but as i stated, everything works except for receiving the name of the contact in the body message of the e-mail.

Finally, it's solved:

I didn't define $contato_name.

So adding

$contato_name = $_POST['contato_name'];

did the trick :-)

Again, you need to validate your mail() command. By using @ or IF.

Done :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.