1

This is my code:

 $emailTemplate  = Mage::getModel('core/email_template')->loadDefault('newsletter_subscription_confirm_email_template');
            $emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email', $storeId));
            $emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name', $storeId));
            try {
                $emailTemplate->send($email, $order->getData('customer_firstname')." ".$order->getData('customer_lastname'), array());
                Mage::getSingleton('core/session')->addSuccess(Mage::helper('customer')->__('An email was sent with a confirmation url'));
                $this->redirectSubscribeSuccess();
            } catch (Exception $e) {
                throw new Exception($e->getMessage());
            }

I received the the email with the content of the transactional email, except that the

{{var subscriber.getConfirmationLink()}} is empty . Do you know why ?

Thanks.!

[UPDATE]

 $emailTemplate  = Mage::getModel('core/email_template')->loadDefault('newsletter_subscription_confirm_email_template');
            $emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email', Mage::app()->getStore()->getId()));
            $emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name', Mage::app()->getStore()->getId()));
            $subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
            if (!$subscriber->getId()
                || $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED
                || $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
                $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED);
                $subscriber->setSubscriberEmail($email);
                $subscriber->setSubscriberConfirmCode($subscriber->RandomSequence());
                $subscriber->setStoreId(Mage::app()->getStore()->getId());
                $subscriber->setCustomerId($order->getData('customer_id'));
            }
            try {
                $emailTemplate->send($email, $order->getData('customer_firstname')." ".$order->getData('customer_lastname'), array('subscriber' => $subscriber));
                Mage::getSingleton('core/session')->addSuccess(Mage::helper('customer')->__('An email was sent with a confirmation url'));
                $this->redirectSubscribeSuccess();
            } catch (Exception $e) {
                throw new Exception($e->getMessage());
            }
  • Have you set System > configuration > Newsletter > Need to Confirm value to Yes? – Jaimin Sutariya Jan 24 '17 at 13:48
  • yes :) it is set to yes – Attila Naghi Jan 24 '17 at 13:49
1

If you look at default Magento confirmation email send function Mage_Newsletter_Model_Subscriber::sendConfirmationRequestEmail You will see that they have used $this, object of newsletter/subscriber model to pass as subscriber in it.

$email->sendTransactional(
    Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_TEMPLATE),
    Mage::getStoreConfig(self::XML_PATH_CONFIRM_EMAIL_IDENTITY),
    $this->getEmail(),
    $this->getName(),
    array('subscriber'=>$this)
);

You need to pass object of newsletter/subscriber to your custom email send function to make {{var subscriber.getConfirmationLink()}} work. In your code you have passed blank array() You can simply use

$newsletterObject = Mage::getModel('newsletter/subscriber');
$emailTemplate->send($email, $order->getData('customer_firstname')." ".$order->getData('customer_lastname'), array('subscriber'=>$newsletterObject));

You may need to load current subscriber in $newsletterObject.

  • ok, Its a start, I get in the email the confirm url, which looks like: baseurl/newsletter/subscriber/confirm/. But when i clicked on it i don;t get subscribe and i am redirected to the home page. – Attila Naghi Jan 24 '17 at 14:14
  • The id and the code is missing :| – Attila Naghi Jan 24 '17 at 14:16
  • Yes, and to make it work, you first need to subscribe the email address. Please review Mage_Newsletter_Model_Subscriber::subscribe function. Once you have added the subscriber email, you can pass object of that subscriber in your mail. You will notice that Confirmation email send function is there in the code. :) – Jaimin Sutariya Jan 24 '17 at 14:28
  • please check my update. i got now the code , but the id is still missing . What did I do wrong ? thx – Attila Naghi Jan 24 '17 at 14:32
  • I need to add the subscriber->setId(''what here? ') ? – Attila Naghi Jan 24 '17 at 14:34
0

First you need to save it as unconfirmed, that load it via email and pass it to the send function:

$emailTemplate  = Mage::getModel('core/email_template')->loadDefault('newsletter_subscription_confirm_email_template');
            $emailTemplate->setSenderEmail(Mage::getStoreConfig('trans_email/ident_general/email', Mage::app()->getStore()->getId()));
            $emailTemplate->setSenderName(Mage::getStoreConfig('trans_email/ident_general/name', Mage::app()->getStore()->getId()));
            $subscriber = Mage::getModel('newsletter/subscriber');
            if (!$subscriber->getId()
                || $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_UNSUBSCRIBED
                || $subscriber->getStatus() == Mage_Newsletter_Model_Subscriber::STATUS_NOT_ACTIVE) {
                // set subsc status to unconfirmed
                $subscriber->setStatus(Mage_Newsletter_Model_Subscriber::STATUS_UNCONFIRMED);
                $subscriber->setSubscriberEmail($email);
                $subscriber->setSubscriberConfirmCode($subscriber->RandomSequence());
                $subscriber->setStoreId(Mage::app()->getStore()->getId());
                $subscriber->setCustomerId($order->getData('customer_id'));

                try {
                    $subscriber->save();
                }
                catch (Exception $e) {
                    throw new Exception($e->getMessage());
                }
            }
            try {
                $subscriber = $subscriber->loadByEmail($email);
                $emailTemplate->send($email, $order->getData('customer_firstname')." ".$order->getData('customer_lastname'), array('subscriber' => $subscriber));
                Mage::getSingleton('core/session')->addSuccess(Mage::helper('customer')->__('An email was sent with a confirmation url'));
                $this->redirectSubscribeSuccess();
            } catch (Exception $e) {
                throw new Exception($e->getMessage());
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.