469,328 Members | 1,261 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

How do I add BCC: to this PHP mailer please?

I found this basic PHP mailer with file attachment capability. It
works just fine, but I want to add a CC: and BCC: to its output. I
added the $cc & $bcc variables below, but unsure how to implement the
rest. Can someone please help? Thank you - LM

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$cc = $_POST['cc'];
$bcc = $_POST['bcc'];

// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>

Aug 24 '05 #1
12 20893
Larry wrote:
I found this basic PHP mailer with file attachment capability. It
works just fine, but I want to add a CC: and BCC: to its output. I
added the $cc & $bcc variables below, but unsure how to implement the
rest. Can someone please help? Thank you - LM

<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$cc = $_POST['cc'];
$bcc = $_POST['bcc'];

// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>


You have to add something like this to the header:

BCC: address

Have a look at this:
http://www.faqs.org/rfcs/rfc822.html

Stefan
Aug 24 '05 #2
Hum, I messed around with adding CC:/BCC: to the $header and $from
variable, but it never seems to catch. Has anyone been successful out
there in adding it to code similar to mine?

Aug 24 '05 #3
Here's a little code snippet I generally use for quick emails:

$domain = '';
$headers = "MIME-Version: 1.0\r \n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r \n";
$headers .= "From: \"$sender\" <$email>\r \n";
$headers .= "CC: \"name\" <email>\r \n";
$headers .= "BCC: \"name\" <email>\r \n";
$headers .= "X-Mailer: $domain";

$to = "";
$subject = "";
$message .= "\n\n[This email was sent via $domain]";

mail ($to, $subject, $message, $headers);

Aug 24 '05 #4
You may also want to try the phpMailer class. It makes it all pretty
simple: http://phpmailer.sourceforge.net/

Aug 24 '05 #5
I'm not sure what you played arround with, but maybe you could try
changing:

$headers = "From: $from";

to:

$headers = "From: $from\nCC: $cc\nBCC: $bcc";

Aug 24 '05 #6
Stefan Rybacki wrote:
Have a look at this:
http://www.faqs.org/rfcs/rfc822.html


Yes, but that one's been around a wheen of years now, over 23
to be precise!! It would've celebrated its birthday not last
Saturday but the Saturday before had its successor not
obsoleted it back in 2001. The docs for mail() refer to the
latest one. Point your browser here:

http://www.ietf.org/rfc/rfc2822

--
Jock
Aug 24 '05 #7
John Dunlop wrote:
Stefan Rybacki wrote:

Have a look at this:
http://www.faqs.org/rfcs/rfc822.html

Yes, but that one's been around a wheen of years now, over 23
to be precise!! It would've celebrated its birthday not last
Saturday but the Saturday before had its successor not
obsoleted it back in 2001. The docs for mail() refer to the
latest one. Point your browser here:

http://www.ietf.org/rfc/rfc2822


Yes maybe but the BCC part didn't change. And I didn't know that
standards are abled to be outdated. What if the mail goes to an email client developed
before 2001? I didn't read the new rfc but I guess its more about extensions to the standard.

Never mind. Bookmarked.

Stefan
Aug 24 '05 #8
Stefan Rybacki wrote:
And I didn't know that standards are abled to be outdated.
Yes, see RFC2026 secs. 6.3 and 6.4:

http://www.ietf.org/rfc/rfc2026
What if the mail goes to an email client developed before 2001?
Fingers crossed, it gets understood!!
I didn't read the new rfc but I guess its more about extensions
to the standard.


Appendix B lists the differences between 822 and 2822, so if
you've already read 822, you can save yourself the trouble of
picking through 2822. The last item the appendix lists is
'Bcc more clearly specified.'.

--
Jock
Aug 24 '05 #9
* Nerdlance.com:
$headers = "MIME-Version: 1.0\r \n";

^
Please do not put a whitespace between the CLRF. It violates RFC2822.

Followup set to comp.lang.php, alt.php is not available here.
--
"The sky above the port was the color of television,
tuned to a dead channel." o <http://knurd.de/>
-- William Gibson, Neuromancer, o -- A Geek's View
Chiba City Blues ooo
Aug 24 '05 #10
> Please do not put a whitespace between the CLRF. It violates RFC2822.

I wondered about that. I this is the only case I have ever done so. It
was an example on php.net that I did a quick copy/paste.

Aug 24 '05 #11
* Nerdlance.com:
I wondered about that. I this is the only case I have ever done so. It
was an example on php.net that I did a quick copy/paste.


RFC2822 tells you to seperate (header) lines by CRLF, not by any other
combination of characters. It's mostly a thing of interoperability and
avoiding this is, in the luckiest way, considered harmful.

--
"The sky above the port was the color of television,
tuned to a dead channel." o <http://knurd.de/>
-- William Gibson, Neuromancer, o -- A Geek's View
Chiba City Blues ooo
Aug 24 '05 #12
Larry wrote:
I found this basic PHP mailer with file attachment capability. It
works just fine, but I want to add a CC: and BCC: to its output. I
added the $cc & $bcc variables below, but unsure how to implement the
rest. Can someone please help? Thank you - LM
Apologies if this is already on your to-do list, but you're going to
want to make some changes to that code if it's going to be publicly
accessible. If not expect the spammers to take full advantage of it.
They will.
<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

$cc = $_POST['cc'];
$bcc = $_POST['bcc'];

<snip>

Kelv
--
LoHost
http://www.lohost.com
Aug 25 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Hai Nguyen | last post: by
12 posts views Thread by 1111111111 | last post: by
4 posts views Thread by Trint Smith | last post: by
5 posts views Thread by mohammaditraders | last post: by
reply views Thread by mayariasxf | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.