[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / ecrire / inc / envoyer_mail.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) return;
14
15 include_spip('inc/charsets');
16 include_spip('inc/texte');
17
18 // http://doc.spip.org/@nettoyer_titre_email
19 function nettoyer_titre_email($titre) {
20 return str_replace("\n", ' ', textebrut(corriger_typo($titre)));
21 }
22
23 // http://doc.spip.org/@nettoyer_caracteres_mail
24 function nettoyer_caracteres_mail($t) {
25
26 $t = filtrer_entites($t);
27
28 if ($GLOBALS['meta']['charset'] <> 'utf-8') {
29 $t = str_replace(
30 array("&#8217;","&#8220;","&#8221;"),
31 array("'", '"', '"'),
32 $t);
33 }
34
35 $t = str_replace(
36 array("&mdash;", "&endash;"),
37 array("--","-" ),
38 $t);
39
40 return $t;
41 }
42
43 /**
44 * Envoi d'un mail
45 * http://doc.spip.org/@inc_envoyer_mail_dist
46 *
47 * @param string $destinataire
48 * @param string $sujet
49 * @param string|array $corps
50 * au format string, c'est un corps d'email au format texte, comme supporte nativement par le core
51 * au format array, c'est un corps etendu qui peut contenir
52 * string texte : le corps d'email au format texte
53 * string from : email de l'envoyeur (prioritaire sur argument $from de premier niveau, deprecie)
54 * array headers : tableau d'en-tetes personalises, une entree par ligne d'en-tete
55 * --- Support partiel par une fonction mail_embarquer_pieces_jointes a fournir, ---
56 * --- chargee de convertir en texte encodee les pieces jointes ---
57 * array pieces_jointes : listes de pieces a embarquer dans l'email, chacune au format array :
58 * string chemin : chemin file system pour trouver le fichier a embarquer
59 * string nom : nom du document tel qu'apparaissant dans l'email
60 * string encodage : encodage a utiliser, parmi 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
61 * string mime : mime type du document
62 * --- Non implemente ici ---
63 * string html : le corps d'email au format html
64 * string nom_envoyeur : un nom d'envoyeur pour completer l'email from
65 * string cc : destinataires en copie conforme
66 * string bcc : destinataires en copie conforme cachee
67 * string adresse_erreur : addresse de retour en cas d'erreur d'envoi
68 * @param string $from (deprecie, utiliser l'entree from de $corps)
69 * @param string $headers (deprecie, utiliser l'entree headers de $corps)
70 * @return bool
71 */
72 function inc_envoyer_mail_dist($destinataire, $sujet, $corps, $from = "", $headers = "") {
73
74 if (!email_valide($destinataire)) return false;
75 if ($destinataire == _T('info_mail_fournisseur')) return false; // tres fort
76
77 // Fournir si possible un Message-Id: conforme au RFC1036,
78 // sinon SpamAssassin denoncera un MSGID_FROM_MTA_HEADER
79
80 $email_envoi = $GLOBALS['meta']["email_envoi"];
81 if (!email_valide($email_envoi)) {
82 spip_log("Meta email_envoi invalide. Le mail sera probablement vu comme spam.");
83 $email_envoi = $destinataire;
84 }
85
86 if (is_array($corps)){
87 $texte = $corps['texte'];
88 $from = (isset($corps['from'])?$corps['from']:$from);
89 $headers = (isset($corps['headers'])?$corps['headers']:$headers);
90 if (is_array($headers))
91 $headers = implode("\n",$headers);
92 $parts = "";
93 if ($corps['pieces_jointes'] AND function_exists('mail_embarquer_pieces_jointes'))
94 $parts = mail_embarquer_pieces_jointes($corps['pieces_jointes']);
95 } else
96 $texte = $corps;
97
98 if (!$from) $from = $email_envoi;
99
100 // ceci est la RegExp NO_REAL_NAME faisant hurler SpamAssassin
101 if (preg_match('/^["\s]*\<?\S+\@\S+\>?\s*$/', $from))
102 $from .= ' (' . str_replace(')','', translitteration(str_replace('@', ' at ', $from))) . ')';
103
104 // nettoyer les &eacute; &#8217, &emdash; etc...
105 // les 'cliquer ici' etc sont a eviter; voir:
106 // http://mta.org.ua/spamassassin-2.55/stuff/wiki.CustomRulesets/20050914/rules/french_rules.cf
107 $texte = nettoyer_caracteres_mail($texte);
108 $sujet = nettoyer_caracteres_mail($sujet);
109
110 // encoder le sujet si possible selon la RFC
111 if (init_mb_string()) {
112 # un bug de mb_string casse mb_encode_mimeheader si l'encoding interne
113 # est UTF-8 et le charset iso-8859-1 (constate php5-mac ; php4.3-debian)
114 $charset = $GLOBALS['meta']['charset'];
115 mb_internal_encoding($charset);
116 $sujet = mb_encode_mimeheader($sujet, $charset, 'Q', "\n");
117 mb_internal_encoding('utf-8');
118 }
119
120 if (function_exists('wordwrap') && (preg_match(',multipart/mixed,',$headers) == 0))
121 $texte = wordwrap($texte);
122
123 list($headers, $texte) = mail_normaliser_headers($headers, $from, $destinataire, $texte, $parts);
124
125 if (_OS_SERVEUR == 'windows') {
126 $texte = preg_replace ("@\r*\n@","\r\n", $texte);
127 $headers = preg_replace ("@\r*\n@","\r\n", $headers);
128 $sujet = preg_replace ("@\r*\n@","\r\n", $sujet);
129 }
130
131 spip_log("mail $destinataire\n$sujet\n$headers",'mails');
132 // mode TEST : forcer l'email
133 if (defined('_TEST_EMAIL_DEST')) {
134 if (!_TEST_EMAIL_DEST)
135 return false;
136 else {
137 $texte = "Dest : $destinataire\r\n" . $texte;
138 $destinataire = _TEST_EMAIL_DEST;
139 }
140 }
141
142 return @mail($destinataire, $sujet, $texte, $headers);
143 }
144
145 function mail_normaliser_headers($headers, $from, $to, $texte, $parts="")
146 {
147 $charset = $GLOBALS['meta']['charset'];
148
149 // Ajouter le Content-Type et consort s'il n'y est pas deja
150 if (strpos($headers, "Content-Type: ") === false)
151 $type =
152 "Content-Type: text/plain;charset=\"$charset\";\n".
153 "Content-Transfer-Encoding: 8bit\n";
154 else $type = '';
155
156 // calculer un identifiant unique
157 preg_match('/@\S+/', $from, $domain);
158 $uniq = rand() . '_' . md5($to . $texte) . $domain[0];
159
160 // Si multi-part, s'en servir comme borne ...
161 if ($parts) {
162 $texte = "--$uniq\n$type\n" . $texte ."\n";
163 foreach ($parts as $part) {
164 $n = strlen($part[1]) . ($part[0] ? "\n" : '');
165 $e = join("\n", $part[0]);
166 $texte .= "\n--$uniq\nContent-Length: $n$e\n\n" . $part[1];
167 }
168 $texte .= "\n\n--$uniq--\n";
169 // Si boundary n'est pas entre guillemets,
170 // elle est comprise mais le charset est ignoree !
171 $type = "Content-Type: multipart/mixed; boundary=\"$uniq\"\n";
172 }
173
174 // .. et s'en servir pour plaire a SpamAssassin
175
176 $mid = 'Message-Id: <' . $uniq . ">";
177
178 // indispensable pour les sites qui collent d'office From: serveur-http
179 // sauf si deja mis par l'envoyeur
180 $rep = (strpos($headers,"Reply-To:")!==FALSE) ? '' : "Reply-To: $from\n";
181
182 // Nettoyer les en-tetes envoyees
183 // Ajouter le \n final
184 if (strlen($headers = trim($headers))) $headers .= "\n";
185
186 // Et mentionner l'indeboulonable nomenclature ratee
187
188 $headers .= "From: $from\n$type$rep$mid\nMIME-Version: 1.0\n";
189
190 return array($headers, $texte);
191 }
192 ?>