2

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("[email protected]", "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

2 Answers 2

3

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("[email protected]", "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();

    }
4
  • Great! You can increase the max upload file size in your php.ini file: upload_max_filesize = 10M post_max_size = 10M Jun 6, 2015 at 15:36
  • Or in your .htaccess file php_value upload_max_filesize 10M and php_value post_max_size 10M Jun 6, 2015 at 15:36
  • By the way, you might want to remove your e-mail address from the script source posted here. Jun 6, 2015 at 15:38
  • eventually i even changed it so that it automaticlly usses one of the magento email adresses
    – DaanRio
    Mar 1, 2016 at 8:35
1

You should put some breakpoints after the if statements. There is no way to check if $tmpFilePath != "" and move_uploaded_file($tmpFilePath, $newFilePath) return true.

I'd recommend either putting breakpoints after the if statements, or doing a echo or var_dump + exit. That way you can check where it goes wrong.

13
  • What i did was this Mage::getSingleton('core/session')->addSuccess($this->__("step1")); and step 2 after the if's Result : both not working
    – DaanRio
    May 28, 2015 at 7:49
  • Is the form called using ajax? Try to put echo 'step1 complete';exit; after the first if statement, and echo 'step2 complete';exit after the second statement. If the form is not being submitted using ajax, that should give you an indication what steps work and where it goes wrong. May 28, 2015 at 8:05
  • still i t doesnt work
    – DaanRio
    May 28, 2015 at 8:10
  • Apparently $tmpFilePath is not set then. Do echo '<pre>';var_dump($_FILES);exit; to debug. May 28, 2015 at 8:13
  • definitly code not workin empty array array(0) { }
    – DaanRio
    May 28, 2015 at 11:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.