0

i have tried using the below code but didn't get the solution.

public function contactUs()
  {
     //$params = $this->getRequest()->getParams();

     //$mail = new Zend_Mail();
     $this->mail->setTemplateVars('hHai ywyywn.');
     $this->mail->setFrom('[email protected]', 'fayis');
     $this->mail->addTo('[email protected]', 'Some Recipient');
     $this->mail->setSubject('Test contact us');
       try {
         $this->mail->send();
       }        
       catch(Exception $ex) {
          Mage::getSingleton('core/session')->addError('Unable to send email. Sample of a custom notification error from Inchoo_SimpleContact.');
       }

2 Answers 2

1

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

5
  • Hi josh what are all the values i want to pass in $variables. i have passed like this but it doesnt set the 'values: $variable= array('Name'=>'chris','Phone Number'=>'55555555','Email'=>'[email protected]','Comment'=>'tes comment');
    – christy
    Commented Mar 9, 2018 at 6:23
  • Hi Christy, i've updated my answer now. Please note that PHP would be case sensitive when accessing the values and they will be trying to access to values with lowercase keys. My guess is that's what's up with your array.
    – JoshCarter
    Commented Mar 9, 2018 at 8:56
  • No problem Christy, please kindly upvote my answer if you found it helpful!
    – JoshCarter
    Commented Mar 9, 2018 at 14:14
  • Actually i have override contact post controller in custom module but m getting error with this function Fatal error: Uncaught Error: Call to a member function send()
    – Magecode
    Commented Jul 6, 2018 at 4:50
  • magento.stackexchange.com/questions/232501/…
    – Magecode
    Commented Jul 6, 2018 at 4:50
1

I got the answer for my question.find the below code:

    public function contactUs($name,$email,$telephone,$comment)
   {
   // echo $name." ".$email." ".$telephone." ".$comment;exit;
     $result_arr = $this->_masterobj->getResultarray();
     $data = array('data'=>new DataObject([
                'name' => $name,
                'email' => $email,
                'telephone' => $telephone,
                'comment' => $comment
            ]));
  // print_r($data);exit;
$store = $this->_storeManager->getStore()->getId();
    $transport = $this->_transportBuilder->setTemplateIdentifier($this->contactsConfig->emailTemplate())
        ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
        ->setTemplateVars(
           $data
        )
        ->setFrom($this->contactsConfig->emailSender())
        // you can config general email address in Store -> Configuration -> 
        General -> Store Email Addresses
        ->addTo($this->contactsConfig->emailRecipient())
        ->getTransport();
    $transport->sendMessage();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.