0

我正在保存用户在本地路径之一中上传的图像和 PDF 文件。现在我想在邮寄时附上那些 PDF 和图像文件[邮寄 - 使用 POSTMARK API]。但它没有附着。

以下是我正在使用的代码 -

//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    
}

我已正确完成所有配置。我能够发送没有任何附件的纯文本邮件。

当我尝试附加 PDF 或图像时,我收到以下错误 -

可捕获的致命错误:传递给 MZ\PostmarkBundle\Postmark\Message::addAttachment() 的参数 1 必须是 Symfony\Component\HttpFoundation\File\File 的实例,给定字符串,在 C:\wamp\www\skerp4\src 中调用\Skerp\SalesBundle\Controller\ExhibitionCustomerController.php 在第 558 行,并在 C:\wamp\www\skerp4\vendor\mlpz\postmark-bundle\MZ\PostmarkBundle\Postmark\Message.php 第 254 行定义

如何附加本地保存的 PDF 或图像。我哪里出错了。如何实施附件。任何帮助都会得到帮助。

4

1 回答 1

1

假设您的附件在 10 MB 的限制之下,并且假设这是您所引用的库,那么您没有addAttachment正确使用该方法,因为它只接受一个 SymfonyFile对象。

该 repo 下的 README 举例说明了这一点:

$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();

的构造Symfony\Component\HttpFoundation\File\File接受文件的系统路径,并相应地生成正确的对象,因此根据该推断,您需要按以下方式编写代码:

//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();
于 2014-11-05T19:05:28.673 回答