How to create an unsubscribe page in magento

How to create an unsubscribe page in magento

September 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to create an unsubscribe page in magento

Creating an Unsubscribe Page in Magento: A Step-by-Step Guide

Introduction In this article, we will guide you through the process of creating a direct unsubscribe page for your Magento e-commerce store. This feature allows customers to easily opt out of receiving newsletters and promotional emails, enhancing their overall shopping experience. We’ll break down the technical steps, including creating PHP files, adding blocks to CMS pages, and writing controller functions.

Understanding the Steps Before we dive into the implementation details, let’s clarify the two ambiguous steps mentioned in your question:

  1. Creating the “unsubscribe.phtml” page: This is a custom PHP file where you will place the unsubscribe form HTML code. You’ll need to create this file in an appropriate directory within your Magento theme.

  2. Adding the created block to the CMS page: After creating the “unsubscribe.phtml” file, you’ll add it as a block to a new or existing CMS page using the Magento admin interface. This makes the unsubscribe form accessible through that particular CMS page on your store’s frontend.

Creating the Unsubscribe Form (Step 1) To create the “unsubscribe.phtml” page, follow these steps:

  • Navigate to your theme directory in Magento. For example: app/design/frontend/package/theme
  • Create a new folder named ‘templates’ or find the existing one if it exists.
  • Inside the ‘templates’ folder, create another folder named ‘newsletter’.
  • Finally, inside the ‘newsletter’ folder, create a file called “unsubscribe.phtml”.

Your directory structure should look like this: app/design/frontend/package/theme/templates/newsletter/unsubscribe.phtml

Next, open the “unsubscribe.phtml” file in your favorite text editor and paste the provided PHP code. This code includes the newsletter form with an email field and a submit button.

Adding the Unsubscribe Form to a CMS Page (Step 2) Now that you have created the “unsubscribe.phtml” file, let’s add it as a block to a new or existing CMS page:

  • Log in to your Magento admin panel.
  • Navigate to Content > Pages.
  • Either create a new page or select an existing one where you want to place the unsubscribe form.

To add the “unsubscribe.phtml” file as a block, follow these steps:

  1. Click on ‘Design’ under the ‘Page Information’ section of the selected page.
  2. In the ‘Layout Update XML’ field, paste the following code and save it:
<reference name="content">
    <block type="core/template" name="unsubscribe.form" template="newsletter/unsubscribe.phtml"/>
</reference>

This code adds a new block named “unsubscribe.form” to the content area of the page using the custom “unsubscribe.phtml” template.

Adding an Unsubscribe Link (Step 3) To provide customers with a link to access the unsubscribe form, you need to modify the newsletter subscribe form. Open the file located at: app/design/frontend/base/default/template/newsletter/subscribe.phtml

Add the following code inside the existing <div class="block-content"> section:

<div class="unsubscribe">
    <a href="<?php echo Mage::getUrl('unsubscribe-newsletter') ?>"><?php echo $this->__('Unsubscribe') ?></a>
</div>

This adds an “Unsubscribe” link to the newsletter subscribe block, which will redirect customers to the CMS page containing the unsubscribe form.

Creating the Form Action URL (Step 4) To make the unsubscribe form functional, you need to add a function in the Subscribe.php file located at: app/code/core/Mage/Newsletter/Block/Subscribe.php

Open this file and add the following function inside the Mage_Newsletter_Block_Subscribe class:

public function getUnsubscribeFormActionUrl()
{
    return $this->getUrl('newsletter/subscriber/unsubscribecus', array('_secure' => true));
}

This function generates the URL for the unsubscribe form action using the newly created ‘unsubscribecus’ controller action.

Creating the Unsubscribe Controller Action (Step 5) Finally, you need to create a new action in the SubscriberController.php file located at: app/code/core/Mage/Newsletter/controllers/SubscriberController.php

Open this file and add the following code inside the Mage_Newsletter_SubscriberController class:

public function unsubscribecusAction()
{
    $email = $this->getRequest()->getParam('email');
    $subsModel = Mage::getModel('newsletter/subscriber');
    $subscriber = $subsModel->loadByEmail($email);

    $id = (int)$subsModel->getId();
    $code = (string)$subsModel->getCode();

    if ($id && $code) {
        $session = Mage::getSingleton('core/session');
        try {
            Mage::getModel('newsletter/subscriber')->load($id)
                ->setCheckCode($code)
                ->unsubscribe();
            $session->addSuccess($this->__(‘You have been unsubscribed.’));
        } catch (Mage_Core_Exception $e) {
            $session->addException($e, $e->getMessage());
        } catch (Exception $e) {
            $session->addException($e, $this->__(‘There was a problem with the un-subscription.’));
        }
    }

    $this->_redirectReferer();
}

This new action handles the unsubscribe process by verifying the email and code provided through the form.

Conclusion By following this comprehensive guide, you have successfully created an unsubscribe page in Magento. This feature allows your customers to easily opt out of receiving newsletters and promotional emails, enhancing their overall shopping experience.

Remember to test the unsubscribe functionality thoroughly on different devices and browsers before deploying it to your live store.

If you encounter any issues or need further assistance with customizing your Magento store, don’t hesitate to visit Laravel Company for professional development services and expert advice.

Themes Admin Email Customers

Our network