Click to See Complete Forum and Search --> : calling mail() function


awebb
09-10-2003, 10:38 AM
I have a function that should mail me the contents of a form when triggered by onSubmit"".
The mail arrives (twice actually) but is empty ie only the subject titles are shown.
Is the function in the wrong place or am I calling it wrong?
Heres the code above the head and just underneath the code I am using for onSubmit"".

<?php
function sendMailInfo()
{
/* emailing info, first set variables*/
$title=$HTTP_POST_VARS['title'];
$fname=$HTTP_POST_VARS['firstname'];
$lname=$HTTP_POST_VARS['lastname'];
$address1=$HTTP_POST_VARS['address1'];
$address2=$HTTP_POST_VARS['address2'];
$towncity=$HTTP_POST_VARS['towncity'];
$postcode=$HTTP_POST_VARS['postcode'];
$country=$HTTP_POST_VARS['country'];
$telephone=$HTTP_POST_VARS['telephone'];
$email=$HTTP_POST_VARS['email'];
$startdate1=$HTTP_POST_VARS['startdate1'];
$startdate2=$HTTP_POST_VARS['startdate2'];
$startdate3=$HTTP_POST_VARS['startdate3'];
$startdate4=$HTTP_POST_VARS['startdate4'];
$adults=$HTTP_POST_VARS['adults'];
$children=$HTTP_POST_VARS['children'];
$catering=$HTTP_POST_VARS['catering'];
$comments=$HTTP_POST_VARS['comments'];

$to="webmaster@chinchall.com, admin@chinchall.com";
$subject="Clouseau Booking";
/* The body text */
$content= "Name: ".$title." ".$fname." ".$lname."\r\n"
."Address: ".$address1." ".$address2."\r\n"
."Address: ".$towncity." ".$postcode." ".$country."\r\n"
."Telephone: ".$telephone."\r\n"
."email: ".$email."\r\n"
."Week1: ".$startdate1."\r\n"
."Week2: ".$startdate2."\r\n"
."Week3: ".$startdate3."\r\n"
."Week4: ".$startdate4."\r\n"
."Adults: ".$adults."\r\n"
."Children: ".$children."\r\n"
."Catering: ".$catering."\r\n"
."Comments: ".$comments."\r\n";
/* send the info */
mail ($to, $subject, $content);
}

and I am calling it in the form with:

onSubmit"<?php sendMailInfo();?>"

Any suggestion gratefully recieved.
Andy

pyro
09-10-2003, 10:42 AM
It looks like you would benefit from a loop. Take a look at http://www.webdevfaqs.com/php.php#mailer

awebb
09-10-2003, 11:02 AM
Thanks for the loop idea it looks useful BUT...
the whole form tag currently looks like this:

<form action="<?php echo $editFormAction; ?>" method="POST" name="form" onsubmit="<?php sendMailInfo();?>">

So what I am trying to do is not only populate the db (with the $editFormAction) but also send the info by mail at the same time to various email addresses.

I am getting the email, but all the fields are empty!

Any other ideas?
Andy

pyro
09-10-2003, 11:17 AM
Try commenting out the mail() call, and echoing $content and see what you get.

awebb
09-10-2003, 11:38 AM
Ok, I tried that but it didn't work, perhaps I didn't do it right.

I have some succes with removing the function parameter (sendMailInfo()) and changing the onSubmit to:

onSubmit"<?php echo mail();?>"

Now I get two emails, one is empty the other is correctly filled, so a partial succes.

Is there a way of avoiding the empty email?

pyro
09-10-2003, 11:43 AM
In the $to line, you have two addresses. Is one getting it correctly filled out and the other one incorrectly filled out?

awebb
09-10-2003, 11:50 AM
Both addresses are getting two emails, one empty the other correct.

I have a feeling its because the form, or the mail(), or the info is being sent twice, once before the db (or the $variables=$HTTP_POST_VARS['varaible']) is populated and then again once it has been.

Bit of a head scratcher...

pyro
09-10-2003, 12:05 PM
What does the forms action do? I'm guessing the sendMailInfo function is being called more that once...

awebb
09-10-2003, 12:20 PM
The form tag is as follows:

<form action="<?php echo $editFormAction; ?>" method="POST" name="form" onsubmit="<?php echo mail();?>">

$editFormAction populates the MySQL db. This is a piece of code generated by DreamweaverMX (don't shoot me, I'm doing my best, honest).

I did try changing the action from the above to:

action="<?php echo $editFormAction; mail();?>"

and removing the onSubmit"" value.

I still got the two emails, one empty, one correct.

pyro
09-10-2003, 12:46 PM
I think you are going to need to do things a bit differently.

The forms action should not point to a PHP function, or probably even a variable, unless the variable points to a PHP file. What you are going to want to do is point the forms action to the PHP file that writes to the DB and sends the email to you.

awebb
09-10-2003, 12:49 PM
OK,
Thanks for the suggestions, I'll go and rethink the method.
Thanks for your time.
Andy

pyro
09-10-2003, 12:51 PM
You bet, I was happy to help... :)