Email et PHP5

Preview:

DESCRIPTION

Présentation de plusieurs clients Email pour PHP 5

Citation preview

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(‘destinataire@example.com', 'Mon Sujet', $message);

?>

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

$to = 'aidan@example.com' . ', '; // notez la virgule $to .= 'wez@example.com'; // En-têtes additionnels $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@exa

mple.com>' . "\r\n"; $headers .= 'From: Anniversaire <anniversaire@example.com>'

. "\r\n"; $headers .= 'Cc: anniversaire_archive@example.com' . "\r\n"; $headers .= 'Bcc: anniversaire_verif@example.com' . "\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('your@example.com'); $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('a@gmail.com', 'Server');

$mail->addTo($to, 'a@gmail.com');

$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"=>"me@example.com",

"Subject"=>"Test Mail«

);

$body = "This is a test! » ;

$mail->send("best@friend.com", $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('your@example.com'); $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('a@gmail.com', 'Server');

$mail->addTo($to, 'a@gmail.com');

$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"=>"me@example.com",

"Subject"=>"Test Mail »

);

$body = "This is a test! » ;

$ msg ->addAttachment(« mon Fichier.txt »);

$mail =& Mail::factory('mail');

$mail->send("best@friend.com", $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

◦ jmrenouard@lightpath.fr

Jean-Marie RENOUARD ◦ jmrenouard@gmail.com

◦ Twitter: @jmrenouard

◦ http://www.jmrenouard.fr

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

Recommended