954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Newbie seeks urgent help

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 =<<

Any help is appreciated

TIA

Luc

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

ezzaral, is this the error checking you refer to?

<?
// Print form field errors if present
if (count($contacter_form_error)>0){
print 'Algo está errado:


'."\n";
print ''."\n";
foreach($contacter_form_error as $form_err) {
print "$form_err\n";
}
print ''."\n";
}
?>

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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)

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

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 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.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

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

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 
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

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

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.

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Finally, it's solved:

I didn't define $contato_name.

So adding

$contato_name = $_POST['contato_name'];


did the trick :-)

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Done :-)

Luc
Newbie Poster
11 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You