Вопрос

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('testfrom@test.in', 'fayis');
     $this->mail->addTo('testto@test.in', '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

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

Другие советы

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' => 'josh@carter.com',
    'telephone' => '12345678901'
    'comment' => 'I need some help so I filled in the contact form.'
]

Hope this helps.

Josh

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top