Page 1 of 1

PHP mail() Bcc problem

Posted: Thu Oct 12, 2006 5:04 am
by madhu
Hi all,

Am facing this problem from last 2 days, but am not able to decide where the problem is.

I have small problem when sending mails as Bcc.

Here is my code,please help how to solve this issue.

Code: Select all

[b]<?

// toid , bccpath(uploaded file path) , subject and message i will request from text field

if(isset($_REQUEST['toid']) && isset($_REQUEST['bccpath']) && isset($_REQUEST['subject']) && isset($_REQUEST['message']))
 {

 if(isset($_REQUEST['toid'])) $toid=$_REQUEST['toid'];        //toid
 if(isset($_REQUEST['bccpath'])) $bccpath=$_REQUEST['bccpath'];     //bccpath
 if(isset($_REQUEST['subject'])) $subject=$_REQUEST['subject'];              //sub
 if(isset($_REQUEST['message'])) $message=$_REQUEST['message'];          //msg

// i created a folder named as 'bcc' in the server . bcc folder contains some text files , text file contains the mailid's which i have to send as Bcc.

 if(!(is_dir('bcc')))
        @mkdir('bcc');
        @chmod('bcc', 0777);
        $temp = "bcc/" . $_FILES['bccpath']['name'];
        @move_uploaded_file($_FILES['bccpath']['tmp_name'], $temp);
        @chmod($temp, 0777);
        $filename = "bcc/" . $_FILES['bccpath']['name'];
        $fp=fopen($filename,"rb");
        $bccemails=file($filename);
        foreach($bccemails as $bccemail)
             {
             $headers.= "$bccemail" . ", ";
             }
          $header = "Bcc: $headers\r\n";
          mail($toid , $subject, $message, $header);
 }
 ?>[/b]
Now my problem is , when i tried to send mail, the Bcc mail id's are coming along with the message.
The mail is not going for the mailid which is in Bcc.

I think am clear with my problem.

Waiting for your valuable replies...............

Thanks and regards
MADHU

Posted: Thu Oct 12, 2006 5:49 am
by Chris Corbyn
You need a proper mailing library to handle Bcc correctly.

http://www.swiftmailer.org/

Posted: Thu Oct 12, 2006 5:53 am
by miro_igov
How about adding this line

Code: Select all

$headers = rtrim($headers,", ");
before

Code: Select all

$header = "Bcc: $headers\r\n";

Posted: Thu Oct 12, 2006 6:06 am
by madhu
Hi miro_igov,

I tried by giving

Code: Select all

$headers = rtrim($headers,", ");
before

Code: Select all

$header = "Bcc: $headers\r\n";
Then also the mail is not going to Bcc.

The output is displaying as :

, madhusep7@yahoo.co.in (Bcc mail id)


success (Message)




************************************************

Hi d11wtq,

How to know "proper mailing library " is handled or not???

Please suggest.........

Waiting for your valuable replies................

Posted: Thu Oct 12, 2006 6:10 am
by miro_igov
If you look into http://bg2.php.net/manual/en/function.file.php you will see a text:
Each element of the array corresponds to a line in the file, with the newline still attached.
so i suggest $bccemails = rtrim($bccemails,"\n");

Posted: Thu Oct 12, 2006 6:26 am
by madhu
I tried with what you suggested, but still mail is not working.

I think in my code some small problem is there.

Again i will start struggling, if anybody got any solution , please let me know.

Thanks in Advance
MADHU

Posted: Thu Oct 12, 2006 8:16 am
by madhu
Hi all,

Finally i understand where the problem is,

The Bcc mails are coming with the space at the end of the mailid's.

Like:

a@gmail.com , b@gmail.com , c@gmail.com , d@gmail.com,

So am trying to remove the space at the end of the mailid with

Code: Select all

$headers=rtrim($headers," ");  //$headers contains Bcc mailid's
[/b]

But the space is coming after the use of rtrim().

Please suggest me how to overcome this issue.

Thanks in advance
MADHU

Posted: Thu Oct 12, 2006 8:25 am
by feyd

Code: Select all

$header = 'Bcc: ' . implode(',', array_map('trim', $bccemails));
or some similar facsimile.

..although I will second d11's recommendation to use SwiftMailer.

Posted: Fri Oct 13, 2006 12:32 am
by madhu
Hi all,

Again am facing the same problem(Small problem).Its not trimming the space at the end of the mailid in Bcc.

My code:

Code: Select all

$fp=fopen($filename,"rb");         //open the file
        $bccemails=file($filename); //reads as an array
        
       foreach($bccemails as $bccemail)
             {
             $bccemail=rtrim($bccemail," ");        //[b]its not trimming[/b]
             $headers.= "$bccemail" . ", ";
             }
             echo $headers;
          $headers = "Bcc: $headers\r\n";
          if(mail($toid , $subject, $message, $from.$headers))
          {
           echo "success.............";
          }

Thanks in advance
MADHU

Space is added to code

Posted: Wed Oct 25, 2006 3:56 am
by lettie
Hi you have added a space after your ',' in this line of code

Code: Select all

$header .= "Bcc: $bccemail" . ", ";
Effectively putting the space back in again!