如何使用PHPMailer

时间:2015-09-28 15:54:58

标签: php email file-upload phpmailer email-attachments

当我尝试通过附带文件的网站上的表单发送电子邮件时,我遇到了问题。 我正在使用PHPMailer,总是发送邮件,但总是没有附加文件。

我不知道应该修复或添加到我的代码中。

我一直在寻找一些教程,如何使用PHPMailer并附加文件。

代码如下:

HTML:

<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="name" style="width: 70%;" placeholder="Name"><br />
<input type="text" name="email" style="width: 70%;" placeholder="Email"><br />
<input type="text" name="phone" style="width: 70%;" placeholder="Phone"><br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<input type="submit" name="send" id="gobutton" value="Send">
</form>

PHP:

  <?
require_once("phpmailer/class.phpmailer.php");

if (isset($_POST['odeslat'])){
  $allowedExts = array("doc", "docx", "xls", "xlsx", "pdf");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "application/pdf")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "application/excel")
|| ($_FILES["file"]["type"] == "application/vnd.ms-excel")
|| ($_FILES["file"]["type"] == "application/x-excel")
|| ($_FILES["file"]["type"] == "application/x-msexcel")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|| ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))

&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "<script>alert('Error: " . $_FILES["file"]["error"] ."')</script>";
    }
  else
    {
        $d='upload/';
        $de=$d . basename($_FILES['file']['name']);
    move_uploaded_file($_FILES["file"]["tmp_name"], $de);
$fileName = $_FILES['file']['name'];
    $filePath = $_FILES['file']['tmp_name'];
     //add only if the file is an upload
     }
  }
else
  {
  echo "<script>alert('Invalid file')</script>";
  }

$to = $_POST['email'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$mail = new PHPMailer();



$mail->From = "info@mywebsite.com";
$mail->FromName = "My website";



$mail->addAddress("mail@mail.com");
$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);


$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
   header ("Location: /?e=1");
} 
else 
{
header ("Location: /?s=1");
}
}
?>

3 个答案:

答案 0 :(得分:1)

如果您不关心是否需要将文件保留在系统上,可以直接发送文件:

**HTML Code:**
<div ng-app="ssbApp" ng-controller="ssbCtrl">
    <input type="radio" name="showHideExample" ng-model="showHideExample" value="single" ng-click="showHideTest=true">Show
    <input type="radio" name="showHideExample" ng-model="showHideExample" value="multi" ng-click="showHideTest=false">hide
    <div ng-snow="showHideTest" ng-model="showHideTest">Test hide and show</div>
</div>

**Angular JS Code:**
var ssbApp = angular.module("ssbApp", []);
ssbApp.controller('ssbCtrl', function ($scope, $http) {
   $scope.showHideTest = false;
});

This subject has already been treated

答案 1 :(得分:0)

Send File Attachment from Form Using phpMailer and PHP

基本上,您需要上传到服务器的tmp名称和文件名

$_FILES['uploaded_file']['tmp_name']
$_FILES['uploaded_file']['name']

答案 2 :(得分:0)

你在做什么,以及在其他答案中提出的建议,是非常错误的。

如果您对此类事情一无所知,或许您应该先尝试reading the docs

直接使用$_FILES属性具有安全隐患,可以通过使用move_uploaded_file来减轻这种影响,您无需明白原因就可以避免这种情况。

an example provided with PHPMailer完全符合您的要求,但没有安全漏洞。用最重要的部分来解释:

if (array_key_exists('fileToUpload', $_FILES)) {
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['fileToUpload']['name']));
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile)) {
        $mail->addAttachment($uploadfile, 'My uploaded file');
    }
}

要在使用之前更全面地验证上传,请参阅PHP文档中的this comment