简体   繁体   English

phpMailer 不发送附件

[英]phpMailer not sending attachmnets

I have the following phpmailer function that works except that it's not attaching the attachment.我有以下 phpmailer 函数,但它没有附加附件。 The function does echo "found attachment".该函数确实回应了“找到的附件”。 Are there other settings that have to be tweaked to allow attachments?是否有其他设置需要调整以允许附件?

function mailerExpressBlueHostSWAG(array $mailInputs){   

    require_once '../include/phpmailer/PHPMailerAutoload.php';

    $mail = new PHPMailer();
    $mail->IsMail();   
    $mail->SetFrom('swag@sustainablewestonma.org');
    $mail->addAddress($mailInputs['addAddress']);   // use for production; 


    if(file_exists("../uploads/" . $mailInputs['name']))echo 'found attachment';   
    $mail->addAttachment("../uploads/" . $mailInputs['name']);

    $mail->addEmbeddedImage("../../../uploads/2019/09/newswagimageSmall.jpg", "swag-logo");

    $body = $mailInputs['body'] ;   
    $mail->Subject = $mailInputs['subject'] ;
    $mail->Body    = $body;
    $mail->IsHTML(true);
    $mail->ContentType="text/HTML";   

    if(1==1){
        if(!$mail->send()) {
            echo 'mail not sent <br>' ;
            return 'Message could not be sent.' . 'Mailer Error: ' . $mail->ErrorInfo;
        }else{
            echo 'mail sent 2<br>';
            return 'Message has been sent';
       }
   }
   $mail->ClearAddresses();   
}

Before anything else, you're using an old version of PHPMailer, so get the latest .首先,您使用的是旧版本的 PHPMailer,因此请获取最新的.

This code is somewhat confused:这段代码有点混乱:

if(file_exists("../uploads/" . $mailInputs['name']))echo 'found attachment';   
$mail->addAttachment("../uploads/" . $mailInputs['name']);

That condition will not apply to the addAttachment call because you've got no {} around it.该条件不适用于addAttachment调用,因为它周围没有{}

The main problem is that checking that a file exists doesn't mean you can read it.主要问题是检查文件是否存在并不意味着您可以读取它。 addAttachment returns true if it can read the file, so use that to check directly:如果addAttachment可以读取文件,则返回 true,因此可以直接使用它进行检查:

if (!$mail->addAttachment("../uploads/" . $mailInputs['name'])) {
    echo 'File could not be read';
}

If that complains, check ownership and permission son the file.如果出现问题,请检查文件的所有权和权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM