Advertisement
thebys

METEOLOG - mailing.php

Jan 16th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.35 KB | None | 0 0
  1. <?php
  2. //
  3. //třída AttachmentEmail pochází z tutoriálu: http://www.kavoir.com/2009/08/php-email-attachment-class.html
  4. //následující kód umožňuje odeslání libovolných souborů jako přílohu k emailu na
  5. //uvedenou adresu.
  6. //EDIT: diakritika v předmětu se nezobrazuje korektně, nicméně v textu emailu ano.
  7. //EDIT: příchozí soubory jsou v pořádku.
  8. //
  9. class AttachmentEmail {
  10.     private $from = 'meteolog@iver.cz';
  11.     private $from_name = 'J. K. Meteofrog';
  12.     private $reply_to = 'support@iver.cz';
  13.     private $to = '';
  14.     private $subject = '';
  15.     private $message = '';
  16.     private $attachment = '';
  17.     private $attachment_filename = '';
  18.  
  19.     public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
  20.         $this -> to = $to;
  21.         $this -> subject = $subject;
  22.         $this -> message = $message;
  23.         $this -> attachment = $attachment;
  24.         $this -> attachment_filename = $attachment_filename;
  25.     }
  26.  
  27.     public function mail() {
  28.         if (!empty($this -> attachment)) {
  29.             $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
  30.             $path = dirname($this -> attachment);
  31.             $mailto = $this -> to;
  32.             $from_mail = $this -> from;
  33.             $from_name = $this -> from_name;
  34.             $replyto = $this -> reply_to;
  35.             $subject = $this -> subject;
  36.             $message = $this -> message;
  37.  
  38.             $file = $path.'/'.$filename;
  39.             $file_size = filesize($file);
  40.             $handle = fopen($file, "r");
  41.             $content = fread($handle, $file_size);
  42.             fclose($handle);
  43.             $content = chunk_split(base64_encode($content));
  44.             $uid = md5(uniqid(time()));
  45.             $name = basename($file);
  46.             $header = "From: ".$from_name." <".$from_mail.">\r\n";
  47.             $header .= "Reply-To: ".$replyto."\r\n";
  48.             $header .= "MIME-Version: 1.0\r\n";
  49.             $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
  50.             $header .= "This is a multi-part message in MIME format.\r\n";
  51.             $header .= "--".$uid."\r\n";
  52.             $header .= "Content-type:text/plain; charset=utf-8\r\n";
  53.             $header .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
  54.             $header .= $message."\r\n\r\n";
  55.             $header .= "--".$uid."\r\n";
  56.             $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
  57.             $header .= "Content-Transfer-Encoding: base64\r\n";
  58.             $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
  59.             $header .= $content."\r\n\r\n";
  60.             $header .= "--".$uid."--";
  61.  
  62.             if (mail($mailto, $subject, "", $header)) {
  63.                 return true;
  64.             } else {
  65.                 return false;
  66.             }
  67.         } else {
  68.             $header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
  69.             $header .= "Reply-To: ".($this -> reply_to)."\r\n";
  70.  
  71.             if (mail($this -> to, $this -> subject, $this -> message, $header)) {
  72.                 return true;
  73.             } else {
  74.                 return false;
  75.             }
  76.  
  77.         }
  78.     }
  79. }
  80. //
  81. //Vlastní kód, využívající výše zapsanou třídu voláním metody AttachementEmail
  82. //
  83. $predmet = "Data meteologu";
  84. $text = "Dobrý den,\n\n na webu meteologu jste si vy, nebo někdo vaší emailovou adresou\n vyžádali zaslání databáze záznamů na tuto emailovou adresu. \n Databázi najdete v příloze ve formátu .CSV. \n\n S pozdravem team meteolog(.iver).cz";
  85. $sendit = new AttachmentEmail($_POST['mail'], $predmet, $text, 'data.csv');
  86. if($sendit -> mail()){header('Location: succes.php');}else{header('Location: fail.php');}
  87. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement