Question

1 Question: How do I send my attachment with the other data?

2 Question: Is there any way I can get the uploaded file and change the name to the email and the schedule of the uploader?

This code save my attachment

Function

        $_UP['pasta'] = '/aplic/www/html/media/uploadcontato/';

        $_UP['caminho'] = '/media/uploadcontato/';

        $_UP['extensoes'] = array('jpg', 'png', 'gif', 'pdf', 'txt', 'doc', 'docx');

        $_UP['renomeia'] = false;

        if(filter_input(INPUT_POST, "btnSubmit")){
        if ($_FILES['attachment']['error'] != 0) {                  
        }
        $extensao = strtolower(end(explode('.', $_FILES['attachment']['name'])));
        if (array_search($extensao, $_UP['extensoes']) === false) {                  
        }
        if ($_UP['renomeia'] == true) {
          $nome_final = md5(time()).'.jpg';
        } else {
          $nome_final = $_FILES['attachment']['name'];
        }
        if (move_uploaded_file($_FILES['attachment']['tmp_name'], $_UP['pasta'] . $nome_final)) {
          $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
          $query = "INSERT INTO contato (`nome`,`caminho`) VALUES ('".$nome_final."', '".$_UP['pasta']."')";
          $connection->query($query);
        } else {
        } 

And this send email

Function

$post = $this->getRequest()->getPost();
if ( $post ) {
    $translate = Mage::getSingleton('core/translate');

    $translate->setTranslateInline(false);
    try {
        $postObject = new Varien_Object();
        $postObject->setData($post);

        $error = false;

        if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
            $error = true;
        }

        if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
            $error = true;
        }

        if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
            $error = true;
        }

        if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
            $error = true;
        }

        if ($error) {
            throw new Exception();
        }
        $mailTemplate = Mage::getModel('core/email_template');

        $mailTemplate->setDesignConfig(array('area' => 'frontend'))
            ->setReplyTo($post['email'])
            ->addBcc($post['email'])
            ->sendTransactional(
                Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                null,
                array('data' => $postObject)
            );

        if (!$mailTemplate->getSentSuccess()) {
            throw new Exception();
        }

        $translate->setTranslateInline(true);

        Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
        $this->_redirect('*/*/');

        return;
    } catch (Exception $e) {
        $translate->setTranslateInline(true);

        Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
        $this->_redirect('*/*/');
        return;
    }

} else {
    $this->_redirect('*/*/');
}

}

Was it helpful?

Solution

Hi try below code it will work for you.

$mailTemplate = Mage::getModel('core/email_template');

$dir = Mage::getBaseDir();  
$attachments = $dir.DS.'media'.DS.'customdir'.DS.$imagename;
if(file_exists($attachments))
{
    $fileContents = file_get_contents($attachments);
    $attachment   = $mailTemplate->getMail()->createAttachment($fileContents);
    $attachment->filename = basename($attachments);
}

$mailTemplate->setDesignConfig(array('area' => 'frontend'))
->setReplyTo($post['email'])
->addBcc($post['email'])
->sendTransactional(
    Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
    null,
    array('data' => $postObject)
);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top