How to add an attachment in email in magento?

I want to add a custom email script to add an attachment in  controller file

Junior Developer Asked on November 24, 2017 in MAGENTO.
Add Comment
1 Answer(s)

<?php
$ToName=”Sender Name”;
/* Sender Email */
$ToEmail=”domainname.com”;

$mail = new Zend_Mail();
$mail->setType(Zend_Mime::MULTIPART_RELATED);

$mail->setFrom(‘support@example.com’, ‘Your business name goes here’);
$mail->addTo($ToEmail, $ToName);

$sub=”Email subject goes here”;
$mail->setSubject($sub);
$dir = Mage::getBaseUrl();

if (isset($_FILES[‘filename’][‘name’]) && $_FILES[‘filename’][‘name’] != ”) {
$uploaddir22=”media/uploads/”;

$f_name=time() .”_”.$_FILES[‘filename’][‘name’];

$uploadfile22 = $uploaddir22 . $f_name;

if(move_uploaded_file($_FILES[‘filename’][‘tmp_name’], $uploadfile22)) {
$filename = $f_name;
chmod($uploadfile22, 0777);

}

$ext1 = pathinfo($filename, PATHINFO_EXTENSION);
$exttype1=’application/’.$ext1;
$file = $mail->createAttachment(file_get_contents($uploadfile22));
$file ->type = $exttype1;
$file ->disposition = Zend_Mime::DISPOSITION_INLINE;
$file ->encoding = Zend_Mime::ENCODING_BASE64;
$file ->filename = $filename;

}

$html = “<h1>Body of your email data goes here</h1>”;
$mail->setBodyHtml($html);

try{
if (! $mail->send()) {
throw new Exception ();
}else{
echo “Email sent successfully”;
}
}
catch(Exception $ex){
echo $ex->getMessage();
}

?>

Team Leader Answered on December 4, 2017.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.