Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding unsubscribe link in custom email magento

Tags:

magento

How to add unsubscribe link in custom email notification i am sending an email through zend mail function i follow this function sending mail in magento in body part i want to add unsubscribe link how can we implement that? In my e mail notification i am using this function.


public function sendMail()
    {           
        $post = $this->getRequest()->getPost();     
        if ($post){
                $random=rand(1234,2343);

                $to_email = $this->getRequest()->getParam("email");
                $to_name = 'Hello User';
                $subject = ' Test Mail- CS';
                $Body="Test Mail Code : "; 

                $sender_email = "[email protected]";
                $sender_name = "sender name";

                $mail = new Zend_Mail(); //class for mail
                $mail->setBodyHtml($Body); //for sending message containing html code
                $mail->setFrom($sender_email, $sender_name);
                $mail->addTo($to_email, $to_name);
                //$mail->addCc($cc, $ccname);    //can set cc
                //$mail->addBCc($bcc, $bccname);    //can set bcc
                $mail->setSubject($subject);
                $msg  ='';
                try {
                      if($mail->send())
                      {
                         $msg = true;
                      }
                    }
                catch(Exception $ex) {
                        $msg = false;
                        //die("Error sending mail to $to,$error_msg");
                }
                $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($msg));
            }
    }

like image 622
user717841 Avatar asked Jan 20 '23 16:01

user717841


2 Answers

If you have a custom module use this code:

Mage::getModel('newsletter/subscriber')->loadByEmail($email)->getUnsubscriptionLink();

Explanation:

first part is the model for the subscriber.
If you want to see al the available methods within the model just use this code:

$myModel =  Mage::getModel('newsletter/subscriber');
foreach (get_class_methods(get_class($myModel)) as $cMethod) {
    echo '<li>' . $cMethod . '</li>';
}

the second part of the code loadByEmail($email) is to get 1 specific subscriber object. $email should be a string of the emailaddress.

The last part of the code is a selfexplaning method. It will generate a link to unsubscribe. This is a method that is given by Magento.

like image 83
IJsbrand van Prattenburg Avatar answered Jan 22 '23 05:01

IJsbrand van Prattenburg


In my Magento version I get the following code by default when creating a new newsletter template:

Follow this link to unsubscribe <!-- This tag is for unsubscribe link --><a href="{{var subscriber.getUnsubscriptionLink()}}">{{var subscriber.getUnsubscriptionLink()}}</a>

I expect it to work in any Magento version.

like image 34
Lucas Moeskops Avatar answered Jan 22 '23 06:01

Lucas Moeskops