I have a Symfony 3.3 application that is successfully sending an email with an attachment. The relevant function looks like this:
private function sendEmail($data)
{
$vgmsContactMail = self::contactMail;
$mailer = $this->get('mailer');
/* @var $uploadedFile UploadedFile */
$uploadedFile = $data["attachment"];
$extension = $uploadedFile->guessExtension();
if(!in_array($extension, ['pdf','rtf']) ){
die('Please upload a .pdf or .rtf file.');
}
$newFilePath = '/tmp';
$newFileName = 'temporary' . rand(0,10000) . '.rtf';
$uploadedFile->move($newFilePath, $newFileName);
$attachment = \Swift_Attachment::fromPath('/tmp/' . $newFileName);
$message = \Swift_Message::newInstance("VGMS Contact Form: ". $data["subject"])
->setFrom(array($vgmsContactMail => "Message by ".$data["name"]))
->setTo(array(
$vgmsContactMail => $vgmsContactMail
))
->setBody($data["message"]."<br>ContactMail :".$data["email"])
->attach($attachment)
;
return $mailer->send($message);
}
My problem is that hard-coding the /tmp
directory feels very brittle. Is there a more elegant way to get the path to the directory where temporary files are stored?