PHPmailer发送带有附件的电子邮件

时间:2019-01-31 10:44:40

标签: php phpmailer

我有一个使用PHPmailer发送电子邮件的脚本(类不同,因为我使用CRUD,但功能相同)

这是脚本:

    if (array_key_exists('recipient', $_POST)) {
        $attachment = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachment']['name']));
        move_uploaded_file($_FILES['attachment']['tmp_name'], $attachment);
        $recipient = $_POST['recipient']; 
        $subject = $_POST['subject'];
        $content = $_POST['content'];
        //$attachment = $_POST['attachment'];

        $email->Sender = "noreply@fw.net";
        $email->Recipient = "noreply@fw.net";
        if (!empty($_POST['recipient'])) {
            $email->Recipient = $recipient;
        } else { 
            foreach ($bcc as $bcc) {
                $email->addBcc($bcc);
        }
    }   
    //$email->addRecipient = $bcc;
    $email->Subject = $subject;
    $email->Content = $content; 
    $email->addAttachment($attachment, 'MyFile');
    $email->Format = "html";
    //$email->send();

    if(!$email->send()) {
        $msg = '<div style="Color:red">Sent:</div>'. $email->SendErrDescription;    
    } else {
        $msg = '<div style="Color:green">not Sent</div>';
        header("Refresh:5");
    }
}

此表单:email.php(在同一页面上工作的脚本/表单)

<form id="mess" action="email.php" method="POST" enctype="multipart/form-data">
<?php if ($Page->CheckToken) { ?>
<input type="hidden" name="<?php echo TOKEN_NAME ?>" value="<?php echo $Page->Token ?>">
<?php } ?>
<div id="jsInfo" data-count="<?php echo $count; ?>"></div>
<div class="card card-primary card-outline">
.......
                 <div class="form-group">
                  <div class="btn btn-default btn-file">
                    <i class="fa fa-paperclip"></i> Allegato
                    <input type="hidden" name="MAX_FILE_SIZE" value="100000"> <input type="file" name="attachment" id="attachment">
                  </div>
                  <p class="help-block">Max. 10MB</p>
                </div>
........
                  <button type="submit" value="Upload" name="submit" class="btn btn-primary"><i class="fa fa-envelope-o"></i> Send </button>

我将目的地更改为php.ini,我在本地使用wamp进行测试 来自:

sys_temp_dir = "/tmp"

sys_temp_dir = "c:/Users/lt/Documents/Temp"

问题:

为什么未附加到原始文件(pdf,jpg,bmp,txt等),但是却附加了* .tmp文件。 ?我在哪里错了?

假设我解决了这个问题。

don't works
$attachment = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['attachment']['name']));
move_uploaded_file($_FILES['attachment']['tmp_name'], $attachment);

works
$uploaddir = 'C:/wamp64/www/fw3/';
$attachment_tmp = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
move_uploaded_file($attachment_tmp, $uploaddir . $attachment_name);

我又遇到两个问题... 1)如何将附件发送给所有收件人? 2)发送4Mb文件时,页面加载时间很长,如何修改脚本以使发送工作在后台进行?

0 个答案:

没有答案