Table des matières

Introduction

Cette page regroupe divers ressources utiles trouvées sur le net.

Liens Utiles

Sur l'authentification HTTP

http://fr2.php.net/manual/fr/features.http-auth.php

Sur le header HTTP

http://fr2.php.net/manual/fr/function.header.php

Notamment l'exemple utile:

<?php
// Vous voulez afficher un pdf
header('Content-type: application/pdf');
 
// Il sera nommé downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
 
// Le source du PDF original.pdf
readfile('original.pdf');
?> 

Mail avec pièces jointes

http://www.toutestfacile.com/php/cours/mail_3.php5

<html>
<body>
<?php
 
//----------------------------------
// Construction de l'entête
//----------------------------------
$boundary = "-----=".md5(uniqid(rand()));
 
$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
$header .= "\r\n";
 
//--------------------------------------------------
// Construction du message proprement dit
//--------------------------------------------------
 
$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";
 
//---------------------------------
// 1ère partie du message
// Le texte
//---------------------------------
$msg .= "--$boundary\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding:8bit\r\n";
$msg .= "\r\n";
$msg .= "Ceci est un mail avec 2 fichiers joints\r\n";
$msg .= "\r\n";
 
//---------------------------------
// 2nde partie du message
// Le 1er fichier (inline)
//---------------------------------
$file = "monfichier1.gif";
$fp   = fopen($file, "rb");   // le b c'est pour les windowsiens
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));
 
$msg .= "--$boundary\r\n";
$msg .= "Content-Type: image/gif; name=\"$file\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: inline; filename=\"$file\"\r\n";
$msg .= "\r\n";
$msg .= $attachment . "\r\n";
$msg .= "\r\n\r\n";
 
//---------------------------------
// 3ème partie du message
// Le 2ème fichier (attachment)
//---------------------------------
$file = "monfichier2.gif";
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));
 
$msg .= "--$boundary\r\n";
$msg .= "Content-Type: image/gif; name=\"$file\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
$msg .= "\r\n";
$msg .= $attachment . "\r\n";
$msg .= "\r\n\r\n";
 
$msg .= "--$boundary--\r\n";
 
$destinataire = "testemail@toutestfacile.com";
$expediteur   = "moi@monsite.com";
$reponse      = $expediteur;
echo "Ce script envoie un mail avec 2 fichiers joints à $destinataire";
mail($destinataire,
     "Email avec 2 fichiers joints (dont 1 inline)",
     $msg,
     "Reply-to: $reponse\r\nFrom: $destinataire\r\n".$header);
?>
</body>
</html>

Mail avec images jointes

http://www.toutestfacile.com/php/cours/mail_4.php5

Cela ressemble à l'envoi d'un mail avec un ou plusieurs fichiers joints. Il suffit simplement d'indiquer que le premier bloc est de type text/html, d'ajouter pour chaque fichier joint un entête Content-ID précisant l'identifiant du fichier dans le mail et de préciser cet identifiant dans les balises <img src=”…”> en les faisant précéder de cid:.

<html>
<body>
<?php
 
//----------------------------------
// Construction de l'entête
//----------------------------------
$delimiteur = "-----=".md5(uniqid(rand()));
 
$entete = "MIME-Version: 1.0\r\n";
$entete .= "Content-Type: multipart/related; boundary=\"$delimiteur\"\r\n";
$entete .= "\r\n";
 
//--------------------------------------------------
// Construction du message proprement dit
//--------------------------------------------------
 
$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";
 
//---------------------------------
// 1ère partie du message
// Le code HTML
//---------------------------------
$msg .= "--$delimiteur\r\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding:8bit\r\n";
$msg .= "\r\n";
$msg .= "<html><body><h1>Email HTML avec 2 images</h1>";
$msg .= "Image 1:<img src=\"cid:image1\"><br />";
$msg .= "Image 2:<img src=\"cid:image2\"><br /></body></html>\r\n";
$msg .= "\r\n";
 
//---------------------------------
// 2nde partie du message
// Le 1er fichier (inline)
//---------------------------------
$fichier = "monfichier.jpg";
$fp      = fopen($fichier, "rb");
$fichierattache = fread($fp, filesize($fichier));
fclose($fp);
$fichierattache = chunk_split(base64_encode($fichierattache));
 
$msg .= "--$delimiteur\r\n";
$msg .= "Content-Type: application/octet-stream; name=\"$fichier\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-ID: <image1>\r\n";
$msg .= "\r\n";
$msg .= $fichierattache . "\r\n";
$msg .= "\r\n\r\n";
 
//---------------------------------
// 3ème partie du message
// Le 2ème fichier (attachment)
//---------------------------------
$fichier = "monfichier2.jpg";
$fp      = fopen($fichier, "rb");
$fichierattache = fread($fp, filesize($fichier));
fclose($fp);
$fichierattache = chunk_split(base64_encode($fichierattache));
 
$msg .= "--$delimiteur\r\n";
$msg .= "Content-Type: application/octet-stream; name=\"$fichier\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-ID: <image2>\r\n";
$msg .= "\r\n";
$msg .= $fichierattache . "\r\n";
$msg .= "\r\n\r\n";
 
$msg .= "--$delimiteur\r\n";
 
$destinataire = "testemail@toutestfacile.com";
$expediteur   = "moi@monsite.com";
$reponse      = $expediteur;
echo "Ce script envoie un mail au format HTML avec 2 images à $destinataire";
mail($destinataire,
     "Email HTML avec 2 images",
     $msg,
     "Reply-to: $reponse\r\nFrom: $expediteur\r\n".$entete);
?>
</body>
</html> 

SWIFT mailer

Pour facilement et rapidement envoyer des mails, il y a la librairie SWIFT:

http://www.swiftmailer.org/

Voir la page de ce wiki sur swift

PDF

Une librairie existe pour générer des fichiers PDF:

http://www.fpdf.org/

Pour convertir un fichier PDF en HTML il existe pdftohtml

apt-get install poppler-utils

XML Parser

La librairie XML_Parser de Pear est très simple à utiliser

pear install XML_Parser

http://www.sitepoint.com/article/xml-php-pear-xml_serializer/

XML-RPC

Un petit tutorial pour commencer:

http://www.journaldunet.com/developpeur/tutoriel/php/020109php_xmlrpc.shtml

Une librairie PHP pour envoyer des requêtes facilement:

https://sourceforge.net/projects/phpxmlrpc/

Gandi XML-RPC

Pour Gandi, si on a le message d'erreur:

could not login because: CURL error: SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed 

Il faut ajouter après la création du proxy ce code pour désactiver la vérification du certificat SSL

$proxy->setSSLVerifyPeer(false);

Il faut que l'utilisateur soit propriétaire du nom de domaine pour pouvoir accéder aux enregistrements des hosts

<?php

$domain = new xmlrpcval("citizen-web.com");
$msg  = new xmlrpcmsg("host_list", array($session, $domain));
$reply  = $proxy->send($msg);
if ($reply->faultCode()) {
    printf("could not retrieve the list of hosts because: %s\n", $reply->faultString());
}
else {
    $val  = php_xmlrpc_decode($reply->value());
    print_r($val);
}

?>

HTML2PS

Sur Linux, vous pouvez trouver un script perl qui tente de faire une capture de page HTML vers PS. Mais il ne traite pas le charset UTF-8, alors que c'est la norme pour les charset maintenant. Et le rendu n'est pas terrible.

De manière surprenante, il y a une version PHP qui propse une interface web et dont le résultat est très satisfaisant!

http://sourceforge.net/projects/html2ps

A essayer…