使用phpmailer以联系表单文件附件

时间:2015-02-17 06:58:16

标签: php file email attachment phpmailer

我尝试在联系表单中输入文件,以便在使用phpmailer发送时将文件作为附件附加到电子邮件中。下面的代码没有显示任何错误,但它只是留在空白的php页面上,并没有发送电子邮件。提前致谢。

<form action="send-mail-tutor.php"  method="post" class="form-inline contact-form" entype="multipart/form-data">
  <div class="form-group col-sm-10">
    <label for="inputName1">Name</label>
    <input type="text" class="form-control" name="name" id="name" placeholder="Enter first and last name">
  </div>

  <div class="form-group col-sm-6">
    <label for="inputEmail1">Email address</label>
    <input type="email" class="form-control" name="email" id="email" placeholder="Enter email">
  </div>

  <div class="form-group col-sm-6">
    <label for="inputPhone1">Phone Number</label>
    <input type="tel" class="form-control" name="phone" id="phone-number"  placeholder="Enter phone number" maxlength="10">
  </div>

  <div class="form-group">
    <label for="resume">Resume</label>
    <input type="file" id="resume" name="resume">
  </div> <br/>

  <button type="submit" class="btn btn-default">Submit</button>
</form>



  <?php

if(isset($_POST['submit'])) {

$emailAddress = "ronniehung95@gmail.com";

// require_once "class.phpmailer.php";

$msg = 'Name: '.$_POST['name'].'<br />  Email: '.$_POST['email'].'<br /> Phone: '.$_POST['phone'];

if (array_key_exists('resume', $_FILES)) {
// first handle the upload
// don't trust provided filename - same goes for MIME types
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['resume']['name']));
if (move_uploaded_file($_FILES['resume']['tmp_name'], $uploadfile)) {
// upload handled successfully
// now create a message
// This should be somewhere in your include_path
require_once 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsMail();

$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->addAddress($emailAddress);
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->Subject = "New Tutor Applicant";
$mail->msgHTML($msg);
// attach the uploaded file
$mail->addAttachment($uploadfile, 'resume');
$mail->send();

} echo "<script> window.location=../message-sent.php; </script>";
} 
}

0 个答案:

没有答案