PHPMailer文件附件

时间:2016-06-27 03:20:21

标签: php html web phpmailer contact-form

我一直在一个有联系表格的网站上工作。一切都工作正常,但附件没有邮寄。以下是HTML和PHP文件:

(这是我第一次在这个网站上提问,我为这种可怕的格式道歉,提前谢谢)

<? php
require_once 'phpmailer/PHPMailerAutoload.php';

if (isset($_POST['name'])) {
  $name = $_POST['name'];
}
if (isset($_POST['email'])) {
  $email_address = $_POST['email'];
}
if (isset($_POST['phone'])) {
  $phone = $_POST['phone'];
}
if (isset($_POST['comment'])) {
  $message = $_POST['comment'];
}

$body_message = '<p>You have a new message from your website contact form.</p> </br> </br> <p> Here are the deatils:</p> </br> </br> <p>Name: </p>'.$name.
'<p>Email: </p>'.$email_address.
'<p>Phone: </p> '.$phone.
'</br> <p>Message: </p> </br>'.$message;


$mail = new PHPMailer;
$mail - > IsSMTP();
$mail - > Host = 'mail.ronaldtest.com';
$mail - > Username = 'Ronald@ronaldtest.com';
$mail - > Password = '151588';
$mail - > Port = 587;
$mail - > addAddress('ronaldng1588@gmail.com');

$mail - > setFrom('ronald@ronaldtest.com', 'Brighten Management Auto Mailer');

$mail - > Subject = 'Website Contact Form: $name';
$mail - > Body = $body_message;

if (isset($_FILES['attachmentFile']) && $_FILES['attachmentFile']['error'] == UPLOAD_ERR_OK) {
  $mail - > AddAttachment($_FILES['attachmentFile']['tmp_name'],
    $_FILES['attachmentFile']['name']);
}


$mail - > IsHTML(true);


if ($_POST["submit"]) {
  if (!$_POST['name']) {
    $error = "<br />Please enter your name";
  }
  if (!$_POST['email']) {
    $error. = "<br />Please enter your email address";
  }
  if (!$_POST['phone']) {
    $error. = "<br />Please enter your phone number";
  }
  if (!$_POST['comment']) {
    $error. = "<br />Please enter a comment";
  }
  if ($error) {
    $result = '<div class="alert alert-danger" There were error(s)
            in your form: '.$error.
    '</div>';
  } else {
    if (!$mail - > Send()) {
      $result = '<div class="alert alert-danger">Sorry, there was
                    an error sending your message. Please try again later.</div>';
    } else {
      $result = '<div class="alert alert-success">Thank you, <strong>'.$_POST['name'].
      '</strong>!   I will be in touch!</div';;
    }
  }
}


?>
<section id="contact">

  <? php include ( 'email.php'); ?>

  <div class="container">
    <div class="row">
      <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="feature_header text-center">
          <h3 class="feature_title">Keep In <b>touch</b></h3>
          <h4 class="feature_sub">Contact us for future improvements</h4>
          <div class="divider"></div>
          <?php if (isset($result)){ echo $result; } ?>
        </div>
      </div>

      <div class="row">
        <div class="contact_full">

          <form method="post">
            <div class="col-md-6 left">
              <div class="left_contact">
                <div class="control-group">
                  <div class="form-level controls">
                    <input type="text" name="name" class="input-block" placeholder="Your Name" value="<?php 
                                            if (isset($_POST[" name "])){
                                                echo $_POST['name']; }?>"/>
                    <span class="form-icon fa fa-user"></span>
                  </div>

                </div>

                <div class="control-group">
                  <div class="form-level controls">
                    <input type="email" name="email" class="input-block" placeholder="Your Email" value="<?php 
                                            if (isset($_POST[" email "])){
                                                echo $_POST['email']; }?>" />
                    <span class="form-icon fa fa-envelope-o"></span>
                  </div>
                </div>

                <div class="control-group">
                  <div class="form-level controls">
                    <input type="phone" name="phone" class="input-block" placeholder="Your Phone Number" value="<?php 
                                            if (isset($_POST[" phone "])){
                                                echo $_POST['phone']; }?>" />
                    <span class="form-icon fa fa-phone"></span>
                  </div>

                </div>

              </div>
            </div>

            <div class="col-md-6 right">
              <div class="control-group">
                <div class="form-level controls">
                  <input type="file" name="attachmentFile" id="attachmentFile" value="<?php echo $_FILES['attachmentFile']['tmp_name'] ?>" />
                  <span class="fa fa-file"></span>
                </div>
              </div>
              <div class="control-group">
                <div class="form-level controls">
                  <textarea class="textarea-block" name="comment" rows="4" value="<?php 
                                            if (isset($_POST[" comment "])){
                                                echo $_POST['comment']; }?>"></textarea>
                  <span class="form-icon fa fa-pencil"></span>
                </div>
              </div>
            </div>

            <div class="col-md-12 text-center">

              <input type="submit" name="submit" class="btn btn-main featured" value="Submit Now" />
            </div>

          </form>

        </div>
      </div>


    </div>
  </div>
</section>

1 个答案:

答案 0 :(得分:0)

要上传文件的表单,必须在表单标记中包含enctype="multipart/form-data"。所以在你的情况下:

 <form method="post" enctype="multipart/form-data">