I am able to attach a file using SwiftMailer with its name hardcoded. But what if the file is uploaded by a user from an HTML form's 'file' input type control and has to be sent with an email by a PHP script? How do I specify the file name in SwiftMailer?

Here is my code, the file upload portion:

//File upload

// Where the file is going to be placed 
$target_path = "uploads/";

// Add the original filename to our target path.  
//Result is "uploads/filename.extension" 
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";

//End of file upload

This is the file attaching portion:

//Create the attachment
$attachment = Swift_Attachment::fromPath('Plan.doc', $target_path);

Thank you!

7 Years
Discussion Span
Last Post by levsha

The solution is:

$attachment = Swift_Attachment::fromPath($target_path);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.