Rozdział 28. Zend_Mail

Spis treści

28.1. Wprowadzenie
28.1.1. Getting started
28.1.2. Configuring the default sendmail transport
28.2. Wysyłanie przez SMTP
28.3. Wysyłanie wielu maili podczas jednego połączenia SMTP
28.4. Używanie innych transportów
28.5. E-mail w postaci HTML
28.6. Załączniki
28.7. Dodawanie odbiorców
28.8. Pole rozgraniczające MIME
28.9. Dodatkowe nagłówki
28.10. Zestawy znaków
28.11. Kodowanie
28.12. Uwierzytelnianie SMTP
28.13. Bezpieczne połączenie SMTP
28.14. Reading Mail Messages
28.14.1. Simple example using Pop3
28.14.2. Opening a local storage
28.14.3. Opening a remote storage
28.14.4. Fetching messages and simple methods
28.14.5. Working with messages
28.14.6. Checking for flags
28.14.7. Using folders
28.14.8. Advanced Use
28.14.8.1. Using NOOP
28.14.8.2. Caching instances
28.14.8.3. Extending Protocol Classes
28.14.8.4. Using Quota (since 1.5)

28.1. Wprowadzenie

28.1.1. Getting started

Zend_Mail zapewnia możliwość tworzenia i wysyłania tekstowych wiadomości e-mail oraz wieloczęściowych wiadomości e-mail zgodnych z MIME. Wiadomość może być wysłana przez Zend_Mail za pomocą domyślnego transportu Zend_Mail_Transport_Sendmail lub za pomocą Zend_Mail_Transport_Smtp.

Przykład 28.1. Wysyłanie prostego e-maila za pomocą Zend_Mail

Prosty e-mail składa się z odbiorców, z tematu, treści i z nadawcy. Aby wysłać taki e-mail używając Zend_Mail_Transport_Sendmail możesz zrobić to w ten sposób:

$mail = new Zend_Mail();
$mail->setBodyText('Treść wiadomości e-mail.');
$mail->setFrom('somebody@example.com', 'Nadawca');
$mail->addTo('somebody_else@example.com', 'Odbiorca');
$mail->setSubject('Testowy Temat');
$mail->send();

            

[Notatka] Minimalne definicje

Aby wysłać e-mail za pomocą Zend_Mail musisz określić chociaż jednego odbiorcę, nadawcę (np., za pomocą setFrom()), i treść wiadomości (tekst i/lub HTML).

Dla większości atrybutów obiektu mail są dostępne metody "get" w służące do odczytywania przechowywanych w nim informacji. Więcej informacji można znaleść w dokumentacji API. Specjalną metodą jest getRecipients(). Zwraca ona tablicę w wszystkimi adresami e-mail odbiorców, które zostały dodane.

Ze względów bezpieczeństwa, Zend_Mail filtruje wszystkie nagłówki aby zapobiec dołączeniu niechcianych nagłówków za pomocą znaku nowej linii (\n).

You also can use most methods of the Zend_Mail object with a convenient fluent interface. A fluent interface means that each method returns a reference to the object on which it was called, so you can immediately call another method.

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.')
    ->setFrom('somebody@example.com', 'Some Sender')
    ->addTo('somebody_else@example.com', 'Some Recipient')
    ->setSubject('TestSubject')
    ->send();

        

28.1.2. Configuring the default sendmail transport

The default transport for a Zend_Mail instance is Zend_Mail_Transport_Sendmail. It is essentially a wrapper to the PHP mail() function. If you wish to pass additional parameters to the mail() function, simply create a new transport instance and pass your parameters to the constructor. The new transport instance can then act as the default Zend_Mail transport, or it can be passed to the send() method of Zend_Mail.

Przykład 28.2. Passing additional parameters to the Zend_Mail_Transport_Sendmail transport

This example shows how to change the Return-Path of the mail() function.

$tr = new Zend_Mail_Transport_Sendmail('-freturn_to_me@example.com');
Zend_Mail::setDefaultTransport($tr);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('somebody@example.com', 'Some Sender');
$mail->addTo('somebody_else@example.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send();

            

[Notatka] Safe mode restrictions

The optional additional parameters will be cause the mail() function to fail if PHP is running in safe mode.