tags:

views:

48

answers:

1

Hello,

The code below works great. It allows a user to send a recommendation for my site to a list of friends via email.

For each person that gets the email below, I would like to get an email with that person's email, and the name of the person that sent them the message. If my email address was [email protected], what code could I use to do this?

Thanks in advance,

John

$msg = "<html><body>Hello, your friend ".htmlspecialchars($_POST['sendername'])." recommends that you use <a href='http://www.site.com/'&gt;Site.com&lt;/a&gt;.&lt;a href='http://www.site.com/'&gt;Site.com&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;img src='http://site.com/images/blacklogo.PNG'&gt;&lt;/body&gt;&lt;/html&gt;";
$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);
}
+4  A: 
$msg = "<html><body>Hello, your friend ".htmlspecialchars($_POST['sendername'])." recommends that you use <a href='http://www.site.com/'&gt;Site.com&lt;/a&gt;.&lt;a href='http://www.site.com/'&gt;Site.com&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;img src='http://site.com/images/blacklogo.PNG'&gt;&lt;/body&gt;&lt;/html&gt;";
$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);
}

Rather than messing about with raw headers, consider using one of the many great APIs available, like Swiftmailer, PHPMailer, or Zend_Mail to name just three. Zend_Mail example:

$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();
karim79
You need to add \r\n to the end of the CC and BCC lines
Brandon Bodnár
@bodnarbm - Thanks, added.
karim79
@karim79, I tried your original suggestion, but it's not sending the CC to my email address.
John
@John - I've edited. I think the MIME needs to be the first header, and it's a good idea to have a Content-Type header too.
karim79