Spinto
@Spinto

Как автоматически отправить файл на почту, указанную в заявке сайта?

Здравствуйте. Есть задача: при отправке заявки на сайте посетителю автоматически на введенную почту почту должен падать файл презентации. Пытаюсь использовать для этого встроенный в modx phpMailer.
Заявка доходит до клиента, но на почту, введенную в форму захвата, отправка файла не происходит.
Использую этот код. Как его допилить под эту задачу?
<?php
define( 'MODX_API_MODE' , true ) ;
require_once ( dirname( __FILE__) . '/index.php') ;
if ( empty( $modx )) throw new Exception( 'Core not found', 500);
//куда слать заявки
$sendto = 'evgenii_krishtopa@mail.ru';

# От кого отправляем почту - Имя
$my_name = "leader-a.ru";
# От кого отправляем почту - E-mail
$my_mail = "eugene.krishtopa@yandex.ua";
# Почта для ответа - E-mail       
$my_replyto = "eugene.krishtopa@yandex.ua";

$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,'eugene.krishtopa@yandex.ua');
    $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','eugene.krishtopa@yandex.ua');
    $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 );
}
  • Вопрос задан
  • 420 просмотров
Пригласить эксперта
Ответы на вопрос 1
<form enctype="multipart/form-data" method="post"> ?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы