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));
}
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With