How to send newsletter in Magento With dynamic content? - magento

I am Extremely New to Magento.
I have created newsletter template with dummy content. Now I need to replace dummy content with dynamic content. Basically I need to send mail with new featured products in site. What should be my next step? .
How to do this please help me .

First you should create / override Newsletter module. I just suggest to send newsletter programmatic ally to customer
// This code returns all subscribers
$objCol=Mage::getResourceModel('newsletter/subscriber_collection')->load();
$collection = $objCol;
// Load the newsletter template model by $templateId
$tmplt=Mage::getModel('newsletter/template')->load($templateId);
$message=$tmplt->getTemplateText();
foreach($collection->getItems() as $item) {
$email = $item->getSubscriberEmail();
$name = $item->getSubscriberFullName();
try{
$mail = new Zend_Mail();
$mail->setFrom($tmplt->getTemplateSenderEmail());
$mail->setBodyHtml($message);
$mail->addTo($email, 'recipient');
$mail->setSubject($tmplt->getTemplateSubject());
$mail->send();
echo "Newsletter sent to ".$email."<br>";
} catch(Exception $e) {
echo $e->getMessage();
}
}
?>
May be help Magento custom emails

Related

Ajax Shoping Cart Items Update in Magento

I wrote here because I has been looking for way how to resolve my issue for 2h+ )
I need in updating shopping cart in Magento. There are similar questions at StackOverFlow.com but They looks not appropriate to my task
Let me explain shortly
1) I overwrote Mage_Checkout_CartController
like
class IB_Ajax_IndexController extends Mage_Checkout_CartController
It works nice with ajax adding products
for updating I send request
/ajax/index/updatePost
with params
form_key=H7XpKxwBOWQCkIHk&cart[304][qty]=39&cart[305][qty]=1&cart[306][qty]=1&update_cart_action=upd
It goes to my controller "IB_Ajax_IndexController"
which has all methods "Mage_Checkout_CartController"
I detected that this method does update
public function updatePostAction()
and then some update go to $this->_updateShoppingCart(); in above method
case 'update_qty':
$this->_updateShoppingCart();
break;
I copied its code to my controller for rewriting it here )
and I encountered so much difficult with it
how to detect SUCCESS or ERROR in this method updatePostAction() after execution $this->_updateShoppingCart() in it (
????
Maybe someone has experience with updating shopping cart via ajax ?
and how to modify above methods
Thanks a lot in advance
First make an response array like this.
$response = array();
After add cart code like $cart->save();
try{ if (!$cart->getQuote()->getHasError()){
$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
$response['status'] = 'SUCCESS';
$response['message'] = $message;
}
} catch (Exception $e) {
$response['status'] = 'ERROR';
$response['message'] = $this->__('Cannot add the item to shopping cart.');
Mage::logException($e);
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
return;
And when you get response, by $response['status'] you can fetch SUCCESS or ERROR.

Magento custom controller action does not render design

people. i have a problem. I have an action in my custom module, that creates customer.
There is a code:
class SeosClub_BusinessCategory_CustomerController extends Mage_Core_Controller_Front_Action {
public function createAction(){
$requestData = Mage::app()->getRequest()->getParams();
if(!$requestData['id'] || !$requestData['activation_code']){
Mage::getSingleton('core/session')->addError('Request data invalid');
}
$company = Mage::getModel('businesscategory/company')->load($requestData['id']);
if (!$company->getId()){
Mage::getSingleton('core/session')->addError('Your company does not exist');
}
if($company->getCustomerId()){
Mage::getSingleton('core/session')->addNotice($this->__('Customer already created. Forgot password?'));
$this->_redirect('customer/account/forgotpassword/');
return;
}
$group = Mage::getModel('customer/group')->load('Companies', 'customer_group_code');;
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->setStore(Mage::app()->getStore());
$customer->setData('group_id', $group->getId());
$customer->setFirstname($company->getName());
$customer->setLastname($this->__('Unknown'));
$customer->setEmail($company->getEmail());
$customer->setPassword(Mage::helper('core')->getRandomString(8));
try{
$customer->save();
$customer->sendNewAccountEmail();
$company->setCustomerId($customer->getId());
$company->save();
Mage::getSingleton('core/session')->addSuccess($this->__('Account was created succefully'));
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError($e->getMessage());
}
$this->loadLayout();
$this->renderLayout();
}
}
I have also installed theme on my magento. But, when i open the page, it renderes base magento theme, not my installed theme.
If i comment code from if($company->getCustomerId()) to $this->loadLayout(); (not including $this->loadLayout();), it renderes correct theme.
Any ideas?
Enable developer mode in magento. Uncomment display_errors in index.php. You should see some errors, since you have:
$company = Mage::getModel('businesscategory/company')->load($requestData['id']);
$requestData['id'] might not be set here, because your if condition is not ending the processing of the method. So further in the code you might not have $company and you call functions on null.
Check if $company is an object.
I think it renders the default theme becuse you have errors.
Ok, to fix that i needed to send customer email after rendering layout.

magento pass variable from controller to another model

I want to send custom mail through smtp.For which I installed http://www.magentocommerce.com/magento-connect/aschroder-com-smtp-pro-email-free-and-easy-magento-emailing-for-smtp-gmail-or-google-apps-email.html.
Which works properly for every mail provided by magento as default.
Now since the admin is having mail Id of google apps , my custom mail is not received by admin.So for this reason I want to Integrate my controller with the sending mail function of ashrodder extension.
My Controller code:
$frm = 'mail#domainname.com';
$to = 'admin#domainname.com';
$subject = "UPDATE";
$mail = new Zend_Mail('utf-8');
$mail->setBodyHtml($message)
->setFrom($frm, 'Admin')
->addTo($to, 'Site Admin')
->setSubject($subject);
try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Mail sent successfully.');
}
catch(Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send email.');
}
My question is simple : How should I pass [to,from,body] variable to any other model/controller ?
Please Help .Thanks in Advance
I think overriding the extensions controller would be the best solution.Check this link How to extend a controller

Magento: How to Print backend invoices like frontend as HTML?

I'm on Magento 1.7.0.2. How can I print invoices from backend with the same manner that frontend uses? I want it to be on HTML format not PDF.
Assuming that you want to print one invoice at a time from the admin order detail page
Create a custom admin module
Add a controller with the method below
public function printInvoiceAction()
{
$invoiceId = (int) $this->getRequest()->getParam('invoice_id');
if ($invoiceId) {
$invoice = Mage::getModel('sales/order_invoice')->load($invoiceId);
$order = $invoice->getOrder();
} else {
$order = Mage::registry('current_order');
}
if (isset($invoice)) {
Mage::register('current_invoice', $invoice);
}
$this->loadLayout('print');
$this->renderLayout();
}
Reference printInvoiceAction() in app/code/core/Mage/Sales/controllers/GuestController.php
Then in your custom layout.xml use <sales_guest_printinvoice> in /app/design/frontend/base/default/layout/sales.xml as your template
Then add a button with link to the following url (need to get invoice id from order) /customModule/controller/printInvoice/invoice_id/xxx
(Not tested, so let me know if you run into any issues)
You should create your custom css file for printing print.css. And you should add "Print Button", that will call window.print()

Adding unsubscribe link in custom email 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 = "sender#sender.com";
$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 -->{{var subscriber.getUnsubscriptionLink()}}
I expect it to work in any Magento version.
I am using Magento 1.9.
To add newsletter unsubscribe link in newsletter template here are following steps:
Override the core file
/app/code/core/Mage/Newsletter/Model/Subscriber.php
by copy in local directory
/app/code/local/Mage/Newsletter/Model/Subscriber.php
Open in editor to edit the code and seacrh the function sendConfirmationSuccessEmail()
replace the code
$email->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
$this->getEmail(),
$this->getName(),
array('subscriber'=>$this)
);
with this
$email->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_SUCCESS_EMAIL_IDENTITY),
$this->getEmail(),
$this->getName(),
array('subscriber'=>$this, 'unsubscribe' =>$this->getUnsubscriptionLink())
);
and place this code in email template where you want to use unsubscribe link:
Unsubscribe here
That's it!
Hope this helps someone.

Resources