PHPMailer发送没有附件的邮件

时间:2017-09-07 10:17:57

标签: php email phpmailer

我使用PHPMailer类从我的在线服务器发送电子邮件。

此邮件包含附件或多附件。

由于某些原因,我无法通过邮件收到附件。

我只收到包含没有附件的信息的电子邮件。

这是我的代码:

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->From = $email;
$mail->FromName = $staffname;
$mail->addAddress($email,$staffname);
for($i=0;$i<count($files);$i++){      
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT'],"/$path".$_FILES['myfile']['name'][$i]);     
}
//Filename is optional

//Provide file path and name of the attachments
$mail->isHTML(true);
$mail->Subject = "New Task";
$message = "<h1 align='center'>YOU HAVE A NEW TASK</h1>
    <table  width='100%' border='1'>
    <tr bgcolor='#f7ac01' align='center'>
    <td>Date</td>
    <td>Description</td>
    <td>Status</td>
    <td>Type</td>
    <td>Frequency</td>
    <td>Priority</td>
    </tr>
    <tr align='center'>
    <td>$date</td>
    <td>$desc</td>
    <td>$status</td>
    <td>$type</td>
    <td>$frequency</td>
    <td>$priority</td>
    </tr>
    </table>
    <h2 align='center'>PLEASE CHECK IT www.eiwms-progroup.com</h3>
    ";
$mail->MsgHTML($message);    
$mail->AltBody = ""; 

经过一定的搜索后,我尝试了这个解决方案

$mail->AddAttachment(dirname(__FILE__),"/$path".$_FILES['myfile']['name'][$i]); 

这个

$mail->AddAttachment($_FILES['myfile']['tmp_name'][$i],$_FILES['myfile']['name'][$i]);

这个

$mail->AddAttachment("uploads/",$_FILES['myfile']['name'][$i]);

另外,我检查我的上传文件夹,文件存在于文件夹

但所有这些解决方案都没有解决我的问题

如何使用PHPmailer Class ??

发送带附件的电子邮件

上传代码:

foreach(preg_replace('/ /','-',($_FILES['myfile']['name'])) as $f => $name) {

if(move_uploaded_file($_FILES["myfile"]["tmp_name"][$f],$path.$name)){
$query=mysqli_query($conn,"INSERT INTO tbl_taskimage(db_taskid,db_image)VALUES('$row[0]','$name')") or die(mysqli_error($conn));
$count++; // Number of successfully uploaded file
}else{header("location:add-tasks.php?msg=32");
}
}

2 个答案:

答案 0 :(得分:1)

参数应为:$mail->addAttachment( $path, $name (optional), $encoding (optional), $type (optional) );

所以,你应该只使用$mail->addAttachment ( $filepath , basename ($filepath) );basename返回文件名。

答案 1 :(得分:-1)

请使用下面的工作正常的脚本......

            $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
            if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
                require '../vendor/autoload.php';
                $mail = new PHPMailer;
                $mail->setFrom('from@example.com', 'First Last');
                $mail->addAddress('whoto@example.com', 'John Doe');
                $mail->Subject = 'PHPMailer file sender';
                $mail->Body = 'My message body';
                // Attach the uploaded file
                $mail->addAttachment($uploadfile, 'My uploaded file');
                if (!$mail->send()) {
                    $msg .= "Mailer Error: " . $mail->ErrorInfo;
                } else {
                    $msg .= "Message sent!";
                }
            } else {
                $msg .= 'Failed to move file to ' . $uploadfile;
            }
        }

快速链接: https://github.com/PHPMailer/PHPMailer/blob/master/examples/send_file_upload.phps

https://github.com/PHPMailer/PHPMailer/wiki/Tutorial