3

I am having following code in the magento root directory to send email.

<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app("default");

$body = "Hi there, here is some body content";
$mail = Mage::getModel('core/email');
$mail->setToName('John');
$mail->setToEmail('[email protected]');
$mail->setBody($body);
$mail->setSubject('The Subject');
$mail->setFromEmail('[email protected]');
$mail->setFromName("Your Name");
$mail->setType('text');// You can use 'html' or 'text'

try {
    $mail->send();
    Mage::getSingleton('core/session')->addSuccess('Your request has been sent');

}
catch (Exception $e) {
    Mage::getSingleton('core/session')->addError('Unable to send.');

}

?>

How can I attach multiple csv files with this email.(At least 3 or 4).

4 Answers 4

7

you can use zend and this code is tested

require 'app/Mage.php';
umask( 0 );
Mage :: app( "default" );
$mail = new Zend_Mail();
$mail->setType(Zend_Mime::MULTIPART_RELATED);
$mail->setBodyHtml($html_body);
$mail->setFrom('[email protected]', 'Example');
$mail->addTo('[email protected]', 'Amit');
$mail->setSubject('Sending email using Zend Framework');
$dir = Mage::getBaseDir();
$path = $dir.DS.'var'.DS.'docs'.DS.'test.csv';
$file = $mail->createAttachment(file_get_contents($path));
$file ->type        = 'text/csv';
$file ->disposition = Zend_Mime::DISPOSITION_INLINE;
$file ->encoding    = Zend_Mime::ENCODING_BASE64;
$file ->filename    = 'test.csv';
$mail->send();
Sign up to request clarification or add additional context in comments.

Comments

1

I think they internally use PHPMailer, so that's really easy:

$mail->addAttachment('csv/test.csv');

4 Comments

Can I add multiple attachements?
Yes, just call the function again.
I am getting following errror Uncaught exception 'Varien_Exception' with message 'Invalid method Mage_Core_Model_Email::addAttachment(Array ( [0] =&gt; csv/output.csv )
Ok, then they don't support that function. Do you mind to include PHPMailer (github.com/PHPMailer/PHPMailer)? It supports that.
1
$dir = Mage::getBaseDir();
$file_name = $dir.DS.'var'.DS.'docs'.DS.'test.csv'; //file path
if(file_exists($file_name))
{
    $fileContents = file_get_contents($file_name);
    $fileName = 'test.csv';
    $mail->addAttachment($fileContents, $fileName);
}

4 Comments

I am getting following Fatal error Uncaught exception 'Varien_Exception' with message 'Invalid method Mage_Core_Model_Email::addAttachment
wait i will reply with in 1hr
Magento 'core/email' model can not send attachment. So,you should used core/email_template model.that can send attachment... f you want to send this model then it will provide codes
Please check my last answer
0

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/1.12/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);

1 Comment

can you please explain the use of variables used in last line of your answer? And what is $fileContents

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.