tags:

views:

26

answers:

3

Alright, I have my form (first snippet of code), and I am trying to use PHPmailer to send it. However, it sends the message without any of the information from the actual form. I am pretty lost with how to get this to work.

<form action="send_form_email.php" method="post" id="ContactForm">
                    <fieldset>
              <p class="email">magazines/newspapers</p>
                        <ol>
                            <li>
                                <label for=name>Name</label>
                                <input id="name" name="name" type="text" placeholder="name" required autofocus>
                            </li>


                            <li>
                                <label for=email>Email</label>
                                <input id="email" name="email" type=email placeholder="[email protected]" required>
                            </li>

                            <li>
                                <label for=telephone>Phone</label>
                                <input id=telephone name=telephone type=tel placeholder="Eg. 888-555-5555" required>
                            </li>

                            <li>
                            <label for="comments">note</label> 
                <textarea name=comments type=text placeholder="enter your comments" required></textarea>
                            </li>

                            <li>
                            <label for="file">File</label>
                            <input id="file" type="file" name="file" />
                            </li>

                        </ol>
                    </fieldset>

              <fieldset>
                        <button type=submit>submit</button>
                    </fieldset>

                </form>

Mail Script:

require("mail/class.phpmailer.php");

$mail = new PHPMailer();
$mail->Host = "localhost";               
$mail->From = "[email protected]";
$mail->FromName  =  "Your Name";
$mail->AddAddress("[email protected]");


$mail->Subject = "Feedback form results";
$mail->Body = $comments;
$mail->WordWrap = 50;

if(!$mail->Send())
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Thank you for your feedback.';
}
  $email = $_REQUEST['email'] ;
  $comments = $_POST['telephone'] ;
    $phone = $_REQUEST['comments'] ;
  $message = $_REQUEST['message'] ;
A: 

It appears that you're setting the $comments variable after sending the message.

ngroot
O, thanks.. Very dumb question. But how do I send multiple "variables". Right now $comments is set to telephone. But I do need email, telephone, comments, and even a file upload.
Louis Stephens
What do you want the resulting email to look like for a given email address, telephone #, comments, etc. to look like?
ngroot
Nameemailnumbercommentfile attachment
Louis Stephens
So, as Rudi noted below, just set the body to that:`$mail->Body="Name: $_POST['name'] Email: $_POST['email'] Number: $_POST['telephone'] Comments: $_POST['comments']";`Unless you want to stick the contents of the uploaded file into the message inline, you'd probably want to add a call after setting the body along the lines of `$mail->AddAttachment($_FILES['file']['tmp_name']);`
ngroot
A: 

You're setting the variables after trying to send the mail. Also, I don't see a form input named message. However, I do see one named file though the form's enctype is not set. So there are many errors that need fixing.

Not sure what you're trying to do but it seems you don't need both comments and message. Remove message from the sending script and remove file from the html form to see if you can get it working like this. Also move the variable assignments from the bottom of the sending script to the top.

webbiedave
Thanks webbiedave. I did move it up to the top and that fixed everything. Well, it does send the email with the $comments data. However, I need email, telephone, as well as a file (using a file upload input) to be sent as well. Sorry, I am kind of new to all of this.
Louis Stephens
+1  A: 

Ok so step one (optional) is to collect the posted variables into local variables - BEFORE you get into the $mail=new PHPMailer()... bit. This isn't necessary for the limited code fragment you provide, but you might use them somewhere else.

$name = $_POST['name'] ;
$email = $_REQUEST['email'] ;
$telephone = $_REQUEST['telephone'] ;
$comments = $_POST['comments'] ;

And now, change the $mail->Body = $comments; line to:

$mail->Body="
Name: $name
Email: $email
Telephone: $telephone
Comments: $comments";

And as ngroot points out; to add an attachment:

$mail->AddAttachment($_FILES['file']['tmp_name']);

... which you can call multiple times for multiple attachments. Because of the way form-uploads work (files get stored in a temporary space) you need to use this tmp_name sub variable. You'll also need to add multipart form encoding to allow file uploads, so the form line should read:

<form enctype="multipart/form-data" action="send_form_email.php" method="post" id="ContactForm"  >
Rudu