In SwiftMail 3 to send attachment the following example works
if(!empty($_FILES["attachment"]["tmp_name"]))
{
if ($_FILES["attachment"]["error"])
{
//Redirect if the upload has failed
header("Location: ?p=emailFailure&error=upload_failed");
exit();
}
$file_path = $_FILES["attachment"]["tmp_name"];
$file_name = $_FILES["attachment"]["name"];
$file_type = $_FILES["attachment"]["type"];
}
//Enable disk caching if we can
if (is_writable("/tmp"))
{
Swift_CacheFactory::setClassName("Swift_Cache_Disk");
Swift_Cache_Disk::setSavePath("/tmp");
}
//If an attachment was sent, attach it
if ($file_path && $file_name && $file_type)
{
$message->attach( new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
}
In SwiftMail 4.0 how is this accomplished?
When just using:
$message->attach(Swift_Attachment::fromPath($file_path.'/'.$file_name, $file_type));
it does not work
Swift 4.0 attachment path
Moderators: Chris Corbyn, General Moderators
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Swift 4.0 attachment path
If in version 3 you had this, it's the Swift_File bit that specifies the path...
So your version 4 code should look like this:
Code: Select all
$message->attach( new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));Code: Select all
$message->attach(Swift_Attachment::fromPath($file_path, $file_type)->setFilename($file_name));Re: Swift 4.0 attachment path
Thanks! - works great
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Swift 4.0 attachment path
It's also good practice to move_uploaded_file() before attaching it, so your code would become something like:
NOTE: Most common MIME types are automatically detected in version 4.
Code: Select all
$new_path = '/some/path/' . $file_name;
if (move_uploaded_file($file_path, $new_path)) {
$message->attach(Swift_Attachment::fromPath($new_path));
}-
Terrantino
- Forum Newbie
- Posts: 1
- Joined: Mon Mar 23, 2009 5:52 pm
Re: Swift 4.0 attachment path
Also, I just want to confirm if the 'fromPath()' method accept array values, and how would I best implement that?Chris Corbyn wrote:It's also good practice to move_uploaded_file() before attaching it, so your code would become something like:
NOTE: Most common MIME types are automatically detected in version 4.Code: Select all
$new_path = '/some/path/' . $file_name; if (move_uploaded_file($file_path, $new_path)) { $message->attach(Swift_Attachment::fromPath($new_path)); }
Thanks.
Terrantino