簡體   English   中英

PHPMailer發送不帶附件的郵件

[英]PHPMailer send mail without attachment

我使用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");
}
}

參數應該是: $mail->addAttachment( $path, $name (optional), $encoding (optional), $type (optional) );

因此,只應使用$mail->addAttachment ( $filepath , basename ($filepath) ); basename返回文件名。

請使用下面的腳本工作正常...

            $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/教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM