2009-09-16 4 views
1

Le code ci-dessous fonctionne très bien. Il permet à un utilisateur d'envoyer une recommandation pour mon site à une liste d'amis par e-mail.Obtention d'une copie CC des messages d'invite utilisateur

Pour chaque personne qui reçoit l'e-mail ci-dessous, je souhaite recevoir un e-mail avec son adresse e-mail et le nom de la personne qui lui a envoyé le message. Si mon adresse e-mail était [email protected], quel code pourrais-je utiliser pour le faire?

Merci à l'avance,

John

$msg = "<html><body>Hello, your friend ".htmlspecialchars($_POST['sendername'])." recommends that you use <a href='http://www.site.com/'>Site.com</a>.<a href='http://www.site.com/'>Site.com</a><br><br><img src='http://site.com/images/blacklogo.PNG'></body></html>"; 
$subject = "Try out Site.com"; 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= 'From: ' . $_POST['sendername'] . "\r\n"; 
foreach($_POST['email'] as $email){ 
mail($email, $subject,$msg,$headers); 
} 
+0

Vous êtes ouvert aux attaques par injection d'en-tête sur la ligne 5. Vous devez échapper votre entrée. – ryeguy

+0

$ headers = mysql_real_escape_string ($ en-têtes); ? – John

+0

'str_ireplace (array (" \ r "," \ n ","% 0a ","% 0d "," Content-Type: "," bcc: "," à: "," cc: ")," ", $ _POST ['sendername']);' – jimyi

Répondre

4
$msg = "<html><body>Hello, your friend ".htmlspecialchars($_POST['sendername'])." recommends that you use <a href='http://www.site.com/'>Site.com</a>.<a href='http://www.site.com/'>Site.com</a><br><br><img src='http://site.com/images/blacklogo.PNG'></body></html>"; 
$subject = "Try out Site.com"; 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= "CC: [email protected]\r\n"; 
$headers .= "BCC: [email protected]\r\n"; 
$headers .= 'From: ' . $_POST['sendername'] . "\r\n"; 
foreach($_POST['email'] as $email){ 
    mail($email, $subject,$msg,$headers); 
} 

Plutôt que de vous embêter avec des en-têtes brutes, envisagez d'utiliser l'un des nombreux API disponibles, comme Swiftmailer, PHPMailer ou Zend_Mail au nom seulement trois. Zend_Mail Exemple:

$mail = new Zend_Mail(); 
$mail->setBodyHtml('<p>hello</p>'); 
$mail->setFrom('[email protected]', '[email protected]'); 
$mail->setSubject('Test Subject'); 
$mail->addTo('[email protected]', 'Test'); 
$mail->addCc('[email protected]', 'Another Test'); 
$mail->addBcc('[email protected]', 'Another Test'); 
$mail->send(); 
+0

Vous devez ajouter \ r \ n à la fin des lignes CC et BCC –

+0

@bodnarbm - Merci, ajouté. – karim79

+0

@ Karim79, j'ai essayé votre suggestion originale, mais elle n'envoie pas le CC à mon adresse e-mail. – John

Questions connexes