Bcc Mail Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madhu
Forum Commoner
Posts: 82
Joined: Fri Mar 03, 2006 9:34 am

Bcc Mail Problem

Post by madhu »

Dear All,

Am doing a task in which i have to open a file which contains the mailids.

Now i will explain my task by posting the entire code. It willbe helpful for many people.


Code: Select all

<?
session_start();
error_reporting(0);
include("inc.php");   //contains db details
$db = mysql_connect($my_server,$my_user,$my_pass)
	        or die("Unable to connect to MySQL");
mysql_select_db($data_base)
	        or die("failed opening database");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<script language="javascript">
function send()
{
 if(document.sendmail.fromid.value=="")
 {
  alert("Please enter From MailAddress");
  document.sendmail.fromid.focus();
 }
 else if(document.sendmail.toid.value=="")
 {
  alert("Please enter To MailAddress");
  document.sendmail.toid.focus();
 }
 /*else if(document.sendmail.file_1.value=="")
 {
  alert("Please Select Bccpath");
  document.sendmail.file_1.focus();
 }*/
 else if(document.sendmail.subject.value=="")
 {
 alert("Please enter the subject");
 document.sendmail.subject.focus();
 }
 else if(document.sendmail.message.value=="")
 {
 alert("Please enter the message");
 document.sendmail.message.focus();
 }
 else
 {
 fromid=document.sendmail.fromid.value;
 toid=document.sendmail.toid.value;
// bccpath=document.sendmail.file_1.value;
 subject=document.sendmail.subject.value;
 message=document.sendmail.message.value;
 document.sendmail.action="sendmail.php?toid="+toid+'&subject='+subject+'&message='+message+'&fromid='+fromid;
// alert(document.sendmail.action);
 document.sendmail.submit();
 }
}

function cancel()
{
 document.sendmail.action="login.php";
 document.sendmail.submit();
}
function goto()
{
    document.sendmail.fromid.value="";
	document.sendmail.toid.value="";
   // document.sendmail.file_1.value="";
    document.sendmail.subject.value="";
    document.sendmail.message.value="";
}
</script>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;

}
a:link {
	color: #666666;
	text-decoration: underline;
}
a:visited {
	text-decoration: underline;
	color: #666666;
}
a:hover {
	text-decoration: none;
	color: #000000;
}
a:active {
	text-decoration: underline;
	color: #666666;
}
.head {
	background-image:url(images/bg_logo.gif);
	background-repeat:repeat-x;
	height:77px;
	}
.adspace {
	text-align:center;
	vertical-align:middle;
	padding: 10px 0 10px 0;
	}
.footer {
	background-image:url(images/bg_footer.gif);
	background-repeat:repeat-x;
	color:#000000;
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:10px;
	text-align:center;
	vertical-align:middle;
	height:26px;
	}
.small {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
.blue {
	background-color:#014e82;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	color:#FFFFFF;
	font-size:11px;
	padding: 0 0 0 10px;
	}
.cont {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size:11px;
	padding: 5px 10px 5px 20px;
	}
.txt {
	font-family: verdana,Arial,Helvetica,sans-serif;
	font-size: 12px;
	border: 1px;
	border-color: #999999;
	border-style: solid;
	color: #000000;
}
.style1 {
	color: #FF0000;
	font-weight: bold;
	font-size: 12px;
	font-family: Arial, Helvetica, sans-serif;
}
.style2 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-weight: bold;
	font-size: 12px;
	color:#999966;
}
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; padding: 5px 10px 5px 20px; color: #666666; }

.btn {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
	font-weight: bold;
	color: #006699;
	background-color: #FFFFFF;
	border: 1px solid #006699;
	padding-right: 1px;
	padding-left: 1px;
	width: 70px;
}

-->
</style>
<script language="javascript">

function MultiSelector( list_target, max )
{
  this.list_target = list_target;
  this.count = 0;
  this.id = 0;

  if( max )
    {
     this.max = max;
    }
  else
   {
     this.max = -1;
   };

  this.addElement = function( element )
                            {
                             if( element.tagName == 'INPUT' && element.type == 'file' )
                               {
                                element.name =  'attachment[]';
                                element.multi_selector = this;
                                element.onchange = function()
                                                          {
                                                           var new_element = document.createElement( 'input' );
                                                           new_element.type = 'file';
                                                           this.parentNode.insertBefore( new_element, this );
                                                           this.multi_selector.addElement( new_element );
                                                           this.multi_selector.addListRow( this );
                                                           this.style.position = 'absolute';
                                                           this.style.left = '-1000px';
                                                          };
                                                          if( this.max != -1 && this.count >= this.max )
                                                            {
                                                             element.disabled = true;
                                                            };
                                                          this.count++;
                                                          this.current_element = element;
                               }
                               else
                                 {
                                  alert( 'Error: not a file input element' );
                                 };
                            };

  this.addListRow = function( element )
                            {
                             var new_row = document.createElement( 'div' );
                             var new_row_button = document.createElement( 'input' );
                             new_row_button.type = 'button';
                             new_row_button.value = 'Delete';
                             new_row.element = element;
                             new_row_button.onclick= function()
                                                             {
                                                              this.parentNode.element.parentNode.removeChild( this.parentNode.element );
                                                              this.parentNode.parentNode.removeChild( this.parentNode );
                                                              this.parentNode.element.multi_selector.count--;
                                                              this.parentNode.element.multi_selector.current_element.disabled = false;
                                                              return false;
                                                              };
                             new_row.innerHTML = element.value;
                             new_row.appendChild( new_row_button );
                             this.list_target.appendChild( new_row );
                            };

};
</script>
</head>

