文件上传字段附加文件到电子邮件

时间:2016-08-16 21:40:36

标签: php phpmailer

有一个带文件上传字段的简单HTML表单:

    <form action="<?php bloginfo('template_url'); ?>/demo-contacts.php" method="post" id="sky-form" class="sky-form" enctype="multipart/form-data">
      <header>Submit an <strong>Application!</strong></header>
      <fieldset>
        <div class="row">
          <section class="col col-6">
            <label class="label">Name</label>
            <label class="input"> <i class="icon-append icon-user"></i>
              <input type="text" name="name" id="name">
            </label>
          </section>
          <section class="col col-6">
            <label class="label">E-mail</label>
            <label class="input"> <i class="icon-append icon-envelope-alt"></i>
              <input type="email" name="email" id="email">
            </label>
          </section>
        </div>
        <div class="row">
          <section class="col col-6">
            <label class="label">Telephone Number</label>
            <label class="input"> <i class="icon-append icon-phone"></i>
              <input type="text" name="telephone" id="telephone">
            </label>
          </section>
          <section class="col col-6">
            <label class="label">Address</label>
            <label class="input"> <i class="icon-append icon-envelope-alt"></i>
              <input type="text" name="address" id="address">
            </label>
          </section>
        </div>
        <section>
          <label class="label">Subject</label>
          <label class="input"> <i class="icon-append icon-tag"></i>
          <input type="text" name="subject" id="subject" value="Technician Application">
          </label>
        </section>
        <section>
          <label class="label">Covering Letter</label>
          <label class="textarea"> <i class="icon-append icon-comment"></i>
            <textarea rows="4" name="message" id="message"></textarea>
          </label>
        </section>
    <div class="row">
          <section class="col col-6">
            <label class="label">Attach your CV</label>
            <label class="input"> <i class="icon-append icon-file"></i>
              <input type="file" name="cv" id="cv">
            </label>
          </section>
    </div>
    <input type="hidden" name="recipient" value="<?php the_field('contact_form_recipient'); ?>">
      </fieldset>
      <footer>
        <button type="submit" class="button">Send</button>
      </footer>
      <div class="message"> <i class="icon-ok"></i>
        <p>Your message was successfully sent!</p>
      </div>
    </form>

并使用以下PHP来处理在添加文件上载字段之前运行良好的表单:

$EmailFrom = "email address here";
$EmailTo = Trim(stripslashes($_POST['recipient']));
$Name = Trim(stripslashes($_POST['name'])); 
$Subject = Trim(stripslashes($_POST['subject'])); 
$Email = Trim(stripslashes($_POST['email'])); 
$Telephone = Trim(stripslashes($_POST['telephone'])); 
$Address = Trim(stripslashes($_POST['address'])); 
$Message = Trim(stripslashes($_POST['message'])); 
$CV = Trim(stripslashes($_POST['cv'])); 

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "CV: ";
$Body .= $CV;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL= URL here\">";
}
else{
  echo "failed";
}

目前,收到的电子邮件只是说明了上传文件的名称,我希望将该文件附在电子邮件中,有人可以指导我吗? / p>

由于

0 个答案:

没有答案