2011-02-04 7 views
10

Il seguente codice invia un'e-mail correttamente ma per il corpo. Devo mostrare html nel corpo del messaggio e non riesco a farcela. Gli esempi nel web non inviare l'email :(Php mail: come inviare html?

Come posso risolvere il mio codice per inviare l'e-mail con il codice HTML nel corpo?

Grazie una tonnellata!

<?php 

$to = '[email protected]'; 

$subject = 'I need to show html'; 

$from ='[email protected]'; 

$body = '<p style=color:red;>This text should be red</p>'; 

ini_set("sendmail_from", $from); 

$headers = "From: " . $from . "\r\nReply-To: " . $from . ""; 
    $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) { 

    echo("<p>Sent</p>"); 
} else { 
    echo("<p>Error...</p>"); 
} 

?> 
+0

+1 per domanda che ho avuto modo simile domanda, ma questa discussione risposte IT (provenienti da varie angolazioni) – Sam

risposta

16

uso questa intestazione per il mai l:

$header = "MIME-Version: 1.0\r\n"; 
$header .= "Content-type: text/html; charset: utf8\r\n"; 

e per il contenuto/corpo:

<html> 
    <head> 
     <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
... ... ... 

è importante utilizzare comandi CSS in linea e raccomandato per utilizzare le tabelle per l'interfaccia.

...

nella vostra posta-Body voi che mettere HTML code with head and body

+0

Grazie helle, ma errore nelle intestazioni e nessun codice HTML nel corpo :( – lleoun

+0

cambia la tua linea anche a questo e chiudi tutti i tag che hai aperto e usa il tag ''. '$ intestazioni. =" Da: ". $ from." \ r \ nReply-To: ". $ from." ";' – helle

+0

Spiacente, non lo capisco: $ headers = "Versione MIME: 1.0 \ r \ n"; $ intestazioni . = "Tipo di contenuto: text/html; charset: utf8 \ r \ n"; invia l'e-mail con il testo in lettura, ciò che è grande. Ma nel campo from viene mostrato www-data .. come posso aggiustarlo ? Una volta fatto, sarà finito. Grazie mille – lleoun

1

I pensano che questa funziona bene!

Source

<?php 
//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test HTML email'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 
<? 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
?> 
+0

trovato troppo ma non l'invio nel mio caso, perché? – lleoun

+0

La configurazione della posta è corretta? Sei su un server IIS? – chrisjlee

+0

Questo si basava sul comando mail. Questo comando è presente nella maggior parte dei * sistemi NIX. –

-3

Risposta semplice: non farlo. Le email HTML sono malvagie e fastidiose. Almeno se non è inclusa la versione in chiaro del PROPER. Corretto = stesse informazioni come nella versione HTML, non solo un commento stupido su come ottenere un altro client di posta elettronica o un collegamento alla versione html se è disponibile sul web.

Se si ha realmente bisogno: http://pear.php.net/package/Mail_Mime

+3

Possono essere fastidiosi se vengono usati in modo improprio. Tuttavia, se usati con attenzione, sono abbastanza buoni. A mio parere, la migliore pratica è quella di inviare corpi semplici/di testo e semplici/html, in modo che l'utente possa scegliere quale vuole vedere. – binaryLV

2

vi consiglio invece di fare in giro con fare da soli si utilizza uno dei tanti corsi gratuiti disponibili in tutto il web per farlo.

mi raccomando: PHPMailer

3

Hai guardato le intestazioni del posta in arrivo? Si dice

Reply-To: [email protected]: text/html

sufficiente aggiungere un altro \r\n qui:

Reply-To: " . $from . "\r\n";