Magento 1.9 – Send Email Attachment with Zend [SOLVED]

ce-1.9.1.0emailformsmagento-1.9zend-framework

I wrote a custom module which is using zend to send the information of a form to a email address.

Now i have a file upload in the form and this file (a image) needs to be send as an attachment of the mail
this is the code of the file upload in the form

<label class="label_fi" for="file">Afbeelding:
<input class="input_fi" type="file" name="foto"    value=""    size="30"/></label>

I have found a little piece of code which i entered in the index controller but this is not working

$mail = new Zend_Mail();        

    $mail->setBodyText($bericht);

    $mail->setFrom($email, $name);

    $mail->addTo("Johndoe@example.com", "John Doe");

    $mail->setSubject("Snel offerte");
    $tmpFilePath = $_FILES['file']['tmp_name'];

    if ($tmpFilePath != ""){
        $newFilePath = "./media/complaintsuploads/" . $_FILES['file']['name'][$i];

        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
            $fname = $_FILES['file']['name'];
            $ftempname = $_FILES['file']['tmp_name'];                   
            $at = new Zend_Mime_Part(file_get_contents($newFilePath));
            $at->disposition = Zend_Mime::DISPOSITION_INLINE;
            $at->encoding = Zend_Mime::ENCODING_BASE64;
            $at->filename = $fname;

            $mail->addAttachment($at);
        }
    }
    try {
        $mail->send();
    }

all the other text that is entered is in the mail
and is all gathered in this way

$name = $this->getRequest()->getParam('name');

so my question is what did i do wrong and how can i make it so that the file uploaded is send as an attachment

Best Answer

I changed my code now its working i also made sure you couldnt upload files bigger then 21mb becuause if you do it doesnt work

$mail = new Zend_Mail();        

    $mail->setBodyText($bericht);

    $mail->setFrom($email, $name);

    $mail->addTo("johndoe@example.com", "John Doe");

    $mail->setSubject("Snel offerte");
    $tmpFilePath = $_FILES['foto']['tmp_name'];
    if ($tmpFilePath != ""){
        $file_size = $_FILES['foto']['size'];
        if (($file_size > 22020096)){
            Mage::getSingleton('core/session')->addError($this->__('Bestand mag niet groter zijn dan 21 MB'));  
        }
        else{
            $fname = $_FILES['foto']['name'];
            $ftempname = $_FILES['foto']['tmp_name'];                   
            $at = new Zend_Mime_Part(file_get_contents($tmpFilePath));
            $at->disposition = Zend_Mime::DISPOSITION_INLINE;
            $at->encoding = Zend_Mime::ENCODING_BASE64;
            $at->filename = $fname;             
            $mail->addAttachment($at);
        }
    }
    try {
        $mail->send();

    }
Related Topic