How to upload as attachments in email

Asked

Viewed 37 times

0

I am trying to make a form that sends an email with the form inputs and files that the user uploads. The part of sending inputs is already ready, I’m using the Magic Form (Martin M.) in the Octobercms framework, but the function of uploading files from Magic Form does not work, so I’m trying to do it by hand, but so far I have not had any success. The Magic Form "file-multi.htm" file code is this:

<div
class="responsiv-uploader-fileupload style-file-multi is-multi {{ __SELF__.isPopulated() ? 'is-populated' }}"
data-control="fileupload"
data-template="#uploaderTemplate{{ __SELF__ }}"
data-unique-id="{{ __SELF__ }}"
{% if __SELF__.fileTypes %}data-file-types="{{ __SELF__.fileTypes }}"{% endif %}

<!-- Field placeholder -->
<input type="hidden" name="_uploader[{{ __SELF__.attribute }}]" value="" />

<!-- Upload Button -->
<button type="button" class="ui button btn btn-default oc-icon-upload upload-button">
    {{ __SELF__.placeholderText }}
</button>


<!-- Existing files -->
<div class="upload-files-container">
    {% for file in __SELF__.fileList %}
        <div class="upload-object is-success" data-id="{{ file.id }}" data-path="{{ file.pathUrl }}">
            <div class="icon-container">
                {% if file.isImage %}
                    <img src="{{ file.thumbUrl }}" alt="" />
                {% else %}
                    <img src="{{ 'plugins/martin/forms/assets/imgs/upload.png'|app }}" alt="" />
                {% endif %}
            </div>
            <div class="info">
                <h4 class="filename">
                    <span data-dz-name>{{ file.title ?: file.file_name }}</span>
                </h4>
                <p class="size">{{ file.sizeToString }}</p>
            </div>
            <div class="meta">
                <a
                    href="javascript:;"
                    class="upload-remove-button"
                    data-request="{{ __SELF__ ~ '::onRemoveAttachment' }}"
                    data-request-confirm="{{ __SELF__.removeText }}"
                    data-request-data="file_id: {{ file.id }}"
                    >&times;</a>
            </div>
        </div>
    {% endfor %}
</div>
×

and the file "Sendmail.php" is this:

<?php
namespace Martin\Forms\Classes;
use Mail;
use System\Models\MailTemplate;
use Martin\Forms\Classes\BackendHelpers;

class SendMail {

