swiftmailer和带有附件的电子邮件表单-初学者 [英] swiftmailer and email form with attachment - beginner

查看:227
本文介绍了swiftmailer和带有附件的电子邮件表单-初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和往常一样,这里是我学到很多东西的地方.现在,我有新的东西要学习:

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

我有一个html表单:

I have a html form:

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

和一个mail.php:

and a mail.php:

$attachfile=$_POST["fileatt"];

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

and a correct swiftmailer code to send emails out;

我已经用Google搜索了,发现了很多示例,这些示例如何发送带有存储在网站上的文件的附件,但我想即时进行.因此,当您提交按钮时,它会将其发送给其他人,而不是上传文件.

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('emai@emai.com' => '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!!!

推荐答案

有一种简单的方法可以完成此操作,

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

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

这是SwiftMail可以做到这一点的一种方法,现在只是/tmp文件,然后将以上内容转换为以下内容:

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

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

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

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

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

More information on SwiftMail Attachments can be found on this docs page

尽管我不喜欢 w3schools <,/a>,此页面是稳定的.

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

这篇关于swiftmailer和带有附件的电子邮件表单-初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