PHPmailer: Send from form
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开发者_StackOverflow中文版" required autofocus>
</li>
<li>
<label for=email>Email</label>
<input id="email" name="email" type=email placeholder="example@domain.com" 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 = "xxxxxx@gmail.com";
$mail->FromName = "Your Name";
$mail->AddAddress("xxxxxxx@gmail.com");
$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'] ;
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" >
It appears that you're setting the $comments variable after sending the message.
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.
精彩评论