Fatal error: Uncaught Error: Call to a member function setBodyAttachment() on null in /home/vestuv/web/vestuviniaiziedai.lt/public_html/app/code/Dipolis/Callmeform/Mail/Template/TransportBuilder.php:11 Stack trace: #0 /home/vestuv/web/vestuviniaiziedai.lt/public_html/app/code/Dipolis/Callmeform/Controller/Index/Post.php(77):
Getting error while trying to send custom email with attachment In Magento 2.3.3
Sent mail code
$fileDirectoryPath = $this->directoryList->getPath(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR).'/tmp';
$filePath = $fileDirectoryPath . '/' . $_FILES['attachment']['name'];
move_uploaded_file($_FILES['attachment']['tmp_name'], $filePath);
try {
if($_FILES['attachment']['name']){
$fileName = $_FILES['attachment']['name'];
$mimeType = $_FILES['attachment']['type'];
$transport = $this->_transportBuilder->addAttachment(file_get_contents($filePath), $fileName, $mimeType);
}
$transport = $this->_transportBuilder
->setTemplateIdentifier('callmeform_email_template')
->setTemplateOptions(['area' => MagentoFrameworkAppArea::AREA_FRONTEND, 'store' => $storeId])
->setTemplateVars([
'name' => $username,
'phone' => $userphone
])
->setFrom(['name' => $sentToName,'email' => $sentToEmail])
->addTo($sentToEmail)
->getTransport();
$transport->sendMessage();
Attachment function
public function addAttachment($content, $fileName, $fileType)
{
$this->message->setBodyAttachment($content, $fileName, $fileType);
return $this;
}
public function setBodyAttachment($content, $fileName, $fileType)
{
$attachmentPart = $this->partFactory->create();
$attachmentPart->setContent($content)
->setType($fileType)
->setFileName($fileName)
->setDisposition(Mime::DISPOSITION_ATTACHMENT)
->setEncoding(Mime::ENCODING_BASE64);
$this->parts[] = $attachmentPart;
return $this;
}