19
Jean-Marie Renouard LightPath 2014©

Email et PHP5

Embed Size (px)

DESCRIPTION

Présentation de plusieurs clients Email pour PHP 5

Citation preview

Page 1: Email et PHP5

Jean-Marie Renouard

LightPath 2014©

Page 2: Email et PHP5

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

Page 3: Email et PHP5

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

Page 4: Email et PHP5

LightPath 2014© - http://www.jmrenouard.fr 4

Page 5: Email et PHP5

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

Page 6: Email et PHP5

LightPath 2014© - http://www.jmrenouard.fr 6

Page 7: Email et PHP5

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

Page 8: Email et PHP5

$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

Page 9: Email et PHP5

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

Page 10: Email et PHP5

API PHP trop pauvre

Utilisation API tiers préconisés.

LightPath 2014© - http://www.jmrenouard.fr 10

Page 11: Email et PHP5

LightPath 2014© - http://www.jmrenouard.fr 11

Page 12: Email et PHP5

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

Page 13: Email et PHP5

$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

Page 14: Email et PHP5

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

Page 15: Email et PHP5

LightPath 2014© - http://www.jmrenouard.fr 15

Page 16: Email et PHP5

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

Page 17: Email et PHP5

$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

Page 18: Email et PHP5

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

Page 19: Email et PHP5

LightPath: ◦ Société de conseil et d’ingénierie

◦ Formations, Conseil, Audit et mise en œuvre

[email protected]

Jean-Marie RENOUARD ◦ [email protected]

◦ Twitter: @jmrenouard

◦ http://www.jmrenouard.fr

LightPath 2014© - http://www.jmrenouard.fr 19