How can I send email to multiple recipents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxamis4
    Recognized Expert Contributor
    • Jan 2007
    • 295

    How can I send email to multiple recipents

    I have a php application that has a user enter information in order to process approvals. Once the request is entered an email is sent to the manager for approval. My problem is that now they want me to send an email to both the manager and assistant manager and the director.

    I CC the director but for both managers it has become a challenge.

    Here are some details regarding the database that stores the information about the user:

    - user first and last name
    - user email
    - user group
    - user identifier if they are a manager
    - user status (active or inactive)

    What I want to do is run query that groups the users by group where the condition for manager is true and user status is active. Then loop and create one string value with all the values in the loop. Use this value as the "To" value in the php email part.

    There is a possibility that more managers can be added to the groups so this loop would come in handy.

    I have googled a few items on this and the implode function seems to be the way to go but i need a couple simple examples to learn from to use.

    Does anybody have any examples.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    For anything other than a simple email you are better off using a class. swiftmailer and phpmailer are recommended.

    Comment

    • maxamis4
      Recognized Expert Contributor
      • Jan 2007
      • 295

      #3
      Originally posted by maxamis4
      I have a php application that has a user enter information in order to process approvals. Once the request is entered an email is sent to the manager for approval. My problem is that now they want me to send an email to both the manager and assistant manager and the director.

      I CC the director but for both managers it has become a challenge.

      Here are some details regarding the database that stores the information about the user:

      - user first and last name
      - user email
      - user group
      - user identifier if they are a manager
      - user status (active or inactive)

      What I want to do is run query that groups the users by group where the condition for manager is true and user status is active. Then loop and create one string value with all the values in the loop. Use this value as the "To" value in the php email part.

      There is a possibility that more managers can be added to the groups so this loop would come in handy.

      I have googled a few items on this and the implode function seems to be the way to go but i need a couple simple examples to learn from to use.

      Does anybody have any examples.
      So here is what I have so far. I am trying to use the implode function but I am getting an error. The error
      is Warning: implode() [function.implod e]: Invalid arguments passed in....

      I have googled the error but I have not seen much resolution. Has anyone worked with the implode function?
      Code:
      <?php
      
      	require_once('auth.php');
      ?>
      
      <?php	
      
      	//Include database connection details
      	require_once('configrisks.php');
      	
      	//Array to store validation errors
      	$errmsg_arr = array();
      	
      	//Validation error flag
      	$errflag = false;
      	
      	//Connect to mysql server
      	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
      	if(!$link) {
      		die('Failed to connect to server: ' . mysql_error());
      	}
      	
      	//Select database
      	$db = mysql_select_db(DB_DATABASE);
      	if(!$db) {
      		die("Unable to select database");
      	}
      	
      	//Function to sanitize values received from the form. Prevents SQL injection
      	function clean($str) 
      	{
      		$str = @trim($str);
      		if(get_magic_quotes_gpc()) {
      			$str = stripslashes($str);
      		}
      		return mysql_real_escape_string($str);
      	}
      
      	//--Employee ID  --req_requestor
      	$emp_id=$_SESSION['SESS_MEMBER_ID'];
      	
      	
      	$sql = "SELECT email FROM tbl_users where emp_sym='con'";
      	
      	$result = mysql_query($sql);
      
      	$email_num=mysql_num_rows($result);
      	
      	while($row=mysql_fetch_array($result))
      	{
      	
      		$email_req=$row['email'];
      	
      	}
      
      
      	for($index=0; $index<$email_num;$index++)
      	{
      		$email_req[$index]=trim($email_req[$index]);		
      	}
      	
      	$to=implode("', '",$email_req);
      
      	echo $to;
      
      	
      
      
      ?>

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        $email_req is not an array
        Code:
        $email_req=$row['email'];

        Comment

        • zorgi
          Recognized Expert Contributor
          • Mar 2008
          • 431

          #5
          I don't have full picture and you might have a good reason to collect email addresses into an array but just to point out that you can send your mail without going through all that

          Code:
              while($row=mysql_fetch_array($result))
              {
           
                  //$email_req=$row['email'];
                  /*instead of collecting email addresses you 
                  could just send your mails from here one by one using one 
                  of the classes code green mentioned in his 
                  earlier post or simple mail function*/
                  
                  mail($row['email'], 'Your subject', "Your message");
           
              }

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Code:
                while($row=mysql_fetch_array($result))
                {
             
                    //$email_req=$row['email'];
                    /*instead of collecting email addresses you 
                    could just send your mails from here using one 
                    of the classes code green mentioned in his 
                    earlier post or simple mail function*/
             
                }
            some mail classes even do that themselves, that is, you tell them where to find the addresses in the database and the class fetches them itself (e.g. SwiftMailer does that)

            Comment

            • maxamis4
              Recognized Expert Contributor
              • Jan 2007
              • 295

              #7
              Sending email to multiple recipents

              The implode function works great. Here is the modified code used to fix the problem. I will respond later with the reason why I needed this. If anyone wants to use it please feel free

              Code:
                
              
              <?php    
                
                  $sql = "SELECT email FROM tbl_users where emp_sym='con'"; //QUERY BEING BUILT FOR EMAILS 
               
                  $result = mysql_query($sql);
               
                  $email_num=mysql_num_rows($result);
               
                  while($row=mysql_fetch_array($result))
                  {
               
                      [B]$email_req[]=$row['email'];[/B]//MUST DECLARE THE ARRAY AS AN ARRAY
               
                  }
               
               
                  for($index=0; $index<$email_num;$index++)
                  {
                      $email_req[$index]=trim($email_req[$index]);//TRIM THE EMAIL FOR SPACES        
                  }
               
                  $to=implode("', '",$email_req);
               
                  echo $to;//EMPLODE WILL WORK
               
               
               
               
              ?>

              Comment

              • maxamis4
                Recognized Expert Contributor
                • Jan 2007
                • 295

                #8
                Originally posted by zorgi
                I don't have full picture and you might have a good reason to collect email addresses into an array but just to point out that you can send your mail without going through all that

                Code:
                    while($row=mysql_fetch_array($result))
                    {
                 
                        //$email_req=$row['email'];
                        /*instead of collecting email addresses you 
                        could just send your mails from here one by one using one 
                        of the classes code green mentioned in his 
                        earlier post or simple mail function*/
                        
                        mail($row['email'], 'Your subject', "Your message");
                 
                    }
                In regards to the question of why I need multiple address in one email. For the system that I am building, one of the requirements was that the system include all parties being notified in one email in order to have an email trail that a message had notified all personnel. That being the case it required that when ever I sent an email all parties responsible would be notified at the same time.

                Comment

                Working...