【问题标题】:PHPMailer - AddAttachment not workingPHPMailer - AddAttachment 不起作用
【发布时间】:2014-02-06 14:54:18
【问题描述】:

我有一个网络表单,它使用 phpmailer 功能将表单内容通过电子邮件发送给我。我正在尝试添加 AddAttachment 功能,但我似乎在 php.ini 中遇到了问题。

这是我的 html sn-p:

<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');
    }
?>

我的路径有问题吗???这可能是我想念的一些简单的事情,但如果有人能帮助我,我将不胜感激! 提前谢谢你, 弗兰克。

【问题讨论】:

标签: php html email phpmailer


【解决方案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 是错误的。

【讨论】:

  • 我搞定了。用过的; if(is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) { $file = $_FILES['uploaded_file']['name']; } $mail->AddAttachment($file);感谢您的所有帮助。
猜你喜欢
  • 2015-06-20
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
相关资源
最近更新 更多