Jump to content

Problem in sending email with cc fields


Go to solution Solved by mac_gyver,

Recommended Posts

Hi All,
 
I have been working on an application where I need to send  reminder mails to my users when the condition is met. I will send the reminder mail only to the users  2 days prior to the deadline. Once they crossed the deadline i need to send mail to users along with his supervisor(cc). My code is working perfectly on sending reminder mails to the users but cc to the supervisor part is not working.

 

I have stored all the details to the same table while creating the job.

 

$remail is the field where i am saving the supervisor email.Now the problem is that once the job is overdue its taking all the emails from the $remail instead of taking the jobs corresponding $remail.

 

Can any one help  me on this?  Anyone have suggestions on a better way of getting this done?

<?php

//Send mail after checking the date difference
$viewSchedule = "select * from job WHERE status = 'Open'";
$scheduleResult = mysql_query($viewSchedule,$link) or  trigger_error("SQL", E_USER_ERROR);

while ($raw = mysql_fetch_array($scheduleResult))
{
  $id=$raw['id'];
  $aid = $raw['aid'];
  $aname = $raw['aname'];
  $remail = $raw['remail'];
  $owner = $raw['owner'];
  $reminder = $raw['reminder'];
  $status=$raw['status'];
  $deadline = $raw['deadline'];
  $todaysdate =  date("Y-m-d");
  $date_diff=strtotime($todaysdate) - strtotime($deadline);
  $diff=($date_diff/(60 * 60 * 24));
  $ddate = date('d-m-Y', strtotime($deadline));
   //Cheking date difference

   //Send mail two days prior to deadline
      if ($diff == "-2") {
	    //Selecting emails
		 $dquery = "select * from users where uid ='$aid'"; 
		// echo"<br>dquery=$dquery<br>";
		 $dresult = mysql_query($dquery) or die("".mysql_errno()." Error: ".mysql_error()); 

		  if(mysql_num_rows($dresult) > 0) 
			{ 
				 $recipients = array(); 
				 while($row = mysql_fetch_array($dresult)) 
					{ 
						$recipients[] = $row['email'];
						  // echo '<span style="color:#FF7600">',$row['email'], '</span>'; 

				   }
			 } 

	//Sending message
	 $subject = "Job $id is due on $ddate"; 
	 $message .= "<html><body><table width=500 border=1px solid #ccc align=center cellpadding=0 cellspacing=0 bgcolor=#E8E8E8>";
	 $message .="<tr><th colspan=2  align=center bgcolor=#4D4D4D height=26><b>";
	 $message .= "Job Details</b> </th></tr>";
	 $message .= "<tr><th width=120  align=left>Id:</th><th width=280  align=left>$id</th></tr>";
	 $message .= "<tr><th width=120  align=left>Created By: </th><th width=280  align=left> $owner</th></tr>";
	 $message .= "<tr><th width=120  align=left>Deadline:</th><th width=280  align=left> $ddate</th></tr>";
	 $message .= "</table>";
	 $message .= "</body></html>";
	 
	 $adminemail = "admin@website.com";
	 $headers .= "From:{$adminemail}". "\r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n"; 
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
		 foreach ($recipients as $to) 
			 {     
			  $success = mail($to,$subject,$message,$headers); 
			 }
	
	  if ($success) {
		  echo "Due Reminder (2 day Prior) send successfully to the following recipients.<br>";
		  echo "Name:$aname - Id:$id <br>";
	  } else {
		  echo "Something wrong happend.Please try again.";
	  }

 }//end if

   //Send mail one day prior to deadline
      if ($diff == "-1") {
	    //Selecting emails
		 $dquery = "select * from users where uid ='$aid'"; 
		// echo"<br>dquery=$dquery<br>";
		 $dresult = mysql_query($dquery) or die("".mysql_errno()." Error: ".mysql_error()); 

		  if(mysql_num_rows($dresult) > 0) 
			{ 
				 $recipients = array(); 
				 while($row = mysql_fetch_array($dresult)) 
					{ 
						$recipients[] = $row['email'];
						  // echo '<span style="color:#FF7600">',$row['email'], '</span>'; 

				   }
			 } 

	//Sending message
	 $subject = "Job $id is due on $ddate"; 
	 $message .= "<html><body><table width=500 border=1px solid #ccc align=center cellpadding=0 cellspacing=0 bgcolor=#E8E8E8>";
	 $message .="<tr><th colspan=2  align=center bgcolor=#4D4D4D height=26><font color=#FFFFFF face=Arial, Helvetica, sans-serif><b>";
	 $message .= "Job Details</b></th></tr>";
	 $message .= "<tr><th width=120  align=left>Id:</th><th width=280  align=left> $id</th></tr>";
	 $message .= "<tr><th width=120  align=left>Created By: </th><th width=280  align=left> $owner</th></tr>";
	 $message .= "<tr><th width=120  align=left>Deadline:</th><th width=280  align=left> $ddate</th></tr>";
	 $message .= "</table>";
	 $message .= "</body></html>";
	 
	 $adminemail = "admin@website.com";
	 $headers .= "From:{$adminemail}". "\r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n"; 
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
		 foreach ($recipients as $to) 
			 {     
			  $success = mail($to,$subject,$message,$headers); 
			 }
	
	  if ($success) {
		  echo "Job Due Reminder (1 day Prior) send successfully to the following recipients.<br>";
		  echo "Name:$aname - Id:$id <br>";
	  } else {
		  echo "Something wrong happend.Please try again.";
	  }

 }//end if

   
   //Send mail if the job is overdue 
   if ($diff > "1") {
	    //Selecting emails
		 $dquery = "select * from users where uid ='$aid'"; 
		// echo"<br>dquery=$dquery<br>";
		 $dresult = mysql_query($dquery) or die("".mysql_errno()." Error: ".mysql_error()); 

		  if(mysql_num_rows($dresult) > 0) 
			{ 
				 $recipients = array(); 
				 while($row = mysql_fetch_array($dresult)) 
					{ 
						$recipients[] = $row['email'];
						   //echo '<span style="color:#FF7600">',$row['email'], '</span>'; 

				   }
			 } 

	//Sending message
	 $subject = "Job $id is Overdue by $diff"; 
	 $message .= "<html><body><table width=500 border=1px solid #ccc align=center cellpadding=0 cellspacing=0 bgcolor=#E8E8E8>";
	 $message .="<tr><th colspan=2  align=center bgcolor=#4D4D4D height=26><b>";
	 $message .= "Job Details</b></th></tr>";
	 $message .= "<tr><th width=120  align=left>Id:</th><th width=280  align=left> $id</th></tr>";
	 $message .= "<tr><th width=120  align=left>Created By: </th><th width=280  align=left> $owner</th></tr>";
	 $message .= "<tr><th width=120  align=left>Deadline:</th><th width=280  align=left> $ddate</th></tr>";
	 $message .= "</table>";
	 $message .= "</body></html>";
	 
	 $from = "admin@website.com"; 
	 $adminemail = "admin@website.com";
	 $headers .= "From:{$adminemail}". "\r\n";
	 $headers .= "Cc: $remail\r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n"; 
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
		 foreach ($recipients as $to) 
			 {     
			  $success = mail($to,$subject,$message,$headers); 
			 }
	
	  if ($success) {
		  echo "Job Over Due Reminder send successfully to the following recipients.<br>";
		  echo "Name:$aname - Id:$id <br>";

	  } else {
		  echo "Something wrong happend.Please try again.";
	  }

 }//end if
  

} // end reminder
?>

