【问题标题】:How to attach file(s) in php mailer within same php page?如何在同一php页面中的php邮件中附加文件?
【发布时间】:2018-05-15 10:46:56
【问题描述】:

我正在使用 php mailer 发送带有附件的电子邮件。我希望将文件附加在同一页面中。我的表格,

<form method="POST" action="#" enctype="multipart/form-data"> 

<label>Files to upload:</label>
<input type="file" name="files[]" multiple/>

<button type="submit" name="btnSend" value="Send">Send</button>

</form>

而我的 php 代码 sn-p 是,

if (isset($_POST['btnSend'])) {
    $mail = new mymailer();
    include DOC_ROOT . 'include/contact-email-template.php';
    $mailArray = array("my-email-address");
    $subject = "Contact Form";
    $from = $email;
    $mail->sendMail($from, $mailArray, $subject, $admin_template);
    $emailsent = 1;
    $mod_email = "Success";

    /* redirect to home after success */
    if ($emailsent == 1) {

        unset($_POST['firstname']);
        unset($_POST['lastname']);
        unset($_POST['email']);
        unset($_POST['phone']);
        unset($_POST['message']);

        $mod_email = "Show";
                }
}

如何附加我从@​​987654324@ 中选择的文件 请帮忙。

PS:即使我将操作设置为外部文件并将所选图像保存在磁盘中并附加它们,使用此代码发送的电子邮件也很好。 我只想知道如何在同一页面中附加文件。

【问题讨论】:

  • 您必须先将这些文件上传到服务器,然后将其附加到邮件中
  • 查看PHPMailer提供的'send file upload' example

标签: php email phpmailer attachment email-attachments


【解决方案1】:

您可以 $_FILES 并将所选文件附加到同一页面上的邮件正文中

if(!empty($_FILES)){
    $i = 0;
    foreach($_FILES['files']['tmp_name'] as $file){
        echo $mail->AddAttachment($_FILES['files']['tmp_name'][$i], $_FILES['files']['name'][$i]);
        $i++;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多