PHPMailer - AddAttachment不工作

时间:2014-01-17 10:11:03

标签: php html email phpmailer

我有一个网络表单,使用phpmailer函数将表单内容通过电子邮件发回给我。我正在尝试添加AddAttachment功能,但我似乎在php中存在问题。

这是我的html代码段:

<td>
    <div align="right">Add attachment :</div>
</td>
<td colspan="2">
    <input type="file" name="uploaded_file" id="uploaded_file" />
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
</td>

这是我的php;

<?php

require 'PHPMailerAutoload.php';

$mail = new PHPMailer();

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Host     = "mailer.********.local"; // SMTP server

$mail->From     = $_POST['email'];
$mail->AddAddress("frank********@gmail.com");

$mail->Subject = "Request for Contract Registration for " . $_POST['name'];
$mail->Body     = "Supplier number : " . $_POST['suppno'] . "\r\n";
$mail->Body     .= "Payee name : " . $_POST['name'] . "\r\n";
$mail->Body     .= "Address  : " . $_POST['add'] . "\r\n";
$mail->Body     .= "        : " . $_POST['add2'] . "\r\n";
$mail->Body     .= "             : " . $_POST['add3'] . "\r\n";
$mail->Body     .= "Nature of business : " . $_POST['nob'] . "\r\n";
$mail->Body     .= "Tax Ref : " . $_POST['rctref'] . "\r\n";
$mail->Body     .= "Description of works : " . $_POST['descofwks'] . "\r\n";
$mail->Body     .= "Start date of contract : " . $_POST['stdte'] . "\r\n";
$mail->Body     .= "End date of contract : " . $_POST['enddte'] . "\r\n";
$mail->Body     .= "Location of contract : " . $_POST['location'] . "\r\n";
$mail->Body     .= "Estimated value of contract : " . $_POST['contractval'] . "\r\n";
$mail->Body     .= "Confirm contract : " . $_POST['confirm'] . "\r\n";
$mail->Body     .= "Declaration : " . $_POST['declaration'] . "\r\n";
$mail->Body     .= "Department : " . $_POST['dept'] . "\r\n";

$mail->AddAttachment($_POST['uploaded_file']);

$mail->WordWrap = 50;

if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';

    header('Location: confirm.htm');
    }
?>

我的路径有问题吗???这很简单,我很遗憾,但如果有人能帮助我,我会非常感激! 提前感谢你, 坦率。

1 个答案:

答案 0 :(得分:1)

而不是使用

 $mail->AddAttachment($_POST['uploaded_file']); // WRONG

试试这个

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

上传的文件存储在临时文件夹中。 你应该从文件系统添加附件,为此使用$ _POST是错误的。