If you take a look at how the Magento_Contact module handles the email sending you'll be able to use much of what they use.
First off inject Magento\Contact\Model\MailInterface
into your class through the constructor and use as $this->mail
.
...
public function __construct(
\Magento\Contact\Model\MailInterface $mail
) {
$this->mail = $mail;
}
...
Then use this to access the send()
method. Something similar to below:
private function sendTheConatactEmail(array $data)
{
$this->mail->send(
$data['email'],
['data' => new DataObject($data)]
);
}
Looking below at the actual send
method we call you can see how the DataObject
is actually used:
/**
* Send email from contact form
*
* @param string $replyTo
* @param array $variables
* @return void
*/
public function send($replyTo, array $variables)
{
/** @see \Magento\Contact\Controller\Index\Post::validatedParams() */
$replyToName = !empty($variables['data']['name']) ? $variables['data']['name'] : null;
$this->inlineTranslation->suspend();
try {
$transport = $this->transportBuilder
->setTemplateIdentifier($this->contactsConfig->emailTemplate())
->setTemplateOptions(
[
'area' => Area::AREA_FRONTEND,
'store' => $this->storeManager->getStore()->getId()
]
)
->setTemplateVars($variables)
->setFrom($this->contactsConfig->emailSender())
->addTo($this->contactsConfig->emailRecipient())
->setReplyTo($replyTo, $replyToName)
->getTransport();
$transport->sendMessage();
} finally {
$this->inlineTranslation->resume();
}
}
An example of the $data
array we could pass would be:
[
'name' => 'Josh Carter',
'email' => '[email protected]',
'telephone' => '12345678901'
'comment' => 'I need some help so I filled in the contact form.'
]
Hope this helps.
Josh