Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [php] Klasa wysyłająca maila, Dodanie nagłówków
Lejto
post 2.07.2009, 11:49:52
Post #1





Grupa: Zarejestrowani
Postów: 1 385
Pomógł: 48
Dołączył: 23.05.2007

Ostrzeżenie: (0%)
-----


Mam klase wysyłającą maila
  1. <?
  2. class Emailer
  3. {
  4.    private $sender;
  5.    private $recipients;
  6.    private $subject;
  7.    private $body;
  8.    
  9.    function __construct($sender)
  10.    {
  11.        $this->sender = $sender;
  12.        $this->recipients = array();
  13.    }
  14.    public function addRecipients($recipient)
  15.    {
  16.        array_push($this->recipients, $recipient);
  17.    }
  18.    public function setSubject($subject)
  19.    {
  20.        $this->subject = $subject;
  21.    }
  22.    public function setBody($body)
  23.    {
  24.        $this->body = $body;
  25.    }
  26.    public function nag($email)
  27.    {
  28.               $header = "From:  <$email>\n";
  29.              $header .= "MIME-Version: 1.0\n";
  30.              $header .= "Content-type: text/html; charset=utf-8\n";
  31.              $header .= "Return-Path: <$email>";
  32.    }
  33.    public function sendEmail()
  34.    {
  35.        
  36.        foreach ($this->recipients as $recipient)
  37.        {
  38.            $result = mail($recipient,$this->subject,$this->body,$this->nag);
  39.            if($result)
  40.            {
  41.            echo '<div id="good">';
  42.            echo 'Wiadomość została wysłana';
  43.            echo '</div>';
  44.             }
  45.        }
  46.    }
  47.    
  48. }
  49. ?>

Dodałem funkcje z nagłówkami nag();
Uruchamiam tak:
  1. <?php
  2. $email = new Emailer($email); //adres od
  3.            $email->addRecipients("agencik@gmail.com"); // adres do
  4.            $email->setSubject($temat);
  5.            $email->setBody($wiadomosc);
  6.            $email->nag($email);
  7.            $email->sendEmail();
  8. ?>

wszystko działało dobrze tylko jak dodałem te nagłówki to otrzymuje taki błąd
Kod
Catchable fatal error: Object of class Emailer could not be converted to string in /home/s/public_html/s-s.eu/mail.class.php on line 28

w lini 28 znajduje się
  1. <?php
  2. $header = "From:  <$email>\n";
  3. ?>


--------------------
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
fander
post 2.07.2009, 12:48:30
Post #2





Grupa: Zarejestrowani
Postów: 231
Pomógł: 22
Dołączył: 6.10.2008

Ostrzeżenie: (0%)
-----


LOOOOL
Kod
<?php
[color="#ff0000"]$email [/color]= new Emailer($email); //adres od
            $email->addRecipients("agencik@gmail.com"); // adres do
            $email->setSubject($temat);
            $email->setBody($wiadomosc);
            [color="#ff0000"]$email->nag($email[/color][color="#ff0000"]);[/color]
            $email->sendEmail();
?>


$email->nag($email); <= nie może być czegoś takiego jak juz kolega powyżej zauważył zmień $email na $stringMail cokolwiek a dlatego to musisz zmienić ponieważ $email wyżej zadeklarowałeś jako Emailer a emaileraa nie da się wyświetlić jak stringa no chyba ze byśmy przeciązyli operatory ale w php nie da się ich przeciążyć.
Go to the top of the page
+Quote Post
phpion
post 3.07.2009, 09:00:58
Post #3





Grupa: Moderatorzy
Postów: 6 072
Pomógł: 861
Dołączył: 10.12.2003
Skąd: Dąbrowa Górnicza




Cytat(fander @ 2.07.2009, 13:48:30 ) *
LOOOOL

...

zadeklarowałeś jako Emailer a emaileraa nie da się wyświetlić jak stringa no chyba ze byśmy przeciązyli operatory ale w php nie da się ich przeciążyć.

LOOOOL a o metodzie __toString kolega nie słyszał? Operatorów nie da się przeciążyć, to prawda, ale można utworzyć metodę zwracającą obiekt w postaci stringu.

@Lejto:
Wydaje mi się, że powinno wystarczyć dodać metodę __toString (co nie rozwiąże problemu z generowaniem nagłówków) i jako tako powinno działać:
  1. <?php
  2. public function __toString() {
  3.        return $this->sender;
  4.    }
  5. ?>

Jednak lepiej by było jakbyś rozwiązał tą sprawę ciut inaczej. Sprawdź ten kod. Powinien działać ale nie sprawdzałem, pisałem na czuja.
  1. <?php
  2. class Emailer
  3. {
  4.   private $sender;
  5.   private $recipients;
  6.   private $subject;
  7.   private $body;
  8.     private $headers = array();
  9.  
  10.   function __construct($sender)
  11.   {
  12.       $this->sender = $sender;
  13.       $this->recipients = array();
  14.            
  15.             $this->addHeader('MIME-Version', '1.0');
  16.             $this->addHeader('Content-type', 'text/html; charset=utf-8');
  17.   }
  18.    
  19.   public function addRecipients($recipient)
  20.   {
  21.       array_push($this->recipients, $recipient);
  22.   }
  23.    
  24.   public function setSubject($subject)
  25.   {
  26.       $this->subject = $subject;
  27.   }
  28.    
  29.   public function setBody($body)
  30.   {
  31.       $this->body = $body;
  32.   }
  33.    
  34.     public function addHeader($name, $value) {
  35.        $this->headers[$name] = $value;
  36.     }
  37.    
  38.     private function getHeadersAsString() {
  39.        $return = '';
  40.        
  41.        foreach ($this->headers as $name => $value) {
  42.            $return .= $name.': '.$value."\n";
  43.        }
  44.        
  45.        return $return;
  46.     }
  47.  
  48.   public function sendEmail()
  49.   {
  50.            $headersAsString = $this->getHeadersAsString();
  51.      
  52.       foreach ($this->recipients as $recipient)
  53.       {
  54.           $result = mail($recipient,$this->subject,$this->body, $headersAsString);
  55.           if($result)
  56.           {
  57.           echo '<div id="good">';
  58.           echo 'Wiadomość została wysłana';
  59.           echo '</div>';
  60.            }
  61.       }
  62.   }
  63.  
  64. }
  65.  
  66. $email = 'nadawca@host.pl'; // od kogo jest mail
  67.  
  68. $emailer = new Emailer($email); //adres od
  69. $emailer->addRecipients("agencik@gmail.com"); // adres do
  70. $emailer->setSubject($temat);
  71. $emailer->setBody($wiadomosc);
  72.  
  73. // dodanie naglowkow
  74. $emailer->addHeader('From', $email);
  75. $emailer->addHeader('Return-Path', $email);
  76.  
  77. $emailer->sendEmail();
  78. ?>


Ten post edytował phpion 3.07.2009, 09:05:03
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 17:15