<body>
<form name="sendmail" enctype="multipart/form-data" method="post" action="">
<input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center" valign="top"><table width="500" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="head">

            </table></td>
          </tr>
          <tr>
            <td valign="top"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">

              <tr>
                <td height="100%" align="center" bgcolor="#FFFFFF"><br>
                  <table width="70%" border="0" cellpadding="3" cellspacing="0" class="small">
                  <tr>
                    <td width="150"><strong>From</strong></td>
                    <td><input name="fromid" type="text" class="txt" size="30" ></td>
                  </tr>
                  <tr>
                    <td width="150"><strong>To</strong></td>
                    <td><input name="toid" type="text" class="txt" size="30" ></td>
                  </tr>
                  <tr>
                    <td width="150"><strong>Bcc</strong></td>
                    <td>

                    <input id="my_file_element" type="file" name="file_1"  size="30">
                    <div id="files_list"></div>
                    <script>
                	<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
	                var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 100 );
	                <!-- Pass in the file element -->
	                multi_selector.addElement( document.getElementById( 'my_file_element' ) );
                    </script>

                    </td>
                  </tr>
                  <tr>
                    <td width="150"><strong>Subject</strong></td>
                    <td><input name="subject" type="text" class="txt" size="30"></td>
                  </tr>
                  <tr>
                    <td width="150"><strong>Message</strong></td>
                    <td><textarea name="message" cols="50" rows="5" class="txt"></textarea></td>
                  </tr>
                  <tr>
                    <td width="150">&nbsp;</td>
                    <td align="center"><table width="100" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td><input name="save2" id="save2" type="button" class="btn" value="SENDMAIL" onclick="javascript:send()"></td>
                        <td><input name="save2" id="save2" type="button" class="btn" value="CANCEL" onclick="javascript:cancel()"></td>
                        <td><input name="save2" id="save2" type="button" class="btn" value="CLEAR" onclick="javascript:goto()"></td>
                      </tr>
                    </table></td>
                  </tr>
                </table>
                    <br></td>
              </tr>
            </table></td>
          </tr>

        </table></td>
      </tr>

    </table></td>
  </tr>
</table>
<?
if(isset($_REQUEST['toid']) && isset($_REQUEST['subject']) && isset($_REQUEST['message']) && isset($_REQUEST['fromid']))
 {
 if(isset($_REQUEST['fromid'])) $fromid=$_REQUEST['fromid'];
 if(isset($_REQUEST['toid'])) $toid=$_REQUEST['toid'];        //toid
 if(isset($_REQUEST['subject'])) $subject=$_REQUEST['subject'];              //sub
 if(isset($_REQUEST['message'])) $message=$_REQUEST['message'];          //msg

 $headers  = "MIME-Version: 1.0\r\n";
 $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
 $headers .= "From: $fromid\r\n";

 if(!(is_dir('bcc')))
 @mkdir('bcc');
 @chmod('bcc', 0777);

 $filesize = sizeof($_FILES['attachment']['name']);   //text files which contains the mailid's
 for ($i = 1; $i<$filesize;$i++)
 {
   $filename=$_FILES['attachment']['name'][$i];
   $source=$_FILES['attachment']['tmp_name'][$i];

   $dest = "bcc/" . $filename;
   @move_uploaded_file($source, $dest);
   @chmod($temp, 0777);

   $file_name = "bcc/" . $filename;
   $fp=fopen($file_name,"rb");
   $bccemails=file($file_name);

        foreach($bccemails as $bccemail)
             {
               $tbccemail=trim($bccemail);
               $cbccemail.= "$tbccemail". ", ";
              }
              $headers .= "Bcc: $cbccemail\r\n";
              if(mail($toid , $subject, $message, $fromid.$headers))
                {
                 echo "<html><body>";
                print "<script language=\"javascript\" type=\"text/javascript\">";
                print "alert('Mail Sent Successfully...');";
                print "</script>";
                echo("</body></html>");
                }
   fcolse($fp);

 }
}
 ?>
</form>

</body>
</html>
Now my task is am attatching more than one file at a time. That attatched text file contains the mailid's.
I will read the content(mail id's) from text file and i want to send mails to them as Bcc.

But now problem is am able to upload files but i cant send mails.

Mails are going only for the 1st text file (mailid's).

Please help me how to solve....

Thanks and regards
MADHU
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Looks like this type of thing:

Code: Select all

$_FILES['attachment']['name'][$i];
Should be:

Code: Select all

$_FILES['attachment'][$i]['name'];
madhu
Forum Commoner
Posts: 82
Joined: Fri Mar 03, 2006 9:34 am

Post by madhu »

Hi d11wtq,

I tried that, but am not getting the filepath itself...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

From what I remember, the $_FILES reference in the code is correct. Does a var_dump() of $_FILES show that you're accessing it in the right manner? If you remove the error suppression operator from the move_uploaded_file() and chmod() lines, does an error show?
Post Reply