How to send email programmatically in Magento 2?

by jasen_gottlieb , in category: PHP CMS , 2 years ago

How to send email programmatically in Magento 2?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by scotty_walker , a year ago

@jasen_gottlieb 

To send an email programmatically in Magento 2, you can use the MagentoFrameworkMailTemplateTransportBuilder class. Here's an example of how to use it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
use MagentoFrameworkMailTemplateTransportBuilder;

// ...

// Get the TransportBuilder instance
$transportBuilder = $this->transportBuilder;

// Set the sender information
$transportBuilder->setFrom('[email protected]')
    ->addTo('[email protected]');

// Set the subject and body of the email
$transportBuilder->setSubject('Email Subject')
    ->setBody('Email Body');

// Get the transport instance and send the email
$transport = $transportBuilder->getTransport();
$transport->sendMessage();


This will send an email with the specified subject and body to the specified recipient from the specified sender. You can also use the setTemplateIdentifier and setTemplateVars methods to specify a template for the email and pass variables to the template.


You can also use the MagentoFrameworkMailTemplateSenderResolverInterface class to automatically resolve the sender based on the store configuration.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use MagentoFrameworkMailTemplateSenderResolverInterface;

// ...

// Get the SenderResolver instance
$senderResolver = $this->senderResolver;

// Get the sender email and name based on the store configuration
$sender = $senderResolver->resolve('customer_support');
$senderName = $senderResolver->getSenderName('customer_support');

// Set the sender information
$transportBuilder->setFrom($sender, $senderName)
    ->addTo('[email protected]');

// Set the subject and body of the email
$transportBuilder->setSubject('Email Subject')
    ->setBody('Email Body');

// Get the transport instance and send the email
$transport = $transportBuilder->getTransport();
$transport->sendMessage();


In this example, the customer_support sender is used, which is typically configured to be the customer support email address for the store. You can specify any other sender as well, such as general, sales, or support.


I hope this helps! Let me know if you have any questions.

Member

by lily , 9 months ago

@jasen_gottlieb 

To send an email programmatically in Magento 2, you can follow these steps:

  1. Create a new module or use an existing one to add your custom code. If you are creating a new module, make sure it is enabled.
  2. Create a new PHP file in your module's directory, for example, Vendor/Module/Model/Email.php.
  3. In the Email.php file, create a class and inject the following dependencies:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
use MagentoFrameworkMailTemplateTransportBuilder;
use MagentoFrameworkAppArea;
use MagentoFrameworkAppState;

class Email
{
    protected $transportBuilder;
    protected $state;

    public function __construct(
        TransportBuilder $transportBuilder,
        State $state
    )
    {
        $this->transportBuilder = $transportBuilder;
        $this->state = $state;
    }
}


  1. Add a method to send the email in the same class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public function sendEmail()
{
    $this->state->emulateAreaCode(Area::AREA_FRONTEND, [$this, 'sendTransactionalEmail']);
}

public function sendTransactionalEmail()
{
    $transport = $this->transportBuilder
        ->setTemplateIdentifier('your_email_template') // Replace with your email template identifier
        ->setTemplateOptions([
            'area' => Area::AREA_FRONTEND,
            'store' => MagentoStoreModelStore::DEFAULT_STORE_ID,
        ])
        ->setFrom([
            'name' => 'Sender Name',
            'email' => '[email protected]',
        ]) // Replace with your sender name and email
        ->addTo([
            'name' => 'Recipient Name',
            'email' => '[email protected]',
        ]) // Replace with your recipient name and email
        ->setTemplateVars([
            'variable1' => 'Value 1',
            'variable2' => 'Value 2',
        ]) // Replace with your template variables and values
        ->getTransport();
    
    $transport->sendMessage();
}


  1. Replace 'your_email_template' with the actual identifier of your email template. Replace the sender and recipient name and email, as well as the template variables and values with your desired values.
  2. In your module's configuration file etc/module.xml, add the following code to declare the dependency on the Magento_Framework and Magento_Store modules:
1
2
3
4
5
6
    
        
        
    


  1. Use the sendEmail() method to trigger the email sending in your module's code.


Note: Make sure you have created an email template in the admin panel with the same identifier you specified in the sendTransactionalEmail() method.


That's it! You can now send emails programmatically using the Email class in your Magento 2 module.