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




~phpion dzięki za przykład, uczę się dopiero i nie bardzo mi to wychodzi. zrobiłem coś takiego:

  1. <?php
  2. abstract class Loader
  3. {
  4.    abstract public function load();
  5. }
  6. class Loader_File extends Loader
  7. {
  8.    public function load()
  9.    {
  10.        $string = file_get_contents($file);
  11.        $mails_file = explode($separator, $string);
  12.        print_r($mails_file);
  13.    }
  14. }
  15. class Loader_Database extends Loader
  16. {
  17.    public function load()
  18.    {
  19.        $sql_query = mysql_query($sql);
  20.        while ($rows = mysql_fetch_row($sql_query))
  21.        {
  22.            $mails_db[] = $rows[0];
  23.        }
  24.        print_r($mails_db);
  25.    }
  26. }
  27. class Newsletter
  28. {
  29.    private $loader;
  30.  
  31.    public function setLoader(Loader $loader)
  32.    {
  33.        $this->loader = $loader;
  34.    }
  35.  
  36.    public function getMails()
  37.    {
  38.        return $this->loader->load();
  39.    }
  40. }
  41. ?>


ale jest źle. dlaczego? ponieważ nie wiem gdzie mam podać nazwę pliku/query do bazy.

~Cypherq
wiem o nazwach narazie testuje wszytko, później będę wyjątki łapał. narazie chce to uruchomić.

#EDIT

jak podam na sztywno nazwy pliku/query działa.

#EDIT2
zrobiłem tak, powiedzcie co o tym sądzicie:

newsletter-class.php
  1. <?php
  2. abstract class Loader
  3. {
  4.    abstract public function load();
  5. }
  6. class Loader_File extends Loader
  7. {
  8.    public function __construct($file, $separator)
  9.    {
  10.        $this->file = $file;
  11.        $this->sep = $separator;
  12.    }
  13.    public function load()
  14.    {
  15.        $string = file_get_contents($this->file);
  16.        $mails_file = explode($this->sep, $string);
  17.        return $mails_file;
  18.    }
  19. }
  20. class Loader_Database extends Loader
  21. {
  22.    public function __construct($sql)
  23.    {
  24.        $this->sql = $sql;
  25.    }
  26.    public function load()
  27.    {
  28.        $sql_query = mysql_query($this->sql);
  29.        while ($rows = mysql_fetch_row($sql_query))
  30.        {
  31.            $mails_db[] = $rows[0];
  32.        }
  33.        return $mails_db;
  34.    }
  35. }
  36. class Newsletter
  37. {
  38.    private $loader;
  39.  
  40.    public function setLoader(Loader $loader)
  41.    {
  42.        $this->loader = $loader;
  43.    }
  44.  
  45.    public function getMails()
  46.    {
  47.        return $this->loader->load();
  48.    }
  49.    public function Send($where, $subject, $message)
  50.   {
  51.       foreach($where as $mails)
  52.       {
  53.           mail($mails, $subject, $message);
  54.       }
  55.   }
  56. }
  57. ?>


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. $f = 'mail.txt';
  10.  
  11. $news = new Newsletter();
  12. $file = new Loader_File($f, ';');
  13. $db = new Loader_Database($sql);
  14.  
  15. $news -> setLoader($file);
  16.  
  17. print_r($news -> getMails());
  18.  
  19. $news -> Send($news->getMails(), $topic, $msg);
  20. ?>


Ten post edytował piotrooo89 10.04.2009, 20:03:48
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: 9.10.2025 - 01:03