问题
I have a web form which emails the form content back to me using the phpmailer function. I'm trying to add an AddAttachment feature but I seems to have an issue in the php.
This is my html snippet:
<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>
And this is my 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');
}
?>
Is there an issue with my path??? It's probally something simple that I'm missing but if anyone can help me I'd greatly appreciate it! Thanking you in advance, Frank.
回答1:
Instead of using
$mail->AddAttachment($_POST['uploaded_file']); // WRONG
try this
if (isset($_FILES['uploaded_file']) &&
$_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['uploaded_file']['tmp_name'],
$_FILES['uploaded_file']['name']);
}
Uploaded files are stored in the temp folder. You should add the attachment from the filesystem, it is a mistake to use $_POST for this.
来源:https://stackoverflow.com/questions/21183147/phpmailer-addattachment-not-working