473,772 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_fi le($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt, 'rb');
$data = fread($file,fil esize($fileatt) );
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Bo undary_x{$semi_ rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mi me_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(bas e64_encode($dat a));

// Add file attachment to the message
$message .= "--{$mime_boundary }\n" .
"Content-Type: {$fileatt_type} ;\n" .
" name=\"{$fileat t_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fi leatt_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 21179
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_fi le($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt, 'rb');
$data = fread($file,fil esize($fileatt) );
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Bo undary_x{$semi_ rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mi me_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(bas e64_encode($dat a));

// Add file attachment to the message
$message .= "--{$mime_boundary }\n" .
"Content-Type: {$fileatt_type} ;\n" .
" name=\"{$fileat t_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fi leatt_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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1090
by: Hai Nguyen | last post by:
Would anybody please tell me what is this error and what i should do I have 5 files: page1.aspx,page1.aspx.cs; page2.aspx.page2.aspx.cs and page3.cs both page1.aspx.cs and page2.aspx.cs inherit page3 page3.cs is like a library to share common methods I got this error
12
1537
by: 1111111111 | last post by:
I am writing a program that tells users to enter 2 sides of a rectangle. From that the program will output a drwaing of the rectangle using "*"'s... Here is my problem. Say I enter 5 and 10 for the 2 sides.. This is what is output. ********** * * * * * * * **********
4
1170
by: Trint Smith | last post by:
How can I improve this code please? It sometimes produces this error: "Object reference not set to an instance of an object" When I do this: strSQL = "UPDATE TBL_Items SET" & _ " item_itemnumber = " & PrepareStr(Label6.Text) & _ " ,item_itemcurrentbidprice = " & PrepareStr(itemCurrentbidprice) & _ " ,item_itembidhistory = " & PrepareStr(itembidHistory) & _
1
1621
by: omid1980 | last post by:
hi I have an examination and i shuld answer this question please help me. Explain the advantages of using XML with a database for ecommerce sites?
5
2205
by: mohammaditraders | last post by:
Question # 1 Write a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions Cal_percentage() which calculate the percentage of the student by the formula (Ob_marks * 100 )/Total_marks and Display() which show all information of the student. The class should also contain the default constructor which initializes all the data member of...
4
1373
by: isabelle | last post by:
hi, every body.. help me in this program. please!! write a c++ program that converts hexadecimal digit to its corresponding decimal value. input/output: Enter a hexadecimal digit :A The corresponding decimal value:10
13
2611
by: ishakarthika | last post by:
how can i compare the a private variable of a class and a value in the column of a text file. there is a syntax error in my code while comparing. senario is i am getting bus details like busno, source , destination,type of bus, price/head . I should compare the busno which i get through object to the bus no which is present in a .txt file. and contents in the text file is as the format below. |1488|xxx|uuuu|A/c|150....
0
183
by: mayariasxf | last post by:
http://forums.vogue.com.au/member.php?u=91686 tube8 http://forums.vogue.com.au/member.php?u=91688 redtube http://forums.vogue.com.au/member.php?u=91689 pornhub http://forums.vogue.com.au/member.php?u=91690 yobt http://www.dynamicdrive.com/forums/member.php?u=33924 tube8 http://www.dynamicdrive.com/forums/member.php?u=33925 redtube Visit this sites please
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.