• Home "Learn More Step By Step"
  • Tutorialsplane.com
  • Ionic
  • Material Design Lite
  • AngularJs
  • Angular Material
  • Bootstrap
  • jQuery
  • SQL
  • PHP
  • Codeigniter
  • Materialize
Tutorialsplane
  • 🔍
close
  Tutorialsplane
  • Home
  • Sql
    • Mysql Blog
  • Codeigniter
  • Jquery
    • Jquery Blog
  • AngularJs
  • BootStrap
    • Bootstrap Questions And Answers
    • Govt Jobs
September 18, 2017 admin

Magento 2 Send email programmatically

❮❮ Previous
Next ❯❯

Magento 2 Send email programmatically transactional emails– Sending emails is the important functionality of any framework, Magento2 has its own to handle the mail functionality. Here in this post we are going to explain how you can send transactional emails programmatically with or without template from your custom module.


Magento 2 Send email programmatically | Transactional Emails Example

You can use the following steps to send emails easily-

  • 1. Create Template
  • 2. Create Config
  • 3. Create Function

1. Create Template

Now create template with some sample text located at the below specified path.

/app/code/VendorName/ModuleName/view/frontend/email/modulename/myTemplate.html

Magento 2 Send email programmatically Template:

<!---email template--->
<div>
 <h2>Welcome To Tutorialsplane.com !!</h2>
 <p>Hello, Learn More @ tutorialsplane.com.</p> 
</div>

2. Create Config

Now create configuration file to declare template-

/app/code/VendorName/ModuleName/etc/email_templates.xml

Magento 2 Send email configuration Example:


<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:Magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="modulename_mytemplate" label="Test email sample" file="modulename/myTemplate.html" type="html" module="VendorName_ModuleName" area="frontend"/>
</config>

3. Now Create Function To Send Email

Now create controller function to send email-

/app/code/VendorName/ModuleName/Controller/Index/SendCustomMail.php

Magento 2 Mail Function Example:

<?php
 
namespace VendorName\ModuleName\Controller;
 
use Magento\Framework\App\RequestInterface;
 
class SendCustomMail extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\App\Request\Http
     */
    protected $_request;
    /**
     * @var \Magento\Framework\Mail\Template\TransportBuilder
     */
    protected $_transportBuilder;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
 
    public function __construct(
        \Magento\Framework\App\Action\Context $context
        , \Magento\Framework\App\Request\Http $request
        , \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
        , \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        $this->_request = $request;
        $this->_transportBuilder = $transportBuilder;
        $this->_storeManager = $storeManager;
        parent::__constr
    parent::__construct($context);
    }
 
    public function execute()
    {
        $store = $this->_storeManager->getStore()->getId();
        $transport = $this->_transportBuilder->setTemplateIdentifier('modulename_mytemplate')
            ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
            ->setTemplateVars(
                [
                    'store' => $this->_storeManager->getStore(),
                ]
            )
            ->setFrom('general')
            // you can config general email address in Store -> Configuration -> General -> Store Email Addresses
            ->addTo('john@example.com', 'John Dee')
            ->getTransport();
        $transport->sendMessage();
        return $this;
    }
}

So you set your preferences in email template such as – set email template identifier, customer email, store, set from etc.


Advertisements

Add Comment

📖 Read More


  • 1. Magento 2 Download Files
  • 2. Magento 2 Apply Coupon Code Programmatically
  • 3. Magento 2 Get out of stock Products
  • 4. Magento 2 get shipping method from quote
  • 5. Magento 2 get Payment Method from Quote
  • 6. Magento 2 redirect Customer to Custom Page After Login
  • 7. Magento 2 Redirect From Observer
  • 8. Magento 2 Create Customer Programmatically
  • 9. Magento 2 get Invoice from Order
  • 10. Magento 2 get category Url
❮❮ Previous
Next ❯❯
Magento 2 Beginer Tutorial, Magento2 send transactional Email


Magento 2 Tutorial

Magento2 – Magento 2 load product by id
Magento 2 get Product Collection
Get customer data in magento 2
Magento 2 not loading css and js
Magento 2 get base url
Magento 2 Installation Steps on Windows
Magento 2 Directory Structure
Magento2 Turn On Error Messages
Magento 2 Create Customer Attribute
Magento 2 Reindex Command Line
Magento 2 Command Line List
Magento 2 Get Shopping Cart Details
Magento 2 get Customer Details
Magento 2 create system configuration
Magento 2 Get Form Key
Magento 2 Join Collection Query
Magento 2 Left Join Collection Query
Magento 2 Show Static Block
Magneto 2 Call Phtml in Static Block and CMS page
Magento 2 call observer after customer login
Magento 2 collection order by
Magento 2 Collection Set Page Size Limit
Magento 2 Get simple Products From Configurable Product
Magento 2 Get Last Insert Id
Magento 2 Clear Cart programmatically
Magento 2 get Cart Items
Magento 2 Get Current Quote Id
Magento 2 Delete Products programmatically
Magento 2 get Shipment id from Order Id
Magento 2 Get Ip Address
Magento 2 format price currency
Magento 2 Get Order Details
Magento 2 event observer list
Magento 2 Check customer is logged in
Magento 2 Check admin user logged or not
Magento 2 get product category id
Magento 2 Get Product Attribute
Magento 2 Login Customer Programmatically
Magento 2 Load another Template in Template
Magento 2 Update Customer Programmatically
Magneto 2 Bulk Update
Magento 2 Format Price
Magento 2 show cart items count in header
Magento 2 Get Set Session Variable
Magento 2 Logout Customer Programmatically
Magento 2 Send email programmatically
Magento 2 Download Files
Magento 2 Apply Coupon Code Programmatically
Magento 2 Get out of stock Products
Magento 2 get shipping method from quote
Magento 2 get Payment Method from Quote
Magento 2 redirect Customer to Custom Page After Login
Magento 2 Redirect From Observer
Magento 2 Create Customer Programmatically
Magento 2 get Invoice from Order
Magento 2 get category Url
Magento 2 get parent categories
Magento 2 get child categories
Magento 2 Get Current Currency Symbol
Magento 2 get current Category Name
Magento 2 get Store Details
Magento 2 Collection get last Item
Magento 2 Create Coupon Code Programmatically
Magento 2 set session timeout
Magento 2 get category image url
Magento 2 Right Join Collection
Magento 2 Join 3 Tables
Magento 2 get cart Shipping Cost
Magento 2 get product stock detail
Magento 2 check if product is configurable
Magento 2 check if product type is simple
Magento 2 check if product type is bundle
Magento 2 check if product type is grouped
Magento 2 check product type is Virtual
Magento 2 change order status programmatically
Magento 2 Update Product Price Programmatically
Magento 2 get payment method from order
Magento 2 Get Post Data in Observer
Magento 2 get product collection by category id
Magento 2 get store id by store code
Magento 2 get product collection filter by Attribute
Magneto 2 Add product to cart programmatically
Magento 2 Override Controller

Font Awesome Icons
Advertise Here


©@2025 Tutorialsplane     Contact Us   Privacy Policy
Powered by WordPress | | Thanks to WordPress Themes, Free Top Premium Themes and Restaurant Free WordPress Themes