Link to comment
https://forums.phpfreaks.com/topic/279309-problem-in-sending-email-with-cc-fields/
Share on other sites

your statement of what the problem is, makes no sense -

 

$remail is the field where i am saving the supervisor email.Now the problem is that once the job is overdue its taking all the emails from the $remail instead of taking the jobs corresponding $remail.

 

 

you only have one $remail variable in the posted code and it is being set based on what is in the row retrieved from the `job` table. what exactly do you mean by that statement? what result are you getting and what result do you expect? have you verified the data stored in the `job` table is what you expect?

 

you also have too much code. about three times too much of it. you should only write code for things that differ. the code that is the same for all three of your conditions should only appear once. your conditions for two days before and one data before can be combined into ONE statement using an || (or) condition. your query to get a user's email address (while it should be gotten by having your existing query for the `job` table JOIN'ed with the user table) should only match one row and there's no need to loop over the result from that `users` table query or to loop later sending emails to what should be one to: address.

as a continuation of the above reply, you can calculate the date difference in the query, so it's both possible for the query to return the date difference value to use in your php code AND you can have the query return only the rows that match your date range of interest so that you don't need to retrieve every row from your table and loop through them all to find the ones you want.

 

likewise, you can select and format the deadline date in the query the way you want to display it.

Edited by mac_gyver

Thank you somuch for your valuable reply mac_gyver.

 

