par
cire390 » 22 juil. 2009, 18:08
Bonjour,
Ci-joint une fonction (longue desolé) etant censé faire de l'export vers un mail. Cette fonction a été réalisée y a environ 1 an et aujourd'hui on doit la retravailler car notre smtp nécésite une authentification.
On a adapté pour faire un envoi avec pear. Je précise qu'un fonction mail (simple) avec pear fonctionne trés bien.
Le problème est que tous ce déroule bien mais aucun message nulle part.
Ce code etant assez complexe je voulais savoir si au moins la syntaxe etait bonne.
Merci d'avance, (désolé je n'arrive pas a "colorier" mon code)
Code : Tout sélectionner
[php]<?php
class ExportMail {
protected $sender,
$recipient,
$subject;
protected $texts;
protected $binary;
public function __construct(&$config, $Importfile, $Importfile2)
{
$this->texts = $this->binary = array();
$needed_keys = array(
'sender',
'recipient',
'subject'
);
foreach ($needed_keys as $key) {
if (!isset($config[$key])) {
Export::log("Mail :: Paramètre manquant ($key).");
die(1);
}
else $this->$key = $config[$key];
}
}
public function setText($name, $text)
{
$this->texts[$name] =& $text;
}
public function setBinary($name, $bin)
{
$this->binary[$name] =& $bin;
}
public function export()
{
$attach = '';
$content = '';
$boundary = md5(uniqid('BluePen-', FALSE));
$date = date('r');
$headers =<<<TXT
From: <{$this->sender}>
Date: $date
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="$boundary"
TXT;
foreach ($this->texts as $name => $value) {
$content .= "\n" . $name . '=' . $value;
}
foreach ($this->binary as $name => $value) {
if ($value === NULL) continue;
$name = rawurlencode($name);
$attach .= <<<MAIL
--$boundary
Content-Type: application/octet-stream;
name="$name.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$name.png"
MAIL;
$attach .= "\n\n".chunk_split(base64_encode($value), 70, "\n");
}
mb_internal_encoding('UTF-8');
$subject = mb_encode_mimeheader($this->subject, 'UTF-8', 'B', "\n");
$message =<<<TXT
Si vous voyez ce texte au lieu du contenu attendu, veuillez utiliser
un client de messagerie capable d'afficher correctement les emails
multipart (Outlook, Thunderbird, etc.).
--$boundary
Content-Type: text/plain; charset="windows-1252"; format="flowed"
Content-Transfer-Encoding: 8bit
$content
$attach
--$boundary--
TXT;
// FIXME: Some MTAs require lines to end with
// "\r\n" while others support only "\n".
// For now, we stick to the "\r\n" style.
$message = str_replace("\r", "", $message);
$message = str_replace("\n", "\r\n", $message);
// Under Windows, PHP speaks directly to the SMTP server,
// while on Unix, the mail goes through sendmail.
// Therefore, we must make sure no lone dot appears at
// the beginning of a line, with respect to RFC 822.
if (strpos(strtolower(PHP_OS), 'win') !== FALSE)
$message = str_replace("\n.", "\n..", $message);
require_once "Mail.php";
$host = "smtp.hebergeur.com";
$username = "monusername";
$password = "mdp";
/*$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject); */
$smtp = Mail::factory('smtp',
array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($this->recipient, $subject, $message, $headers);
if (PEAR::isError($mail)) {
Export::log("Mail :: Impossible d'envoyer l'email.");
}
else { Export::log("Mail :: Message envoyé");
}
// mail($this->recipient, $subject, $message, $headers);
}
}
?>[/php]
[/php]
Bonjour,
Ci-joint une fonction (longue desolé) etant censé faire de l'export vers un mail. Cette fonction a été réalisée y a environ 1 an et aujourd'hui on doit la retravailler car notre smtp nécésite une authentification.
On a adapté pour faire un envoi avec pear. Je précise qu'un fonction mail (simple) avec pear fonctionne trés bien.
Le problème est que tous ce déroule bien mais aucun message nulle part.
Ce code etant assez complexe je voulais savoir si au moins la syntaxe etait bonne.
Merci d'avance, (désolé je n'arrive pas a "colorier" mon code)
[code]
[php]<?php
class ExportMail {
protected $sender,
$recipient,
$subject;
protected $texts;
protected $binary;
public function __construct(&$config, $Importfile, $Importfile2)
{
$this->texts = $this->binary = array();
$needed_keys = array(
'sender',
'recipient',
'subject'
);
foreach ($needed_keys as $key) {
if (!isset($config[$key])) {
Export::log("Mail :: Paramètre manquant ($key).");
die(1);
}
else $this->$key = $config[$key];
}
}
public function setText($name, $text)
{
$this->texts[$name] =& $text;
}
public function setBinary($name, $bin)
{
$this->binary[$name] =& $bin;
}
public function export()
{
$attach = '';
$content = '';
$boundary = md5(uniqid('BluePen-', FALSE));
$date = date('r');
$headers =<<<TXT
From: <{$this->sender}>
Date: $date
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="$boundary"
TXT;
foreach ($this->texts as $name => $value) {
$content .= "\n" . $name . '=' . $value;
}
foreach ($this->binary as $name => $value) {
if ($value === NULL) continue;
$name = rawurlencode($name);
$attach .= <<<MAIL
--$boundary
Content-Type: application/octet-stream;
name="$name.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="$name.png"
MAIL;
$attach .= "\n\n".chunk_split(base64_encode($value), 70, "\n");
}
mb_internal_encoding('UTF-8');
$subject = mb_encode_mimeheader($this->subject, 'UTF-8', 'B', "\n");
$message =<<<TXT
Si vous voyez ce texte au lieu du contenu attendu, veuillez utiliser
un client de messagerie capable d'afficher correctement les emails
multipart (Outlook, Thunderbird, etc.).
--$boundary
Content-Type: text/plain; charset="windows-1252"; format="flowed"
Content-Transfer-Encoding: 8bit
$content
$attach
--$boundary--
TXT;
// FIXME: Some MTAs require lines to end with
// "\r\n" while others support only "\n".
// For now, we stick to the "\r\n" style.
$message = str_replace("\r", "", $message);
$message = str_replace("\n", "\r\n", $message);
// Under Windows, PHP speaks directly to the SMTP server,
// while on Unix, the mail goes through sendmail.
// Therefore, we must make sure no lone dot appears at
// the beginning of a line, with respect to RFC 822.
if (strpos(strtolower(PHP_OS), 'win') !== FALSE)
$message = str_replace("\n.", "\n..", $message);
require_once "Mail.php";
$host = "smtp.hebergeur.com";
$username = "monusername";
$password = "mdp";
/*$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject); */
$smtp = Mail::factory('smtp',
array (
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($this->recipient, $subject, $message, $headers);
if (PEAR::isError($mail)) {
Export::log("Mail :: Impossible d'envoyer l'email.");
}
else { Export::log("Mail :: Message envoyé");
}
// mail($this->recipient, $subject, $message, $headers);
}
}
?>[/php]
[/code][/php]