0

We try to send attachments via a custom form. With the sendemailAction() in our IndexController.php we upload the files to a directory on the server and send them with the email.

public function sendemailAction()
{
    $params = $this->getRequest()->getParams();
    $mail = new Zend_Mail();
    $senderemail = $params['email'];
    $bodytext = '
        <p><strong>Klachtenformulier Type 1, Over het product</strong></p>
        <p>Naam: ' . $params['name'] . '</p>
        <p>Telefoon: ' . $params['telephone'] . '</p>
        <p>E-mail: ' . $params['email'] . '</p>
        <p>Ordernummer: ' . $params['ordernumber'] . '</p>
        <p>Klacht: ' . $params['comment'] . '</p>';
    $mail->setBodyHtml( $bodytext );
    $mail->setFrom('[email protected]', 'Sitename.nl');
    $mail->addTo('[email protected]', 'Michael van Eijden');
    $mail->setSubject('Subject');

    for($i=0; $i<count($_FILES['file']['name']); $i++) {

        $tmpFilePath = $_FILES['file']['tmp_name'][$i];

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

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

    try {
        $mail->send();
    }

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

    $this->_redirect('complaints-type-one-success');
}

Unfortunately this only sends the name of the added files. How we can get the size and type too?

6
  • Could you include the full email code and not just the attachment creation. Commented Sep 28, 2013 at 13:27
  • Please see the edited post
    – Michael
    Commented Sep 28, 2013 at 13:44
  • Have you tried creating a new Zend_Mime_Part attachment and adding each one individually? Commented Sep 28, 2013 at 13:49
  • I tried. Each one individually is working but does not solve because we provide possibility to add as much as file attachments as the front-end visitor wants.
    – Michael
    Commented Sep 28, 2013 at 13:59
  • What about if you created a new Zend_Mime_Part object for each iteration of the loop? Commented Sep 28, 2013 at 14:17

1 Answer 1

0

I have noticed that there is no type assigned to your attachments maybe you could also try adding that as well as using Zend_Mime_Part and using the $newFilePath as the attachment.

You could try the following including your file type

for($i=0; $i<count($_FILES['file']['name']); $i++) {

    $tmpFilePath = $_FILES['file']['tmp_name'][$i];

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

        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
            $fname = $_FILES['file']['name'][$i];
            $ftempname = $_FILES['file']['tmp_name'][$i];                   
            $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);
        }
    }
}
10
  • Hi David. Thanks but unfortantely changed nothing. I also tried to set the size with $at->size. Seems like the type and size not retrieved in the for.
    – Michael
    Commented Sep 28, 2013 at 14:43
  • Interesting, did you say it works for one file but not multiple? Do the files under /media folder also work if you open it? Commented Sep 28, 2013 at 14:48
  • It works for one file. The files under the media folder work when I open it. Just found out that the file type is correctly retrieved with $_FILES['file']['type']; but $_FILES['file']['size']; is not working.
    – Michael
    Commented Sep 28, 2013 at 14:50
  • Is there anything under $_FILES['file']['error'] error types can be found at php.net/manual/en/features.file-upload.errors.php Commented Sep 28, 2013 at 14:53
  • $_FILES['file']['error'] is null
    – Michael
    Commented Sep 28, 2013 at 14:55

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.