    public static function sendNotification($properties, $post, $record, $files) {

        // CHECK IF THERE IS AT LEAST ONE MAIL ADDRESS
        if (!isset($properties['mail_recipients'])) {
            $properties['mail_recipients'] = false;
        }

        // CHECK IF THERE IS AT LEAST ONE MAIL ADDRESS
        if (!isset($properties['mail_bcc'])) {
            $properties['mail_bcc'] = false;
        }

        if (is_array($properties['mail_recipients']) || is_array($properties['mail_bcc'])) {

            // CUSTOM TEMPLATE
            $template = isset($properties['mail_template']) && $properties['mail_template'] != '' && MailTemplate::findOrMakeTemplate($properties['mail_template']) ? $properties['mail_template'] : 'martin.forms::mail.notification';

            $data = [
                'id'   => $record->id,
                'data' => $post,
                'ip'   => $record->ip,
                'date' => $record->created_at
            ];

            // CHECK FOR CUSTOM SUBJECT
            if (isset($properties['mail_subject'])) {

                // set date format
                $dateFormat = $properties['emails_date_format'] ?? 'Y-m-d';

                // REPLACE RECORD TOKENS IN SUBJECT
                $properties['mail_subject'] = BackendHelpers::replaceToken('record.id', $data['id'], $properties['mail_subject']);
                $properties['mail_subject'] = BackendHelpers::replaceToken('record.ip', $data['ip'], $properties['mail_subject']);
                $properties['mail_subject'] = BackendHelpers::replaceToken('record.date', date($dateFormat), $properties['mail_subject']);

                // REPLACE FORM FIELDS TOKENS IN SUBJECT
                foreach ($data['data'] as $key => $value) {
                    if (!is_array($value)) {
                        $properties['mail_subject'] = BackendHelpers::replaceToken('form.'.$key, $value, $properties['mail_subject']);
                    }
                }

                // SET CUSTOM SUBJECT
                $data['subject'] = $properties['mail_subject'];

            }

            // SEND NOTIFICATION EMAIL
            Mail::sendTo($properties['mail_recipients'], $template, $data, function ($message) use ($properties, $post, $files) {

                // SEND BLIND CARBON COPY
                if (isset($properties['mail_bcc']) && is_array($properties['mail_bcc'])) {
                    $message->bcc($properties['mail_bcc']);
                }

                // USE CUSTOM SUBJECT
                if (isset($properties['mail_subject'])) {
                    $message->subject($properties['mail_subject']);
                }

                // ADD REPLY TO ADDRESS
                if (isset($properties['mail_replyto']) && isset($post[$properties['mail_replyto']])) {
                    $message->replyTo($post[$properties['mail_replyto']]);
                }

                // ADD UPLOADS
                if (isset($properties['mail_uploads']) && $properties['mail_uploads'] && !empty($files)) {
                    foreach ($files as $file) {
                      /*  $message->attach($file->getLocalPath(), ['as' => $file->getFilename()]);   */ $message->attach( Input::file('attachment') );
                    }
                }

            });

        }

    }

    public static function sendAutoResponse($properties, $post, $record) {

        $data = [
            'id'   => $record->id,
            'data' => $post,
            'ip'   => $record->ip,
            'date' => $record->created_at
        ];

        // CHECK FOR CUSTOM SUBJECT
        if (isset($properties['mail_resp_subject'])) {

            // set date format
            $dateFormat = $properties['emails_date_format'] ?? 'Y-m-d';

            // REPLACE RECORD TOKENS IN SUBJECT
            $properties['mail_resp_subject'] = BackendHelpers::replaceToken('record.id', $data['id'], $properties['mail_resp_subject']);
            $properties['mail_resp_subject'] = BackendHelpers::replaceToken('record.ip', $data['ip'], $properties['mail_resp_subject']);
            $properties['mail_resp_subject'] = BackendHelpers::replaceToken('record.date', date($dateFormat), $properties['mail_resp_subject']);

            // REPLACE FORM FIELDS TOKENS IN SUBJECT
            foreach ($data['data'] as $key => $value) {
                if (!is_array($value)) {
                    $properties['mail_resp_subject'] = BackendHelpers::replaceToken('form.'.$key, $value, $properties['mail_resp_subject']);
                }
            }
        }

        $response = isset($properties['mail_resp_field']) ? $properties['mail_resp_field'] : null;
        $to       = isset($post[$response]) ? $post[$response] : null;
        $from     = isset($properties['mail_resp_from']) ? $properties['mail_resp_from'] : null;
        $subject  = isset($properties['mail_resp_subject']) ? $properties['mail_resp_subject'] : null;

        if (filter_var($to, FILTER_VALIDATE_EMAIL) && filter_var($from, FILTER_VALIDATE_EMAIL)) {

            // CUSTOM TEMPLATE
            $template = isset($properties['mail_resp_template']) && $properties['mail_resp_template'] != '' && MailTemplate::findOrMakeTemplate($properties['mail_resp_template']) ? $properties['mail_resp_template'] : 'martin.forms::mail.autoresponse';

            Mail::sendTo($to, $template, [
                    'id'   => $record->id,
                    'data' => $post,
                    'ip'   => $record->ip,
                    'date' => $record->created_at
                ], function ($message) use ($from, $subject) {
                    $message->from($from);
                    if (isset($subject)) {
                        $message->subject($subject);
                    }
                }
            );

        }

    }

}

?>
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.