Reading list Switch to dark mode

    Get Controller URL in Email Template file in Magento 2

    Updated 22 February 2024

    Here, we are discussing how to get a controller URL in the mail template file according to the store.

    For example: If you want to add a link to the custom controller or store-wise login URL, then you can follow the given method.

    Here, we write the code to send an email to a customer in which we add a customer login URL according to the store.

    first, create a method that you call the email template file method to send mail :

    <?php
    	
    	public function generateTemplate(){
    	   $customerId = 4;//any customer id
    	   $customerModel = $this->_objectManager->get(
                 'Magento\Customer\Model\Customer'
               );
            $customer = $customerModel->load($customerId);
            $customerStoreId = $customer->getStoreId();
            //other variables which you want to pass in the template
            $emailTempVariables['store'] = $this->_storeManager->getStore($customerStoreId);
            $this->sendMail($emailTempVariables);
    	}
    	public function sendMail($variables){
    		//code to send mail
    	}
    ?>


    Here,

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    $customerId is a customer ID.

    $customerModel is a customer model, that holds the data of the customer which has id $customerId.

    $customerStoreId is the store ID of a customer.

    $emailtempvariables is an array that holds the values that you want to pass in your email template file.

    sendmail is a function in which you write the code to send an email.

    You can check our blog “create custom mail template in magento2
    Now, create your template file with the code:

    <!--@subject Customer Account Login url mail  @-->
    
    
    {{template config_path="design/email/header_template"}}
    
    <table>
        <tr class="email-intro">
            <td>
                <p class="greeting">{{trans "Customer"}}</p>
            </td>
        </tr>
        <tr class="email-information">
            <td>
                <table class="query-details">
                    <tr>
                        <td class="product-details">
                            <p>
                                {{trans
                                    'Please <a href="%reset_url">Click here</a> to login.'
    
                                    reset_url="$this.getUrl($store,'customer/account/login/')"
                                |raw}}
                            </p>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    
    {{template config_path="design/email/footer_template"}}

    Here,

    rest_url is a variable in which we get the URL according to the store, and it returns the customer login URL according to the store.

    I hope this blog will help you with getting the Controller URL in the Email Template file in Magento 2. You may also check our wide range of best Magento 2 Extensions.

    Please reach out to our team via a support ticket if you have any queries.

    Try this and if you have any queries then just comment below 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home