Call mail sending for contact us magento 2 programmatically

In Magento 2, you can send a mail programmatically using the Magento\Framework\Mail\Template class. Here's an example of how you can send a mail programmatically for the "Contact Us" form:

use Magento\Framework\Mail\Template;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\TransportInterface;

// Get the contact form data
$data = array(
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '1234567890',
    'message' => 'This is a test message'
);

// Create a new mail template
$template = new Template('contact', 'contact', 'html', 'en_US');

// Set the template variables
$template->setVariables($data);

// Create a new mail message
$message = new MessageInterface();
$message->setBody($template->getProcessedTemplate());
$message->setFrom('[email protected]');
$message->addReplyTo('[email protected]');
$message->addHeader('Content-Type', 'text/html; charset=UTF-8');

// Create a new mail transport
$transport = new TransportInterface();

// Send the mail
$transport->sendMessage($message);

In this example, we create a new Template object and set the template variables using the setVariables method. We then create a new MessageInterface object and set the body of the message using the getProcessedTemplate method. We also set the from address, reply-to address, and content type of the message.

Finally, we create a new TransportInterface object and use it to send the mail using the sendMessage method.

You can also use the Magento\Framework\Mail\Template class to send a mail with attachments. Here's an example:

use Magento\Framework\Mail\Template;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\TransportInterface;

// Get the contact form data
$data = array(
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '1234567890',
    'message' => 'This is a test message'
);

// Create a new mail template
$template = new Template('contact', 'contact', 'html', 'en_US');

// Set the template variables
$template->setVariables($data);

// Create a new mail message
$message = new MessageInterface();
$message->setBody($template->getProcessedTemplate());
$message->setFrom('[email protected]');
$message->addReplyTo('[email protected]');
$message->addHeader('Content-Type', 'text/html; charset=UTF-8');

// Create a new attachment
$attachment = new \Zend_Mail_Part();
$attachment->setType('application/octet-stream');
$attachment->setDisposition('attachment; filename="example.txt"');
$attachment->setContent('This is an example attachment');

// Add the attachment to the message
$message->addAttachment($attachment);

// Create a new mail transport
$transport = new TransportInterface();

// Send the mail
$transport->sendMessage($message);

In this example, we create a new Zend_Mail_Part object to represent the attachment, and then add it to the message using the addAttachment method.

You can also use the Magento\Framework\Mail\Template class to send a mail with multiple attachments. Here's an example:

use Magento\Framework\Mail\Template;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\TransportInterface;

// Get the contact form data
$data = array(
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '1234567890',
    'message' => 'This is a test message'
);

// Create a new mail template
$template = new Template('contact', 'contact', 'html', 'en_US');

// Set the template variables
$template->setVariables($data);

// Create a new mail message
$message = new MessageInterface();
$message->setBody($template->getProcessedTemplate());
$message->setFrom('[email protected]');
$message->addReplyTo('[email protected]');
$message->addHeader('Content-Type', 'text/html; charset=UTF-8');

// Create multiple attachments
$attachments = array();
$attachments[] = new \Zend_Mail_Part();
$attachments[0]->setType('application/octet-stream');
$attachments[0]->setDisposition('attachment; filename="example1.txt"');
$attachments[0]->setContent('This is an example attachment 1');

$attachments[] = new \Zend_Mail_Part();
$attachments[1]->setType('application/octet-stream');
$attachments[1]->setDisposition('attachment; filename="example2.txt"');
$attachments[1]->setContent('This is an example attachment 2');

// Add the attachments to the message
foreach ($attachments as $attachment) {
    $message->addAttachment($attachment);
}

// Create a new mail transport
$transport = new TransportInterface();

// Send the mail
$transport->sendMessage($message);

In this example, we create an array of Zend_Mail_Part objects to represent the multiple attachments, and then add them to the message using the addAttachment method.

Note that you need to make sure that the mail transport is configured correctly in your Magento 2 installation. You can do this by going to the "Stores" > "Configuration" > "Advanced" > "System" > "Mail" section of the Magento 2 admin panel.