За последние 24 часа нас посетили 54047 программистов и 1799 роботов. Сейчас ищут 1073 программиста ...

Проблема с отсылкой почты с аттачем

Тема в разделе "PHP для новичков", создана пользователем White Wolf, 18 сен 2009.

  1. White Wolf

    White Wolf Активный пользователь

    С нами с:
    5 авг 2009
    Сообщения:
    29
    Симпатии:
    0
    Адрес:
    Томск
    Здравствуйте!
    Посылаю почту с прикреплением к ней xml и jpg файлов. xml приходит на почту а jpg нет. Вот код:
    Код (Text):
    1.  
    2. <?php
    3. // Класс для отправки почтовых сообщений
    4. class Mailer
    5. {
    6. var $subject;       // (string) Тема
    7. var $text;          // (string) Текст сообщения (txt-вариант)
    8. var $html;          // (string) Текст сообщения (html-вариант)
    9. var $from;          // (string) От кого
    10. var $to;            // (string) Кому
    11. var $charset;       // (string) Кодировка (по умолчанию Windows-1251)
    12.  
    13. var $sHeaders;       // (string)
    14. var $sBody;          // (string)
    15. var $sContentType;   // (string)
    16. var $sHtmlTemplate;  // (string)
    17. var $sBoundary;      // (string)
    18. var $aAttaches;      // (array)
    19.  
    20. // Конструктор класса
    21. function Mailer()
    22.          {
    23.          $this->charset      = 'Windows-1251';
    24.          $this->aAttaches     = array();
    25.          $this->sBoundary     = '----'.substr(md5(uniqid(rand(),true)),0,16);
    26.          $this->sHtmlTemplate = '<html><head><title>{title}</title></head><body>{body}</body></html>';
    27.          }
    28.  
    29. // Добавить заголовок
    30. function DoHeader($sHeader)
    31.          {
    32.          $this->sHeaders .= $sHeader."\r\n";
    33.          }
    34.  
    35. // Прикрепить файл
    36. function Attach($sPath,$mimeType)
    37.          {
    38.          if (file_exists($sPath))
    39.             {
    40.             $sName=basename($sPath);
    41.             $sAttach ="Content-Type: $mimeType; name=\"$sName\"\r\n";
    42.             $sAttach.="Content-Disposition: attachment; filename=\"$sName\"\r\n";
    43.             $sAttach.="Content-Transfer-Encoding: base64\r\n";
    44.             $sAttach.="\r\n";
    45.             $sAttach.=base64_encode(file_get_contents($sPath))."\r\n";
    46.             $this->aAttaches[] = $sAttach;
    47.             }
    48.          }
    49.  
    50. // Добавить HTML
    51. function AddHtml($sHtml)
    52.          {
    53.          $this->html.=$sHtml."\r\n";
    54.          }
    55.  
    56. // Установить шаблон
    57. function SetTemplate($sPath)
    58.          {
    59.          if (file_exists($sPath)) $this->sHtmlTemplate = file_get_contents($sPath);
    60.          }
    61. // Отправить
    62. function Send()
    63.          {
    64.          $iCountAtt=count($this->aAttaches);
    65.          $this->sHeaders ="From: {$this->from}\r\n";
    66.          $this->sHeaders.="MIME-Version: 1.0\r\n";
    67.          if (!$this->html && !$iCountAtt)
    68.             {
    69.             $this->sHeaders.='Content-Type: text/plain; charset='.$this->charset."\r\n";
    70.             $this->sBody = $this->text;
    71.             }
    72.          elseif ($this->html && !$iCountAtt)
    73.                 {
    74.                 $this->sHeaders.='Content-Type: text/html; charset='.$this->charset."\r\n";
    75.                 $aFields=array();
    76.                 $aFields['{title}'] = $this->subject;
    77.                 $aFields['{body}']  = $this->html;
    78.                 $this->sBody = strtr($this->sHtmlTemplate,$aFields);
    79.                 }
    80.          elseif (!$this->html && $iCountAtt)
    81.                 {
    82.                 $this->sHeaders.="Content-Type: multipart/mixed; boundary=\"{$this->sBoundary}\"\r\n";
    83.                 foreach ($this->aAttaches as $sAttach)
    84.                         {
    85.                         $this->sBody .= "--{$this->sBoundary}\r\n";
    86.                         $this->sBody .= $sAttach;
    87.                         }
    88.                 $this->sBody .= "--{$this->sBoundary}--\r\n";
    89.                 }
    90.          elseif ($this->html && $iCountAtt)
    91.                 {
    92.                 $this->sHeaders.="Content-Type: multipart/mixed; boundary=\"{$this->sBoundary}\"\r\n";
    93.                 $this->sBody .= "--{$this->sBoundary}\r\n";
    94.                 $this->sBody .= "Content-Type: text/html; charset={$this->charset}\r\n";
    95.                 $this->sBody .= "Content-Transfer-Encoding: 8bit\r\n";
    96.                 $this->sBody .= "\r\n";
    97.                 $aFields=array();
    98.                 $aFields['{title}'] = $this->subject;
    99.                 $aFields['{body}']  = $this->html;
    100.                 $this->sBody .= strtr($this->sHtmlTemplate,$aFields);
    101.                 foreach ($this->aAttaches as $sAttach)
    102.                         {
    103.                         $this->sBody .= "--{$this->sBoundary}\r\n";
    104.                         $this->sBody .= $sAttach;
    105.                         }
    106.                 $this->sBody .= "--{$this->sBoundary}--\r\n";
    107.                 }
    108.          @mail($this->to, $this->subject, $this->sBody, $this->sHeaders);
    109.          }
    110.  
    111. } // End of class Mailer
    112.  
    113. $number_phone = "xxx xx xxxx";
    114. $email = "mymail@email.com";
    115. $Message = new Mailer();
    116. $Message->from    = 'Ot Menya <ot@menya.ru>';
    117. $Message->to      = 'harapaevaa@sibmail.com';
    118. $Message->subject = 'Форма одежды';
    119. $Message->charset = 'Windows-1251';
    120.  
    121. $Message->html    = '<br /><p><h4>Контактная информация:</h4></p>
    122.                      <p>телефон: '.$number_phone.'<br />email: '.$email.'</p>';
    123. $Message->Attach('038.jpg','image/jpg');
    124. $Message->Attach('xmldate_clothes.xml','application/xml');
    125.  
    126. $Message->Send();
    127. ?>
    При этом выдает такое сообщение в почте:
    Код (Text):
    1.  
    2. Форма одежды
    3.  
    4. Контактная информация:
    5.                      
    6. телефон: xxx xx xxxx
    7. email: mymail@email.com------84f87f2a5ebdb473
    8. Content-Type: image/gif; name="038.jpg"
    9. Content-Disposition: attachment; filename="038.jpg"
    10. Content-Transfer-Encoding: base64
    11.  
    12. R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7
    При этом еще и проблема с кодировкой вылазит. Причем почему не понимаю, потому что кодировка windows-1251 указана.
    Если я делаю три вложения к тексту, то первое не доходит, а второе и третье доходит. Что тут не так? Помогите пожалуйста.
     
  2. LuckyScrat

    LuckyScrat Активный пользователь

    С нами с:
    16 июн 2009
    Сообщения:
    176
    Симпатии:
    0
    Адрес:
    Москва
    в загаловке gif, в реальности jpg...
     
  3. White Wolf

    White Wolf Активный пользователь

    С нами с:
    5 авг 2009
    Сообщения:
    29
    Симпатии:
    0
    Адрес:
    Томск
    а это я описался когда набирал текст. Вообще там image/jpg. первое вложение не доходит в любом случае, а как я понимаю отображается в виде такоф ерунды
    Код (Text):
    1. R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7