Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [klasa] mailing
piotrooo89
post
Post #1


Newsman


Grupa: Moderatorzy
Postów: 4 005
Pomógł: 548
Dołączył: 7.04.2008
Skąd: Trzebinia/Kraków




dziękuje za wszelkie porady, uwagi. zabieram się do poprawek.

proszę również o dalsze komentarze.

Odłożyłem na chwile stronicowanie, potrzeba chwili wymogła na mnie zrobienie newslettera. Nie chce zakładać kolejnego tematu więc pokaże go tu. Jest to najprostsze wysyłanie maila, tymczasowo bez nagłówków. Powiedzcie mi czy idę w dobrą stronę.

newsletter-class.php
  1. <?php
  2. class Newsletter
  3. {
  4.    public function GetMailsFromFile($file, $separator)
  5.    {
  6.        $string = file_get_contents($file);
  7.        $mails_file = explode($separator, $string);
  8.        return $mails_file;
  9.    }
  10.    public function GetMailsFromDb($sql)
  11.    {
  12.        $sql_query = mysql_query($sql);
  13.        while ($rows = mysql_fetch_row($sql_query))
  14.        {
  15.            $mails_db[] = $rows[0];
  16.        }
  17.        return $mails_db;
  18.    }
  19.    public function Send($where, $subject, $message)
  20.    {
  21.        foreach($where as $mails)
  22.        {
  23.            mail($mails, $subject, $message);
  24.        }
  25.    }
  26. }
  27. ?>


index.php
  1. <?php
  2. require_once ('newsletter-class.php');
  3. require_once ('mysql.php');
  4.  
  5. $topic = 'Test';
  6. $msg = 'Wiadomość testowa';
  7.  
  8. $sql = 'SELECT mail FROM emails';
  9.  
  10. $news = new Newsletter();
  11. $file = $news -> GetMailsFromFile('mail.txt', ';');
  12. $db = $news -> GetMailsFromDb($sql);
  13. // z pliku
  14. $news -> Send($file, $topic, $msg);
  15. //lub lub bazy
  16. $news -> Send($db, $topic, $msg);
  17. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
piotrooo89
post
Post #2


Newsman


Grupa: Moderatorzy
Postów: 4 005
Pomógł: 548
Dołączył: 7.04.2008
Skąd: Trzebinia/Kraków




Cytat(netvalue @ 11.04.2009, 11:16:33 ) *
1. jak będę posiadał 60k maili w bazie czy pliku (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) ... To myślisz że wszystko gładko wyśle za jednym zamachem (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) ?


tak tak już o tym pomyślałem. dodam odpowiednie funkcje.

Cytat(netvalue @ 11.04.2009, 11:16:33 ) *
2. Może warto byłoby zaopatrzyć maila w nagłówki i autoryzację smtp. przy obecnym stanie większość serwerów oznaczy to flagą SPAM (IMG:http://forum.php.pl/style_emoticons/default/snitch.gif)


nagłóweczki już są choć narazie chce zrobić ogólny zarys klasy i zapytać innych o zdanie.

Cytat(skowron-line @ 11.04.2009, 12:32:16 ) *
@piotrooo89 a dlaczego wymyślasz koło od nowa jest klasa phpMailer ma wszystko czego tobie potrzeba.


tak jak napisał ~phpion:

Cytat(phpion @ 11.04.2009, 12:42:36 ) *
Może dlatego, że się chłopak uczy?


i chyba bardziej cieszy coś własnego (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

a wracając do tematu jak oceniacie? co zmienić, dodać?

aktualnie wygląda to tak:

newsletter.Class.php
  1. <?php
  2. abstract class Loader
  3. {
  4.    abstract public function load();
  5.    public $errors =
  6.        array(
  7.        'no_file' => 'Plik nie istnieje',
  8.        'wrong_separator' => 'Nie podano separatora',
  9.        'wrong_sql' => 'Brak zapytania SQL',
  10.        'wrong_sql_col' => 'Podaj kolumne z adresami e-mail',
  11.        'wrong_sql_tab' => 'Podaj tabele SQL'
  12.        );
  13. }
  14. class Loader_File extends Loader
  15. {
  16.    public function __construct($file, $separator)
  17.    {
  18.        if (empty($separator))
  19.            throw new Exception($this->errors['wrong_separator']);
  20.  
  21.        $this->file = $file;
  22.        $this->sep = $separator;
  23.    }
  24.    public function load()    
  25.    {
  26.        if (!file_exists($this->file))
  27.            throw new Exception($this->errors['no_file']);
  28.            
  29.            $string = file_get_contents($this->file);
  30.            $mails_file = explode($this->sep, $string);
  31.            return $mails_file;
  32.    }
  33. }
  34. class Loader_Database extends Loader
  35. {
  36.    public function __construct($col, $tab)
  37.    {
  38.        if (empty($col) && empty($tab))
  39.            throw new Exception($this->errors['wrong_sql']);
  40.        elseif (empty($col))
  41.            throw new Exception($this->errors['wrong_sql_col']);
  42.        elseif (empty($tab))
  43.            throw new Exception($this->errors['wrong_sql_tab']);
  44.            
  45.        $this->col = $col;
  46.        $this->tab = $tab;
  47.    }
  48.    public function load()
  49.    {
  50.        $sql_query = mysql_query("SELECT `".$this->col."` FROM `".$this->tab."`");
  51.        while ($rows = mysql_fetch_row($sql_query))
  52.        {
  53.            $mails_db[] = $rows[0];
  54.        }
  55.        return $mails_db;
  56.    }
  57. }
  58. class Newsletter
  59. {
  60.    private $loader;
  61.  
  62.    public function setLoader(Loader $loader)
  63.    {
  64.        $this->loader = $loader;
  65.    }
  66.    public function getMails()
  67.    {
  68.        return $this->loader->load();
  69.    }
  70.    public function Send($where, $subject, $message, $head)
  71.    {
  72.        foreach($where as $mails)
  73.        {
  74.            mail($mails, $subject, $message, $head);
  75.        }
  76.    }
  77. }
  78. ?>


index.php
  1. <?php
  2. require_once ('newsletter.Class.php');
  3. require_once ('mysql.php');
  4.  
  5. $topic = 'Test';
  6. $msg = 'Wiadomość testowa';
  7. $headers .= "From: Olaszewski <piotroo89@gmail.com>r\n";
  8. $headers .= "Reply-To: Olaszewski <piotroo89@gmail.com>r\n";
  9. $headers .= "Return-Path: Olaszewski <piotroo89@gmail.com>r\n";
  10. $headers .= 'X-Mailer: PHP/' . phpversion() ."r\n";
  11. $headers .= 'MIME-Version: 1.0'. "\n";
  12. $headers .= 'Content-type: text/html; charset=utf-8' . "r\n";
  13. $headers .= "Content-transfer-encoding: utf-8r\n";
  14.  
  15. $column_sql = 'mail';
  16. $table_sql = 'emails';
  17.  
  18. $f = 'mail.txt';
  19.  
  20. try
  21. {
  22.    $news = new Newsletter();
  23.    $file = new Loader_File($f, ';');
  24.    $db = new Loader_Database($column_sql, $table_sql);
  25.    $news -> setLoader($db);
  26.  
  27.    print_r($news->getMails());
  28.  
  29.    $news -> Send($news->getMails(), $topic, $msg, $headers);
  30. }
  31. catch(Exception $e)
  32. {
  33.    $error = ($e->getMessage());
  34.    echo $error;
  35. }
  36. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 13.10.2025 - 18:50