本文介绍了PHPMailer - AddAttachment不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络表单,使用phpmailer函数将表单内容发回给我。我试图添加一个AddAttachment功能,但我似乎有一个问题在PHP。



这是我的html片段:

 < td> 
< div align =right>添加附件:< / div>
< / td>
< td colspan =2>
< input type =filename =uploaded_fileid =uploaded_file/>
< input type =hiddenname =MAX_FILE_SIZEvalue =10000000/>
< / td>

这是我的php;

 <?php 

require'PHPMailerAutoload.php';

$ mail = new PHPMailer();

$ mail-> IsSMTP(); //告诉班级使用SMTP
$ mail-> Host =mailer ********。local; // SMTP server

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

$ mail-> Subject =请求合同注册。 $ _ POST [名];
$ mail-> Body =供应商编号:。 $ _POST ['suppno']。 \r\\\
;
$ mail-> Body。=收款人姓名:。 $ _POST ['name']。 \r\\\
;
$ mail->正文。=地址:。 $ _POST ['add']。 \r\\\
;
$ mail-> Body。=:。 $ _POST ['add2']。 \r\\\
;
$ mail-> Body。=:。 $ _POST ['add3']。 \r\\\
;
$ mail-> Body。=业务性质:。 $ _POST ['nob']。 \r\\\
;
$ mail-> Body。=Tax Ref:。 $ _POST ['rctref']。 \r\\\
;
$ mail-> Body。=作品描述。 $ _POST ['descofwks']。 \r\\\
;
$ mail-> Body。=合同开始日期:。 $ _POST ['stdte']。 \r\\\
;
$ mail-> Body。=合约日期:。 $ _POST ['enddte']。 \r\\\
;
$ mail-> Body。=合同位置:。 $ _POST ['location']。 \r\\\
;
$ mail-> Body。=合约预估价值。 $ _POST ['contractval']。 \r\\\
;
$ mail-> Body。=确认合同:。 $ _POST ['confirm']。 \r\\\
;
$ mail-> Body。=声明:。 $ _POST ['declaration']。 \r\\\
;
$ mail-> Body。=Department:。 $ _POST ['dept']。 \r\\\
;

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

$ mail-> WordWrap = 50;

if(!$ mail-> Send()){
echo'消息未发送。
echo'Mailer error:'。 $ MAIL-> ERRORINFO;
} else {
echo'已发送消息。

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

我的路径有问题吗?这很简单,我失踪了,但如果有人可以帮助我,我会非常感激!
提前感谢您,
Frank。

解决方案

而不是使用

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

尝试这个



<$ p $如果(isset($ _ FILES ['uploaded_file'])&&b $ b $ _FILES ['uploaded_file'] ['error'] == UPLOAD_ERR_OK){
$ mail-> AddAttachment($ _ FILES ['uploaded_file'] ['tmp_name'],
$ _FILES ['uploaded_file'] ['name']);
}

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


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.

解决方案

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.

这篇关于PHPMailer - AddAttachment不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:54