php 带有附件的 swiftmailer 和电子邮件表单 - 初学者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11319878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:16:17  来源:igfitidea点击:

swiftmailer and email form with attachment - beginner

phpswiftmailer

提问by TryHarder

As always here is the place where I have learned a lot. And I have now a new things to learn:

一如既往,这里是我学到很多东西的地方。我现在有新的东西要学:

I have a html form:

我有一个 html 表单:

<tr><td width="16%">File attachment</td><td width="2%">:</td><td><input type="file" name="fileatt" /></td></tr>

and a mail.php:

和一个mail.php:

$attachfile=$_POST["fileatt"];

and a correct swiftmailer code to send emails out;

和正确的 swiftmailer 代码以发送电子邮件;

I have googled and I found many examples how to send attachment with a file stored on the website but I would like to do it on the fly. So when you submit the button it would send it to peoples out rather than uploading the file.

我用谷歌搜索,我发现了很多例子,如何发送带有存储在网站上的文件的附件,但我想即时完成。因此,当您提交按钮时,它会将其发送给人们而不是上传文件。

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.server.co.uk', 25)
->setUsername('user')
->setPassword('pass')
;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance($subject)
  ->setFrom(array('[email protected]' => 'name'))

   ->setBody($html, 'text/html')
  ;
// Add alternative parts with addPart()
$message->addPart(strip_tags($html), 'text/plain');

// Send the message
$result = $mailer->send($message);

could anyone help me how to do the on the fly file uploading, please? Thanks in advance!!!

任何人都可以帮助我如何进行动态文件上传吗?提前致谢!!!

回答by André Catita

There's a simple way to do this, here you go:

有一个简单的方法可以做到这一点,给你:

$message->attach(
Swift_Attachment::fromPath('/path/to/image.jpg')->setFilename('myfilename.jpg')
);

That's one way SwiftMail can do this, now just the /tmp file, and turn the above into the following:

这是 SwiftMail 可以执行此操作的一种方式,现在只是 /tmp 文件,并将上述内容转换为以下内容:

Assuming: fileatt is the variable for the $_FILE, ['tmp_name'] actually is the tmp file that PHP creates from the form upload.

假设:fileatt 是 $_FILE 的变量,['tmp_name'] 实际上是 PHP 从表单上传创建的 tmp 文件。

$message->attach(
Swift_Attachment::fromPath($_FILES['fileatt']['tmp_name'])->setFilename($_FILES['fileatt']['name'])
);

More information on SwiftMail Attachments can be found on this docspage

可以在此文档页面上找到有关 SwiftMail 附件的更多信息

More information on $_FILES can be found here on w3schools, despite I don't like w3schools, this page is solid.

关于 $_FILES 的更多信息可以在 w3schools 上找到,尽管我不喜欢w3schools,但这个页面是可靠的。

回答by Aris

Another way to do this, using only a single variable for path and filename is:

另一种方法是,只使用一个变量作为路径和文件名:

$message->attach(Swift_Attachment::fromPath('full-path-with-attachment-name'));

回答by Bimal

Single Attachment

单个附件

My answer is similar to that of André Catita. However, in Laravel 6 you can use $requestinstead of $_FILES. Let me simplify the code above:

我的回答与 André Catita 的回答相似。但是,在 Laravel 6 中,您可以使用$request而不是$_FILES。让我简化上面的代码:

$path = $request->file('import')->getPathName();
$fileName = $request->file('import')->getClientOriginalName();      

$message->attach(
        Swift_Attachment::fromPath($path)->setFilename($fileName)
        );

Here I assume that the name of your file tag is import. For eg: <input type="file" name="import" />

在这里,我假设您的文件标签的名称是 import。例如:<input type="file" name="import" />

Multiple Attachments

多个附件

Now, lets say instead of single attachment you need multiple attachments. Then the code needs to be changed.

现在,假设您需要多个附件而不是单个附件。然后需要修改代码。

First your html code will become: <input type="file" name="import[]" multiple />

首先你的 html 代码会变成: <input type="file" name="import[]" multiple />

And for backend or laravel; code will be:

对于后端或 Laravel;代码将是:

$files = $request->file('import');
foreach($files as $file){

        $path = $file->getPathName();
        $fileName = $file->getClientOriginalName();      

        $message->attach(
            Swift_Attachment::fromPath($path)->setFilename($fileName)
            );
}