I have modified the code according to your suggestion, but still having the problem.

 

In user table i am saving all the user details and job table i am saving all the job details. Here is the example.

 

id---------Job -------------rname-----------remail-----------------------deadline-------status-------aname----aid-----createdby
1 -----Test Job1----------Sam--------sam@test.com----------2013-06-20------Open---------sara------12--------Sam

2 -----Test Job2---------David-------david@test.com---------2013-06-18------Open---------Jim-------10--------David

3 -----Test Job3---------Don---------don@test.com-----------2013-06-19------Open---------Kale------8----------Don

 

rname-requestername and aname-assignee name.

 

I am not saving assignees email id in the job table,it is stored in the user table and i am getting the assignees email using aid

$dquery = "select * from users where uid ='$aid'.

 

But the poblem what i am facing is that if the job is over due, as i told you earlier, i need to send that to supervisor also. That email(remail) i am getting from the job table directly since i saved it while creating the job.

 

So it will be on the first loop

 

while ($raw = mysql_fetch_array($scheduleResult))
{
 $remail = $raw['remail'];

}

 

so it is taking all the $remail values for the cc feild. In the above example, 2 jobs crossed the deadline. While sending email to the assignee, it will send  cc mails to both requesters david@test.com and don@test.com.

 

But i want to testjob2's cc email need to send only david@test.com and testjob3's cc mail need to send only to don@test .com.

 

Now the the correct email is sending to the assignee with the corresponding job details.This part is working perfectly alright..

 

Sorry for the lengthy reply, i was working on this for this last couple of days ,but in vain..I know iam doing something wrong here, but i could not figure it out.

 

Thank you.

 

 

 

Modified code is here. Please let me know if you can suggest anything else on modifiying the code.

//Send mail two days prior to deadline
      if ($diff == "-2") || ($diff == "-1") {
	    //Selecting emails
		 $dquery = "select * from users where uid ='$aid'"; 
		// echo"<br>dquery=$dquery<br>";
		 $dresult = mysql_query($dquery) or die("".mysql_errno()." Error: ".mysql_error()); 

		  if(mysql_num_rows($dresult) > 0) 
			{ 
				 $recipients = array(); 
				 while($row = mysql_fetch_array($dresult)) 
					{ 
						$recipients[] = $row['email'];
						  // echo '<span style="color:#FF7600">',$row['email'], '</span>'; 

				   }
			 } 

	//Sending message
	 $subject = "Job $id is due on $ddate"; 
	 $message .= "<html><body><table width=500 border=1px solid #ccc align=center cellpadding=0 cellspacing=0 bgcolor=#E8E8E8>";
	 $message .="<tr><th colspan=2  align=center bgcolor=#4D4D4D height=26><b>";
	 $message .= "Job Details</b> </th></tr>";
	 $message .= "<tr><th width=120  align=left>Id:</th><th width=280  align=left>$id</th></tr>";
	 $message .= "<tr><th width=120  align=left>Created By: </th><th width=280  align=left> $owner</th></tr>";
	 $message .= "<tr><th width=120  align=left>Deadline:</th><th width=280  align=left> $ddate</th></tr>";
	 $message .= "</table>";
	 $message .= "</body></html>";
	 
	 $adminemail = "admin@website.com";
	 $headers .= "From:{$adminemail}". "\r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n"; 
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
		 foreach ($recipients as $to) 
			 {     
			  $success = mail($to,$subject,$message,$headers); 
			 }
	
	  if ($success) {
		  echo "Due Reminder send successfully to the following recipients.<br>";
		  echo "Name:$aname - Id:$id <br>";
	  } else {
		  echo "Something wrong happend.Please try again.";
	  }

 }//end if

 
   //Send mail if the job is overdue 
   if ($diff > "0") {
	    //Selecting emails
		 $dquery = "select * from users where uid ='$aid'"; 
		// echo"<br>dquery=$dquery<br>";
		 $dresult = mysql_query($dquery) or die("".mysql_errno()." Error: ".mysql_error()); 

		  if(mysql_num_rows($dresult) > 0) 
			{ 
				 $recipients = array(); 
				 while($row = mysql_fetch_array($dresult)) 
					{ 
						$recipients[] = $row['email'];
						   //echo '<span style="color:#FF7600">',$row['email'], '</span>'; 

				   }
			 } 

	//Sending message
	 $subject = "Job $id is Overdue by $diff"; 
	 $message .= "<html><body><table width=500 border=1px solid #ccc align=center cellpadding=0 cellspacing=0 bgcolor=#E8E8E8>";
	 $message .="<tr><th colspan=2  align=center bgcolor=#4D4D4D height=26><b>";
	 $message .= "Job Details</b></th></tr>";
	 $message .= "<tr><th width=120  align=left>Id:</th><th width=280  align=left> $id</th></tr>";
	 $message .= "<tr><th width=120  align=left>Created By: </th><th width=280  align=left> $owner</th></tr>";
	 $message .= "<tr><th width=120  align=left>Deadline:</th><th width=280  align=left> $ddate</th></tr>";
	 $message .= "</table>";
	 $message .= "</body></html>";
	 
	 $from = "admin@website.com"; 
	 $adminemail = "admin@website.com";
	 $headers .= "From:{$adminemail}". "\r\n";
	 $headers .= "Cc: $remail\r\n";
	 $headers .= 'MIME-Version: 1.0' . "\r\n"; 
	 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
		 foreach ($recipients as $to) 
			 {     
			  $success = mail($to,$subject,$message,$headers); 
			 }
	
	  if ($success) {
		  echo "Job Over Due Reminder send successfully to the following recipients.<br>";
		  echo "Name:$aname - Id:$id <br>";

	  } else {
		  echo "Something wrong happend.Please try again.";
	  }

 }//end if
  

} // end reminder
  • Solution

