Question

As same as the question, i want to create the mutation for contact us page and send the email via magento default contact us module without writing the custom email function in my resolver.

How can i write this?

Was it helpful?

Solution

After several search i wrote a custom module to deal with this.i Followed the same contact-us module. I just create a mutation to pass the form data to resolver and get the data from the resolver to data provider.Used the same data binding format and mail function as it is in the default magento.

My schema.graphqls mutation is below.

type Mutation {
   contactusFormSubmit(input: ContactusInput!): ContactusOutput @resolver(class: "\\Ayakil\\ContactUs\\Model\\Resolver\\Contactus") @doc(description:"Contact us form")
}

input ContactusInput {
   fullname: String @doc(description: "The customer's full name")
   email: String @doc(description: "The customer's email address")
   telephone: String @doc(description: "The telephone")
   message: String @doc(description: "The customer's message")
}

type ContactusOutput {
    success_message: String @doc(description: "Success Message")
}

And data provider file is below

public function __construct(
    ConfigInterface $contactsConfig,
    MailInterface $mail,
    DataPersistorInterface $dataPersistor,
    \Magento\Framework\Data\Form\FormKey $formKey
) {
    $this->mail = $mail;
    $this->dataPersistor = $dataPersistor;
    $this->formKey = $formKey;
}

public function contactUs($fullname,$email,$telephone,$message){
    $thanks_message = [];

    try {
        $this->sendEmail($fullname,$email,$subject,$message);
    }catch (LocalizedException $e) { }

    $thanks_message['success_message']="Thanks For Contacting Us";
    return $thanks_message;
}

private function sendEmail($fullname,$email,$telephone,$message)
{
    $form_data = [];
    $form_data['name']      =   $fullname;
    $form_data['email']     =   $email;
    $form_data['telephone'] =   $telephone;
    $form_data['comment']   =   $message;
    $form_data['hideit']    =   "";
    $form_data['form_key']  =   $this->getFormKey();

    $this->mail->send(
        $email,
        ['data' => new DataObject($form_data)]
    );
}

public function getFormKey()
{
    return $this->formKey->getFormKey();
}

With this hack i was able to send the contact us email in my developed website.For the detail read How to write grapgQl mutation to create and integrate the contact us page functionality in magento 2.3.2?

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top