2012-01-09 10 views
5

Attualmente sto utilizzando uno script che utilizza file_get_contents per ottenere il contenuto di un file php e quindi lo invia all'interno di un messaggio di posta elettronica a un elenco di clienti. Vorrei modificare lo script per consentire il fallback di testo normale per ridurre il rischio di essere contrassegnati come spam.Invio di newsletter HTML con fallback testo semplice

Questo è lo script che ho in questo momento:

function sendit($to,$subject,$body) 
{ 
$headers = "To: <{$to}>\n". 
        "From: Test Newsletter Admin <[email protected]>\n". 
        "Reply-To: Test Newsletter Admin <[email protected]>\n". 
        "MIME-Version: 1.0\n". 
        "Content-Type: text/html; charset=ISO-8859-1\n"; 

    $mail_sent = @mail($to, $subject, $body, $headers); 
    return $mail_sent; 
} 
$content = file_get_contents('attach/newsletter.php'); 
//require_once('../../func.php'); 
set_time_limit(0); 
date_default_timezone_set('Europe/London'); 

$log = fopen('send'.date('dmY',time()).'.log','wb'); 

//$array = file('NonCustClean.txt'); 
$array = file('devaddresses.txt'); 
// Delay In Seconds between emails (must be INT) 
$delay = 10; 
$count = count($array); 

$end = time()+ ($delay*$count); 

echo "Start Time: ".date('d/m/Y H:i:s',time()).'<br />'; 
echo "Estimated End Time: ".date('d/m/Y H:i:s',$end).'<br />'; 
echo "(".dateDiff(time(),$end).")<br />"; 

foreach ($array as $email) 
{ 

$status = (sendit(trim($email), 'Test Newsletter',$content))?'Sent':'failed'; 
fwrite($log,date('[d/m/Y H:i:s]',time()).' Email '.$status.' to '.trim($email)."\n"); 
echo date('[d/m/Y H:i:s]',time()).' Email '.$status.' to '.trim($email)."</br>"; 
flush(); 
    sleep(10); 
} 

newsletter.php solo contiene il codice di base HTML/CSS.

Qualcuno potrebbe consigliare come posso modificare questo script per accompagnare un fallback di testo normale?

Grazie per qualsiasi aiuto.

+0

Avete pensato di usare una libreria come [Swiftmailer] (http://swiftmailer.org/)? Questa [risposta] (http://stackoverflow.com/a/29345590/987864) descrive come è facile inviare messaggi di testo HTML e in chiaro. – Daan

risposta

5

quello che stai cercando è conosciuto come un e-mail multipart. Il suo corpo contiene sia HTML che testo, delimitati da un cosiddetto "limite". Il client di posta elettronica si determinerà quindi se mostrerà la versione HTML o di testo della posta, in base alle sue capacità e preferenze dell'utente.

Un esempio su come si può impostare questa funzione (source):

$notice_text = "This is a multi-part message in MIME format."; 
$plain_text = "This is a plain text email.\r\nIt is very cool."; 
$html_text = "<html><body>This is an <b style='color:purple'>HTML</b>" . 
      "text email.\r\nIt is very cool.</body></html>"; 

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

$to = "Me <[email protected]>"; 
$bcc = "You <[email protected]>, Them <[email protected]>"; 
$from = "Me.com <[email protected]>"; 
$subject = "My Email"; 

$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_text 

--$mime_boundary--"; 

if (@mail($to, $subject, $body, 
    "From: " . $from . "\n" . 
    "bcc: " . $bcc . "\n" . 
    "MIME-Version: 1.0\n" . 
    "Content-Type: multipart/alternative;\n" . 
    "  boundary=" . $mime_boundary_header)) 
    echo "Email sent successfully."; 
else 
    echo "Email NOT sent successfully!"; 
+0

Ho provato questo e l'e-mail è finito nella cartella spam. http://www.mail-tester.com/ dice "Il messaggio è dal 10% al 20% di offuscamento HTML". Inoltre dice che non mi è permesso spedire mail dall'indirizzo email specificato. – erdomester

0

Si dovrebbe modificare

Content-Type: text/html; charset=ISO-8859-1\n 

Per maggiori informazioni, si prega di controllare questo site

Problemi correlati