Phpmailer代码在函数内不工作


phpmailer code not working when within function

我在一个函数中有下面的代码。然而,它从不添加附件到电子邮件。邮件收到了,但是没有附件。

然而,如果我从函数中删除代码并添加一个单独的文件(不含函数部分),并在同一目录中,它可以正常工作。

为什么在函数中不添加附件?

function resendOrder()
{
    //global $siteEmailFrom, $siteEmailName, $dir;
    require_once('OrderMailer/class.phpmailer.php');// need this to send email
    $mail = new PHPMailer();
    // Now you only need to add the necessary stuff
    // HTML body
    $body = "Testing";
    // And the absolute required configurations for sending HTML with attachement
    $mail->From      = "mark@******.co.uk"; 
    $mail->AddAddress("mark@******.co.uk", "My-webpage Website");
    $mail->Subject = "test for phpmailer-3";
    $mail->MsgHTML($body);
    $mail->AddAttachment("ploxy.jpg");
    if(!$mail->Send()) {
        echo "There was an error sending the message";
        exit;
    }
    else{
        echo "Message was sent successfully";
    }
}

不能引用附件"ploxy.jpg"的名称,即:不能引用文件名

你必须引用临时文件名,你可以这样做

$_FILES['ploxy']['tmp_name']; // temp_name can be anything

更准确地说:

$filePath = "../images/"; // Path to image, can be the empty string.
$ploxy = $_FILES['ploxy']['tmp_name'];
$mail->AddAttachment($filePath, $ploxy);

基本上:你必须上传你的文件到脚本之前,你可以发送它;http://php.net/manual/en/features.file-upload.php