Swift 4.0 attachment path

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
locus
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2009 6:45 pm

Swift 4.0 attachment path

Post by locus »

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Swift 4.0 attachment path

Post by Chris Corbyn »

If in version 3 you had this, it's the Swift_File bit that specifies the path...

Code: Select all

$message->attach( new Swift_Message_Attachment(new Swift_File($file_path), $file_name, $file_type));
So your version 4 code should look like this:

Code: Select all

$message->attach(Swift_Attachment::fromPath($file_path, $file_type)->setFilename($file_name));
locus
Forum Newbie
Posts: 2
Joined: Tue Mar 10, 2009 6:45 pm

Re: Swift 4.0 attachment path

Post by locus »

Thanks! - works great
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Swift 4.0 attachment path

Post by Chris Corbyn »

It's also good practice to move_uploaded_file() before attaching it, so your code would become something like:

Code: Select all

$new_path =  '/some/path/' . $file_name;
if (move_uploaded_file($file_path, $new_path)) {
  $message->attach(Swift_Attachment::fromPath($new_path));
}
NOTE: Most common MIME types are automatically detected in version 4.
Terrantino
Forum Newbie
Posts: 1
Joined: Mon Mar 23, 2009 5:52 pm

Re: Swift 4.0 attachment path

Post by Terrantino »

Chris Corbyn wrote:It's also good practice to move_uploaded_file() before attaching it, so your code would become something like:

Code: Select all

$new_path =  '/some/path/' . $file_name;
if (move_uploaded_file($file_path, $new_path)) {
  $message->attach(Swift_Attachment::fromPath($new_path));
}
NOTE: Most common MIME types are automatically detected in version 4.
Also, I just want to confirm if the 'fromPath()' method accept array values, and how would I best implement that?

Thanks.

Terrantino
Post Reply