使用 SwiftMailer 发送文件获取 SwiftIOException

Sending Files Using SwiftMailer getting SwiftIOException

我正在使用 Swiftmailer 发送多个 attachments/files。我有 Javascript 和 HTML 通过单击按钮生成每个文件上传。如果用户点击添加文件按钮 3 次,将出现 3 个单独的文件上传按钮,允许上传一个 file/image。我遇到的问题是,如果用户添加了 3 个文件上传并且仅添加了 3 个插槽中的 attaches/uses 2 个并尝试提交电子邮件,那么我得到了经典的

Fatal error: Uncaught exception 'Swift_IoException' with message 'The path cannot be empty' 

如果我的上传已经进入循环,我该如何解决这个问题。我认为问题在于我动态添加 input type="file" 标签。如何停止 SWIFT IO 异常以允许空文件上传通过,或者更好的是,如果一个或多个文件上传为空,则阻止提交电子邮件?

Swiftmail 上传

 if($_FILES['upload']['tmp_name'][0] != ''){
            for ($i=0; $i < count($_FILES['upload']['tmp_name']); $i++) {
                $message->attach(Swift_Attachment::fromPath($_FILES['upload']['tmp_name'][$i])
                    ->setFilename($_FILES['upload']['name'][$i]));
            }
        }

HTML:

 <div class="form-group">
         <label class="col-sm-3 control-label text-white bg-secondary">You can add multiple
                            attachments</label>
            <div class="col-sm-9">
              <span class="btn btn-default btn-file text-white bg-secondary">
                <input id="upload" name="upload[]" type="file" class="file" data-show-upload="true" data-show-caption="true">
                 <button type="button" class="btn btn-primary btn-sm" id="plus">Add File</button>
                 <button type="button" class="btn btn-danger btn-sm" id="minus">Remove File</button>
            </div>
    </div>

当前 JS

$(document).ready(function(){
 //Multiple attachment addition
            $("#plus").click(function (newChild, refChild) {
                $('<input id="upload" name="upload[]" type="file" class="file" data-show-upload="true" data-show-caption="true"></span>').insertBefore(this, refChild);
            });
          }

试试下面的代码:

foreach ($_FILES["upload"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["upload"]["tmp_name"][$key];
        $name = basename($_FILES["upload"]["name"][$key]);
        $message->attach(Swift_Attachment::fromPath($tmp_name)->setFilename($name));
    }
}

希望对你有帮助。

如何设置 try catch 块来检查此消息中的 "setTo"?

 // Create a message


      try {
    $message = (new Swift_Message($_POST['subject']))
        //Email will be from the signed in user and the reply to will go to the signed in user
        ->setFrom($_SESSION['gazette']['Email'])
        ->setTo($finalEmailList)
        ->setBody($_POST['message'], 'text/html')
        ->setReplyTo($_SESSION['gazette']['Email']);

//Allow multiple uploads
    foreach ($_FILES["upload"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["upload"]["tmp_name"][$key];
            $name = basename($_FILES["upload"]["name"][$key]);
            $message->attach(Swift_Attachment::fromPath($tmp_name)->setFilename($name));
        }
    }

    // Send the message
    $result = $mailer->send($message);
} catch (Exception $exception) {
    // some logic
}

    }