2010-08-07 8 views
1

Avait finalement obtenu tous les bugs hors de cela et maintenant ils ont dit "Oh, nous aurons besoin d'ajouter des pièces jointes ..." Donc, cette envoie un courrier HTML avec une version en texte clair et faisait juste gonfler. Maintenant que les pièces jointes arrivent, les clients de messagerie affichent la version en texte brut inline et la version html sous la forme d'une autre pièce jointe, puis un fichier de 93 octets apparemment vide avec un nom comme ATT00248.txt. Est-ce que n'importe qui peut me frapper sur la tête par derrière ou me dire où je me trompe? Je veux le HTML en ligne où disponible dans l'interface utilisateur de messagerie, la version en texte brut où HTML n'est pas disponible, et la pièce jointe unique en pièce jointe.PHP mail() - HTML apparaît comme une pièce jointe une fois qu'une pièce jointe est ajoutée

Une aide?

<?php 
$template = $_SERVER['DOCUMENT_ROOT'] . '/leads/templates/'.$_SESSION['templateFile']; 
ob_start(); 
include($template); 
$html = ob_get_contents(); 
ob_end_clean(); 

if (strlen($html) == 0) { 
    echo "The template at $template did not load."; 
    exit; 
} 

$email = $_SESSION['user']->email; 
$name = $_SESSION['user']->first_name . ' ' . $_SESSION['user']->last_name; 
$from = "$name <$email>"; 
$subject = unslash($_SESSION['subject']); 

$TextMessage = strip_tags(unslash($_SESSION['message'])); 

$notice_text = "This is a multi-part message in MIME format."; 
$plain_text = str_replace('&nbsp;',' ', $TextMessage); 

if ($_SESSION['attachment']) { 
    $fileatt = 'files/' . $_SESSION['attachment']; 
    $file = fopen($fileatt,'rb'); 
    $data = fread($file,filesize($fileatt)); 
    fclose($file); 
    $data = chunk_split(base64_encode($data)); 
    $mailtype = 'mixed'; 

    $fileatt_type = "application/octet-stream"; 
    $fileatt_name = $_SESSION['attachment']; 
} else { 
    $mailtype = 'alternative'; 
} 

$semi_rand = md5(time()); 
$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand"; 
$mime_boundary_header = chr(34) . $mime_boundary . chr(34); 

$body = "$notice_text 

--$mime_boundary 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: 7bit 

$plain_text 

--$mime_boundary 
Content-Type: text/html; charset=us-ascii 
Content-Transfer-Encoding: 7bit 

$html 

--$mime_boundary 
"; 

$body .= "Content-Type: {$fileatt_type};\n" . 
" name=\"{$fileatt_name}\"\n" . 
"Content-Disposition: attachment;\n" . 
"Content-Transfer-Encoding: base64\n\n" . 
$data . "\n\n" . 
"--$mime_boundary\n"; 



// #1 // 
if ($to = $_SESSION['recipients'][0]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 

    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 


// #2 // 
if ($to = $_SESSION['recipients'][1]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// #3 // 
if ($to = $_SESSION['recipients'][2]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// #4 // 
if ($to = $_SESSION['recipients'][3]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// #5 // 
if ($to = $_SESSION['recipients'][4]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 

// CC self? // 
if ($_SESSION['cc_me']) { 
    mail($from, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 
    echo "Email sent to " . htmlentities($from) . ".<br />"; 
} 

if ($fileatt) { 
    unlink($fileatt); 
} 

echo "<a href='email_start.php'>Click here</a> to send another email."; 
list($_SESSION['email'], $_SESSION['subject'], $_SESSION['bullets'], $_SESSION['message'], 
    $_SESSION['templateFile'], $_SESSION['template'], $_SESSION['cc_me'], $_SESSION['recipients']) = ''; 
?> 
+1

Avez-vous envisagé d'utiliser une classe de publipostage prête à l'emploi comme Swiftmailer? http://swiftmailer.org/ –

+0

Pekka- merci! Je deviens tellement en train de rouler my6 que je me fais du stress parfois! swiftmailer a rendu le travail simple et sans stress. Je vous remercie!!! Vous devriez ajouter ceci comme réponse et je le sélectionnerai! – jerrygarciuh

Répondre

0

Je posterais ceci comme un commentaire, mais c'est trop long.

// #1 // 
if ($to = $_SESSION['recipients'][0]) { 
    mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header); 

    echo "Email sent to " . htmlentities($to) . ".<br />"; 
} 


// #2 ... #3 ... #4 ... #5 

finira par exécuter tous les blocs, car ($to = $_SESSION['recipients'][0]) sera toujours vrai. Il affichera également le "Email envoyé à ..." même si mail() échoue.

Qu'est-ce que vous voulez est:

if (in_array($to, $_SESSION['recipients'])) { 
    if (mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header)) { 
     echo "Email sent to " . htmlentities($to) . ".<br />"; 
    } 
} 

Ou, si vous voulez vraiment courrier avec tous, ou

foreach ($_SESSION['recipients'] as $to) { 
    if (mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/$mailtype;\n" . 
    "  boundary=" . $mime_boundary_header)) { 
     echo "Email sent to " . htmlentities($to) . ".<br />"; 
    } 
} 
+0

Merci pour la réponse. J'aurais dû poster un lien vers ceci: http://stackoverflow.com/questions/3419858/php-mail-sends-empty-content-when-looping-over-array-of-addresses Pour expliquer pourquoi J'ai itéré comme je l'ai fait. – jerrygarciuh

+0

Les en-têtes doivent être séparés par "\ r \ n". Cela fait partie de la norme SMTP et PAS FACULTATIF. rfc 5321: 2.3.8. Lignes – ThatGuy