Search code examples
phpemailsymfonyemail-attachmentspostmark

Postmark - Not able to attach PDF and Image from saved location path in Symfony2


I am saving an Image and PDF file which was uploaded by user in one of the local path. And now I want to attach those PDF and Image file when mailing[Mailing - Using POSTMARK API]. But its not getting attached.

The following is the code that I am using -

//Location where the file gets uploaded
$targetPath         =   $this->container->getParameter('upload_dir').'/Exibition/'.$fileName;
$imagePath          =   $this->container->getParameter('upload_dir').'/sk_booth_B17.jpg';

$message  = $this->get('postmark.message');
$message->addTo($custemail);
$message->addCC($userEmail);
$message->setSubject('My Subject Goes Here');
$message->setHTMLMessage($message_body);
$message->addAttachment($targetPath);
$message->addAttachment($imagePath);
$result = $message->send();

if($result){
    // Do Something
}else{
    // Do Something    
}

I have done all the configurations correctly. I am able to send plain text mail without any attachments.

When I tried to attach PDF or Image I am getting the following error -

Catchable Fatal Error: Argument 1 passed to MZ\PostmarkBundle\Postmark\Message::addAttachment() must be an instance of Symfony\Component\HttpFoundation\File\File, string given, called in C:\wamp\www\skerp4\src\Skerp\SalesBundle\Controller\ExhibitionCustomerController.php on line 558 and defined in C:\wamp\www\skerp4\vendor\mlpz\postmark-bundle\MZ\PostmarkBundle\Postmark\Message.php line 254

How to attach the locally saved PDF or Images.Where am I going wrong. How to implement the attachments. Any help will be appriciated.


Solution

  • Assuming your attachments are under the 10 MB limit, and assuming that this is the library you are referring to, you are not using the addAttachment method correctly as it only accepts a Symfony File object.

    The README under that repo states this as an example:

    $message  = $this->get('postmark.message');
    $message->addTo('test@gmail.com', 'Test Test');
    $message->setSubject('subject');
    $message->setHTMLMessage('<b>email body</b>');
    $message->addAttachment(new Symfony\Component\HttpFoundation\File\File(__FILE__));
    $response = $message->send();
    
    $message->addTo('test2@gmail.com', 'Test2 Test');
    $message->setSubject('subject2');
    $message->setHTMLMessage('<b>email body</b>');
    $message->addAttachment(new Symfony\Component\HttpFoundation\File\File(__FILE__), 'usethisfilename.php', 'text/plain');
    $response = $message->send();
    

    The construct of Symfony\Component\HttpFoundation\File\File accepts a system path for the file, and generates the correct object accordingly, so under that inference, you need to write your code in the following manner:

    //Location where the file gets uploaded
    $targetPath         =   $this->container->getParameter('upload_dir').'/Exibition/'.$fileName;
    $imagePath          =   $this->container->getParameter('upload_dir').'/sk_booth_B17.jpg';
    
    $message  = $this->get('postmark.message');
    $message->addTo($custemail);
    $message->addCC($userEmail);
    $message->setSubject('My Subject Goes Here');
    $message->setHTMLMessage($message_body);
    $message->addAttachment(new Symfony\Component\HttpFoundation\File\File($targetPath), $fileName, 'application/pdf');
    $message->addAttachment(new Symfony\Component\HttpFoundation\File\File($imagePath), 'sk_booth_B17.jpg', 'image/jpeg');
    $result = $message->send();