Email with attachment doesn't work

Hello,

My email with attachment doesn’t work.

I think the coding order is wrong.
What’s wrong? How can I solve this?
Please help me…

<?php
return function($kirby, $pages, $page) {

    $alert = null;

    if($kirby->request()->is('POST') && get('submit')) {

        // check the honeypot
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'  => get('name'),
            'company'  => get('company'),
            'email' => get('email'),
            'text'  => get('text'),
            'city' => get('city'),
            'state' => get('state'),
            'postalcode' => get('postalcode'),
            'country' => get('country'),
            'when' => get('when'),
            'howlong' => get('howlong'),
            'source' => get('source'),
            'target' => get('target')
        ];

        $rules = [
            'name'  => ['required', 'min' => 3],
            'email' => ['required', 'email'],
            'text'  => ['required', 'min' => 3, 'max' => 3000],
        ];

        $messages = [
            'name'  => 'Please enter a valid name',
            'email' => 'Please enter a valid email address',
            'text'  => 'Please enter a text between 3 and 3000 characters'
        ];

           // get the uploads
        $uploads = $kirby->request()->files()->get('file');


        // loop through uploads and check if they are valid
        foreach ($uploads as $upload) {
            // make sure the user uploads at least one file
            if ($upload['error'] === 4) {
                $alerts[] = 'You have to attach at least one file';
            //  make sure there are no other errors  
            } elseif ($upload['error'] !== 0) {
                $alerts[] = 'The file could not be uploaded';
            // make sure the file is not larger than 2MB…    
            } elseif ($upload['size'] > 2000000)  {
                $alerts[] = $upload['name'] . ' is larger than 2 MB';
            // …and the file is a PDF
            } elseif ($upload['type'] !== 'application/pdf') {
                $alerts[] = $upload['name'] . ' is not a PDF';
            // all valid, try to rename the temporary file
            } else {
                $name     = $upload['tmp_name'];
                $tmpName  = pathinfo($name);
                // sanitize the original filename
                $filename = $tmpName['dirname']. '/'. F::safeName($upload['name']);

                if (rename($upload['tmp_name'], $filename)) {
                    $name = $filename;
                }
                // add the files to the attachments array
                $attachments[] = $name;
            }  
        }

        // some of the data is invalid
        if($invalid = invalid($data, $rules, $messages)) {
            $alert = $invalid;

            // the data is fine, let's send the email
        } else {
            try {
                $kirby->email([
                    'template' => 'email',
                    'from'     => 'myemail@gmail.com',
                    'replyTo'  => $data['email'],
                    'to'       => 'myemail@gmail.com',
                    'subject'  => esc($data['name']) . ' a message from website',
                    'data'     => [
                        'name'  => esc($data['name']),
                        'company'  => esc($data['company']),
                        'email'  => esc($data['email']),
                        'city'  => esc($data['city']),
                        'state'  => esc($data['state']),
                        'postalcode'  => esc($data['postalcode']),
                        'country'  => esc($data['country']),
                        'text'   => esc($data['text']),
                        'sender' => esc($data['company']),
                        'when' => esc($data['when']),
                        'howlong' => esc($data['howlong']),
                        'source' => esc($data['source']),
                        'target' => esc($data['target'])
                    ],
                    'attachments' => $attachments
                ]);

            } catch (Exception $error) {
                $alert['error'] = "The form could not be sent";
            }

            // no exception occured, let's send a success message
            if (empty($alert) === true) {
                $success = 'Your message has been sent, thank you. We will get back to you soon!';
                $data = [];
            }
        }
    }

    return [
        'alert'   => $alert,
        'data'    => $data ?? false,
        'success' => $success ?? false
    ];
};

Also, is your form exactly like in the recipe?

Oh !! Thank you :slight_smile:
I modified the form code and it works.

<form method="post" action="<?= $page->url() ?>" enctype="multipart/form-data">

Thank you

3 Likes