I need to attach a file to a transactional email. Basically I have a cron job that generates a report and needs to it email to an admin user.
I was hoping something like this exists:
Mage::getModel('core/email_template')
->setDesignConfig(array('area'=>'frontend', 'store'=>1))
->createAttachment($file)
->sendTransactional($templateID, $sender, $email, “Admin", $vars);
Thanks in advance for any help.
I found that magento uses the zend_mail frame work. You can call the getMail function to access this framework to add an attachement.
http://framework.zend.com/manual/en/zend.mail.attachments.html
Example
$transactionalEmail = Mage::getModel('core/email_template')->setDesignConfig(array('area'=>'frontend', 'store'=>1));
$transactionalEmail->getMail()->createAttachment($fileContents,'text/csv')->filename = $filename;
$transactionalEmail->sendTransactional($templateID, $sender, $email, "Admin", $vars);
Related
I have built a cakePHP3 application and I'm looking for some advice concerning the following situation. An e-mail configuration is setup as follows, to send a mail whenever a new order has been created.
public function orderCreated($order, array $attachments = [])
{
$email = $this
->locale($order->customer->language)
->to($order->customer->email)
->from(Configure::read('Webshop.email'), Configure::read('Webshop.name'))
->subject(__('Confirmation of your order {0}', $order->nr))
->template('orders' . DS . 'order_created')
->set(['order' => $order])
->attachments($attachments);
return $email;
}
Has worked perfectly fine for ages, but I'd like to add some new functionality to this specific e-mail (and others). Admins should be able to override the content of the orders/order_created.ctp template if they want to, so they can define the content of this e-mail theirselves. So they don't have to rely on the same content in the order_created.ctp template I did provide.
Creating a UI for saving their own e-mails is not the issue. But the issue I don't really know how I could provide the overrided content to the cakePHP3 mailer. I tried setting
->template(false)
->message($new_content)
But that didn't help. Message doesn't reach mailbox because the body is empty.
Thanks.
I think I'd go with something like this:
->template('orders' . DS . 'order_created_custom')
->set(['order' => $order, 'content' => $new_content])
And then in the new order_created_custom.ctp you would output the $content. This gives you the option to do some text replacements on the content based on the $order, as well as perhaps having things like a standard salutation or signature.
I have to send an email to several recipients, but only if my current email address is marked as 'out of office'. In order to do that I will need to check the condition before sending the email and to retrieve my backups and add them in $to .
I currently managed to find a quick fix, I've modified CakeEmail, but it seems a little wrong to alter this file.
Any ideea of how I can do this without modifying CakeEmail?
Thank you,
In your configuration (core.php) file you create a new configuration
Configure:write( 'notify_people', array( 'email1#example.com' => 'Email1', 'email2#example.com' => 'Email2' );
Then you use the CakeEmail::to() to add recipients before sending an email. So you update all your $email->to() calls to read the configuration value instead.
$email = new CakeEmail();
$email->to( Configure::read( 'notify_people' ) );
//TODO: configure sender, subject etc...
//finally send the email
$email->send( $bodyOfTheMessage );
When you need to change the list of recipients you only have to do it once in the configuration file.
I'm new to magento and I'm trying to create a module for an email form.
In classic MVC I would send the request to the Controller, but in Magento a controller is only responsible for one URL. So when I want to put my email form on the productpage I cant use the controller, is that right?
I inlcude my block element via layout xml in the productpage. So I have to validate my form und send the email in the class of my block element? Or would I have to write one or more helpers for that?
What is the magento way?
Thanks a lot. Sorry if my question is lame, but I'm a beginner and I want to learn the right way and I have seen so much tutorials with the wrong one.
Just while submitting the form give action to controller like:
<?php echo Mage::getUrl()?>bpartner/index/mailbpartner
bpartner your module name
Index your controller name
mailbpartner your function in INDEX NAMED CONTROLLER FILE.
Get all details through POST and send mail like below + redirect with success
$to = "abc#abc.com";
$dt = date('d-m-Y');
$subject = "Become A Partner Details on date $dt";
$mail = new Zend_Mail('utf-8');
$mail->setBodyHtml($message)
->setFrom($data['email'], $data['firstname'])
->addTo($to, 'Site Admin')
->setSubject($subject);
try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Mail sent successfully. We will contact you shortly');
}
catch(Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send email.');
}
$this->_redirect('bpartner');
Some above data are POST DATA which is self understandable
Why is there nowhere in the Configuration/System/Mail Sending Settings to specify a user name and password for your smtp server?
To get around this, do you need to make the changes to getMail() outlined in this post:
http://www.magentocommerce.com/boards/viewthread/1073/P30/
I want to do something very simple:
- create an e-mail template
- do not have to make reference to that template in any config files.
- programmatically send an e-mail using the template defined above
- supply values to replace any tags in the template
- supply recipient e-mail addresses
- supply other bits, like a from address
So first step - create a template.
- In Confguration/Transactional Emails I believe I am supposed to see a list of templates. I see nothing. But if I add a new template, I can select from a list of templates.
- Give template a name of "Bob".
- Add a few vars to the template:
myvar1={{var myvar1}}
myvar2={{var myvar2}}
- Save the template; it is given an Id of 1.
Now send the e-mail programmatically from a controller action:
- No need to make change to LINEEND in Mime.php as it is already set to \n in version 1.4.2.0
- Make changes to getMail() in Template.php as specified in this post: http://www.magentocommerce.com/boards/viewthread/1073/P30/
- Write code in the controller action to send the e-mail:
This returns nothing:
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('no matter what goes here emailTemplate is not set');
This does return an email template:
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('Bob');
but the call to send below fails:
$emailTemplate->setSenderEmail('sent#byme.com');
$emailTemplate->setSenderName('Steve');
$emailTemplateVariables = array();
$emailTemplateVariables['myvar1'] = 'TestValue1';
$emailTemplateVariables['myvar2'] = 'TestValue2';
// $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables); -- this returns nothing
$emailTemplate->send('thisisme#mydomain.com','John', $emailTemplateVariables);
In the system.log I get the warning below, and no e-mail ever arrives.
Warning: stream_socket_enable_crypto() [<a href='streams.crypto'>streams.crypto</a>]: this stream does not support SSL/crypto in C:\Applications\Apache Software Foundation\Apache2.2\htdocs\magento\lib\Zend\Mail\Protocol\Smtp.php on line 206
Should I be using loadByCode? I wish there was some worthwhile documentation (the help for loadByCode is "Load template by code" !!). Should I be using send, sendTransactional? Oh for a bit of quality documentation.
Thanks
I see 2 questions here.
1. How to configure Magento mailing system to use smtp protocol ?
You are having trouble for this because Magento is made to use default host mailing. So it will search for it on the machine where it is installed.
If you want to configure a smtp server, I would recommend using this extension : http://www.magentocommerce.com/magento-connect/ziq2004/extension/460/advanced-smtp--artson.it
I found it simple to use and configure.
2. How to send a mail in your custom module
You can first create your template in Confguration/Transactional Emails, mark down the Id for it will be your identifier
Then, simply use this code to send the mail in your module
<?php
// The Id you just marked...
$templateId = 1;
// Define the sender, here we query Magento default email (in the configuration)
// For customer support email, use : 'trans_email/ident_support/...'
$sender = Array('name' => Mage::getStoreConfig('trans_email/ident_general/name'),
'email' => Mage::getStoreConfig('trans_email/ident_general/email'));
// Set you store
// This information may be taken from the current logged in user
$store = Mage::app()->getStore();
// In this array, you set the variables you use in your template
$vars = Array('my_var' => $my_var,
'another_var' => 12);
// You don't care about this...
$translate = Mage::getSingleton('core/translate');
// Send your email
Mage::getModel('core/email_template')->sendTransactional($templateId,
$sender,
'recipient#gmail.com',
'Recipient Name',
$vars,
$store->getId());
// You don't care as well
$translate->setTranslateInline(true);
?>
Hope this will help you
Regards,
I took out the 'ssl' => 'tls' element in the array in getMail() in Template.php and my e-mail came through.
I'd still appreciate if anyone has an explanation of how the smtp server's username and password should be specified, and an explanation of the differences in the template load methods etc would be most welcome!
If anyone is looking for full sample code of how to send a Magento email based on an existing Magento email template, the following works well. It does not require any XML config. You can load the template by name as well as by ID. In this case I load it by name.
// This is the name that you gave to the template in System -> Transactional Emails
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('My Custom Email Template');
// These variables can be used in the template file by doing {{ var some_custom_variable }}
$emailTemplateVariables = array(
'some_custom_variable' => 'Hello World'
);
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
$emailTemplate->setSenderName('Joe Bloggs');
$emailTemplate->setSenderEmail('test#test.com');
$emailTemplate->setTemplateSubject("Here is your subject");
$emailTemplate->send('recipient#test.com', 'Joanna Bloggs', $emailTemplateVariables);
Is it possible to send an e-mail programmatically in Magento? Maybe from a controller in a custom module, could you get hold of a template, populate its variables, and send the e-mail?
Thanks.
Absolutely. Here's an example from the Checkout helper:
$mailTemplate = Mage::getModel('core/email_template');
$template = Mage::getStoreConfig('checkout/payment_failed/template', $checkout->getStoreId());
$mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>$checkout->getStoreId()))
->sendTransactional(
$template,
Mage::getStoreConfig('checkout/payment_failed/identity', $checkout->getStoreId()),
$recipient['email'],
$recipient['name'],
array(
'reason' => $message,
...
'total' => $total
)
);
It's certainly possible. The email handling in Magento is quite powerful (and can be complex).
Without knowing exactly what you're trying to achieve, it would be worth you starting off by looking at the Mage_Core_Model_Email_Template class as that drives Magento's email handling.