email et php5
DESCRIPTION
Présentation de plusieurs clients Email pour PHP 5TRANSCRIPT
Jean-Marie Renouard
LightPath 2014©
Le logo PHP est du domaine public http://commons.wikimedia.org/wiki/File:PHP-logo.svg
Ce document est licencié sous licence ◦ Attribution-NonCommercial-ShareAlike
◦ CC BY-NC-SA
Plus de détails: http://creativecommons.org/licenses/by-nc-sa/3.0/fr/
LightPath 2014© - http://www.jmrenouard.fr 2
Configuration PHP
Envoyer un message simple
Envoyer un message avec des copies
Envoyer un message en HTML
Autres APIs Mail
Envoyer un message avec pièces jointes
LightPath 2014© - http://www.jmrenouard.fr 3
LightPath 2014© - http://www.jmrenouard.fr 4
API mail supporte SMTP
La configuration se trouve dans php.ini
[sendmail] smtp_server=mail.yourdomain.com smtp_port=25
LightPath 2014© - http://www.jmrenouard.fr 5
LightPath 2014© - http://www.jmrenouard.fr 6
Mail: fonction principale d’envoi de mail <?php // Le message $message = "Line 1\nLine 2\nLine 3"; $message = wordwrap($message, 70); // Envoi du mail
mail(‘[email protected]', 'Mon Sujet', $message);
?>
LightPath 2014© - http://www.jmrenouard.fr 7
$to = '[email protected]' . ', '; // notez la virgule $to .= '[email protected]'; // En-têtes additionnels $headers .= 'To: Mary <[email protected]>, Kelly <kelly@exa
mple.com>' . "\r\n"; $headers .= 'From: Anniversaire <[email protected]>'
. "\r\n"; $headers .= 'Cc: [email protected]' . "\r\n"; $headers .= 'Bcc: [email protected]' . "\r\n";
// Envoi mail($to, $subject, $message, $headers);
LightPath 2014© - http://www.jmrenouard.fr 8
Le message en HTML
◦ $message = ’<html>…</html>’;
Les entêtes MAIL pour l’HTML
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
L’envoi du email
mail($to, $subject, $message, $headers);
LightPath 2014© - http://www.jmrenouard.fr 9
API PHP trop pauvre
Utilisation API tiers préconisés.
LightPath 2014© - http://www.jmrenouard.fr 10
LightPath 2014© - http://www.jmrenouard.fr 11
foreach ($list as $name => $address) { $this->email->clear(); $this->email->to($address); $this->email->from('[email protected]'); $this->email->subject(‘petit message '.$name); $this->email->message(‘Bonjour '.$name.' , et bonne journée.'); $this->email->send(); }
LightPath 2014© - http://www.jmrenouard.fr 12
$tr = new Zend_Mail_Transport_Smtp(‘smtp.gmail.com');
$mail->setFrom('[email protected]', 'Server');
$mail->addTo($to, '[email protected]');
$mail->setSubject($subject);
$mail->setBodyText($body);
$mail->send();
LightPath 2014© - http://www.jmrenouard.fr 13
include('Mail.php');
$mail = Mail::factory("mail");
$headers = array(
"From"=>"[email protected]",
"Subject"=>"Test Mail«
);
$body = "This is a test! » ;
$mail->send("[email protected]", $headers, $body);
LightPath 2014© - http://www.jmrenouard.fr 14
LightPath 2014© - http://www.jmrenouard.fr 15
foreach ($list as $name => $address) { $this->email->clear(); $this->email->to($address); $this->email->from('[email protected]'); $this->email->subject(‘petit message '.$name); $this->email->message(‘Bonjour '.$name.' , et bonne journée.');
$this->email->attach('/path/to/photo1.jpg'); $this->email->send(); }
LightPath 2014© - http://www.jmrenouard.fr 16
$mail = new Zend_Mail();
$mail->setFrom('[email protected]', 'Server');
$mail->addTo($to, '[email protected]');
$mail->setSubject($subject);
$mail->setBodyText($body);
$mail->createAttachment($someBinaryString);
$mail->createAttachment($myImage,
'image/gif',
Zend_Mime::DISPOSITION_INLINE,
Zend_Mime::ENCODING_BASE64);
$mail->send();
LightPath 2014© - http://www.jmrenouard.fr 17
include('Mail.php');
include_once('Mail_Mime/mime.php');
$msg = new Mail_mime() ;
$headers = array(
"From"=>"[email protected]",
"Subject"=>"Test Mail »
);
$body = "This is a test! » ;
$ msg ->addAttachment(« mon Fichier.txt »);
$mail =& Mail::factory('mail');
$mail->send("[email protected]", $headers, $body);
LightPath 2014© - http://www.jmrenouard.fr 18
LightPath: ◦ Société de conseil et d’ingénierie
◦ Formations, Conseil, Audit et mise en œuvre
Jean-Marie RENOUARD ◦ [email protected]
◦ Twitter: @jmrenouard
◦ http://www.jmrenouard.fr
LightPath 2014© - http://www.jmrenouard.fr 19