i finally figured out what you are describing. it's due to initialization of variables (or lack of). both your $message and $header variables keep concatenating each pass through the loop. the first assignment line for $message and $header needs to have the dot . removed.

 

you still have too much repetitive code. the code i came up with to do this is about 66 lines --

<?php

//Send mail after checking the date difference
$viewSchedule = "select j.id,j.aname,j.remail,j.owner,DATE_FORMAT(j.deadline,'%d-%m-%Y') as ddate,
    DATEDIFF(CURDATE(),j.deadline) as diff,u.email as recipient
    from job j JOIN users u ON j.aid = u.uid WHERE j.status = 'Open' HAVING diff BETWEEN -2 AND -1 OR diff > 0";
$scheduleResult = mysql_query($viewSchedule,$link) or  trigger_error("SQL" . mysql_error($link), E_USER_ERROR);

$adminemail = "admin@website.com";

while ($raw = mysql_fetch_assoc($scheduleResult))
{
    $id=$raw['id'];
    $aname = $raw['aname'];
    $remail = $raw['remail'];
    $owner = $raw['owner'];
    $ddate = $raw['ddate'];
    $diff = (int)$raw['diff'];
    $recipient = $raw['recipient'];

    $message = "<html><body><table width=500 border=1px solid #ccc align=center cellpadding=0 cellspacing=0 bgcolor=#E8E8E8>";
    $message .="<tr><th colspan=2  align=center bgcolor=#4D4D4D height=26><b>";
    $message .= "Job Details</b> </th></tr>";
    $message .= "<tr><th width=120  align=left>Id:</th><th width=280  align=left>$id</th></tr>";
    $message .= "<tr><th width=120  align=left>Created By: </th><th width=280  align=left> $owner</th></tr>";
    $message .= "<tr><th width=120  align=left>Deadline:</th><th width=280  align=left> $ddate</th></tr>";
    $message .= "</table>";
    $message .= "</body></html>";

    $subject = ''; // subject is also used as a flag to determine if email should be sent (in case someone monkeys with the query to return more rows than what are used)
    //Checking date difference
    //Send mail two days and one day prior to deadline
    if ($diff == -2 || $diff == -1) {
        // setup data
        $subject = "Job $id is due on $ddate";
        $success_msg = "Due Reminder (".(abs($diff))." day(s) Prior) sent successfully to the following recipient.<br>";
        $cc = ''; // none
    }

    //Send mail if the job is overdue
    if ($diff > 0) {
        //setup data
        $subject = "Job $id is Overdue by $diff day(s)";
        $success_msg = "Job Over Due Reminder sent successfully to the following recipient.<br>";
        $cc = "Cc: $remail\r\n";
    }
    
    // send email as needed
    if($subject){ // if either logic condition above is true, $subject will be true
        
        $headers = "From:$adminemail\r\n";
        $headers .= $cc;
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

        $success = mail($recipient,$subject,$message,$headers);

        if ($success) {
            echo $success_msg;
            echo "Name:$aname - Id:$id <br>";
        } else {
            echo "Something wrong happened.Please try again.";
        }
    }
}
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.