S
S
Spinto2018-06-27 15:10:03
MODX
Spinto, 2018-06-27 15:10:03

How to automatically send a file to the mail specified in the application of the site?

Hello. There is a task: when sending an application on the site to a visitor, a presentation file should automatically be sent to the entered mail. I'm trying to use the phpMailer built into modx for this.
The application reaches the client, but the file is not sent to the mail entered in the capture form.
I am using this code. How to finish it for this task?

<?php
define( 'MODX_API_MODE' , true ) ;
require_once ( dirname( __FILE__) . '/index.php') ;
if ( empty( $modx )) throw new Exception( 'Core not found', 500);
//куда слать заявки
$sendto = '[email protected]';

# От кого отправляем почту - Имя
$my_name = "leader-a.ru";
# От кого отправляем почту - E-mail
$my_mail = "[email protected]";
# Почта для ответа - E-mail       
$my_replyto = "[email protected]";

$subjectuser = "Заявка с сайта leader-a.ru. Форма 'Пройти тест'";

$mes = "";
$my_file = "";

if( $_POST['user_phone'] == '' ) {
    echo 'false';
  die();
}

foreach($_POST as $k=>$v) {
    switch($k) {


        case 'user_name':
        $mes .= 'Имя: '.$_POST['user_name'].PHP_EOL;
        break;

        case 'user_phone':
        $mes .= 'Телефон: '.$_POST['user_phone'].PHP_EOL;
        break;

        case 'user_email':
        $mes .= 'E-mail: '.$_POST['user_email'].PHP_EOL;
        break;
    }
}

$mes .= 'Отправлено с сайта '.$_SERVER[HTTP_HOST];

if (!empty($_FILES['file']['tmp_name'])) {
    $path = $_FILES['file']['name'];

    if (copy($_FILES['file']['tmp_name'], $path)) {
        $my_file = $path;
    }
}

if (@mail_attachment($my_file, $sendto, $my_mail, $my_name, $my_replyto, $subjectuser, $mes)) {
    echo "true";
} else {
    echo "false";
}

if( $_POST['send'] ) {
    if( !isset($_POST['user_email']) || $_POST['user_email'] == '' ) return false;
    switch($_POST['send']) {
        case 0;
        case '0':
            return false;
        break;
        case 1:
        case '1':
            $bodyMessage = "Презентация прикреплена к письму." . PHP_EOL . "Остались вопросы звоните на мобильный +7 984 777-04-54 "; //текст для презентации
            $file = dirname(__FILE__).'/WONDERBEAT_Prezentatsia_franshizy.pdf';
        break;
        default:
            return false;
        break;
    }
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY, $bodyMessage);
    $modx->mail->set(modMail::MAIL_FROM,'[email protected]');
    $modx->mail->set(modMail::MAIL_FROM_NAME,'leader-a.ru');
    $modx->mail->set(modMail::MAIL_SUBJECT,'Презентация нашей фирмы');
    $modx->mail->address('to', $_POST['user_email']);
    $modx->mail->address('reply-to','[email protected]');
    $modx->mail->attach($file);
    $modx->mail->setHTML(true);
    if (!$modx->mail->send()) {
        $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
    }
    $modx->mail->reset();
}


function mail_attachment( $filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message )
{
    if ( ! empty( $filename ) )
    {
        $file = $filename;

        if ( file_exists( $file ) )
        {
            $file_size = filesize( $file );

            $handle = fopen( $file, "r" );
            $content = fread( $handle, $file_size );
            fclose( $handle );

            $content = chunk_split( base64_encode( $content ) );
            $name = basename( $file );

            unlink( $file );
        }
        else
        {
            unset( $filename );
        }
    }

    $uid = md5( uniqid( time() ) );

    $header = "From: " . $from_name . " <" . $from_mail . ">" . PHP_EOL;
    $header .= "Reply-To: " . $replyto . PHP_EOL;
    $header .= "MIME-Version: 1.0" . PHP_EOL;
    $header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"" . PHP_EOL . PHP_EOL;

    $nmessage = "--" . $uid . PHP_EOL;
    $nmessage .= "Content-type:text/plain; charset=utf-8 " . PHP_EOL;
    $nmessage .= "Content-Transfer-Encoding: 7bit" . PHP_EOL . PHP_EOL;
    $nmessage .= $message . PHP_EOL . PHP_EOL;

    if ( ! empty( $filename ) )
    {
        $nmessage .= "--" . $uid . PHP_EOL;
        $nmessage .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . PHP_EOL;
        $nmessage .= "Content-Transfer-Encoding: base64" . PHP_EOL;
        $nmessage .= "Content-Disposition: attachment; filename=\"" . $filename . "\"" . PHP_EOL . PHP_EOL;
        $nmessage .= $content . PHP_EOL . PHP_EOL;
        $nmessage .= "--" . $uid . "--";
    }

    return mail( $mailto, $subject, $nmessage, $header );
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Lyskov, 2018-06-27
@Vlatqa

<form enctype="multipart/form-data" method="post">?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question