Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old August 24th, 2005, 05:15 PM
Larry
Guest
 
Posts: n/a
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>

  #2  
Old August 24th, 2005, 05:35 PM
Stefan Rybacki
Guest
 
Posts: n/a

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


Larry wrote:[color=blue]
> 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>
>[/color]

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
  #3  
Old August 24th, 2005, 05:45 PM
Nerdlance.com
Guest
 
Posts: n/a

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


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);

  #4  
Old August 24th, 2005, 05:45 PM
Larry
Guest
 
Posts: n/a

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


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?

  #5  
Old August 24th, 2005, 05:55 PM
Nerdlance.com
Guest
 
Posts: n/a

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


You may also want to try the phpMailer class. It makes it all pretty
simple: http://phpmailer.sourceforge.net/

  #6  
Old August 24th, 2005, 05:55 PM
Nerdlance.com
Guest
 
Posts: n/a

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


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";

  #7  
Old August 24th, 2005, 06:45 PM
John Dunlop
Guest
 
Posts: n/a

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


Stefan Rybacki wrote:
[color=blue]
> Have a look at this:
> http://www.faqs.org/rfcs/rfc822.html[/color]

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
  #8  
Old August 24th, 2005, 06:55 PM
Stefan Rybacki
Guest
 
Posts: n/a

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


John Dunlop wrote:[color=blue]
> Stefan Rybacki wrote:
>
>[color=green]
>>Have a look at this:
>>http://www.faqs.org/rfcs/rfc822.html[/color]
>
>
> 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
>[/color]

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
  #9  
Old August 24th, 2005, 07:45 PM
John Dunlop
Guest
 
Posts: n/a

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


Stefan Rybacki wrote:
[color=blue]
> And I didn't know that standards are abled to be outdated.[/color]

Yes, see RFC2026 secs. 6.3 and 6.4:

http://www.ietf.org/rfc/rfc2026
[color=blue]
> What if the mail goes to an email client developed before 2001?[/color]

Fingers crossed, it gets understood!!
[color=blue]
> I didn't read the new rfc but I guess its more about extensions
> to the standard.[/color]

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
  #10  
Old August 24th, 2005, 08:55 PM
Christoph 'knurd' Jeschke
Guest
 
Posts: n/a

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


* Nerdlance.com:
[color=blue]
> $headers = "MIME-Version: 1.0\r \n";[/color]
^
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
  #11  
Old August 24th, 2005, 09:35 PM
Nerdlance.com
Guest
 
Posts: n/a

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


> 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.

  #12  
Old August 24th, 2005, 10:35 PM
Christoph 'knurd' Jeschke
Guest
 
Posts: n/a

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


* Nerdlance.com:
[color=blue]
> 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.[/color]

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
  #13  
Old August 25th, 2005, 03:25 AM
Kelv
Guest
 
Posts: n/a

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


Larry wrote:
[color=blue]
> 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[/color]

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.
[color=blue]
> <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'];[/color]
<snip>

Kelv
--
LoHost
http://www.lohost.com
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP mailer Paul Marshall answers 7 September 17th, 2005 05:05 AM
mail to bcc help please fo answers 4 July 17th, 2005 08:22 AM
Help with Jack's FormMail mcp6453 answers 0 July 17th, 2005 07:55 AM
Redirect Damo answers 1 July 17th, 2005 03:44 AM