How to send an email to multiple email addresses?

I have this form that works great, except that it will not send emails to multiple people. He sends it to only one person. How to fix it? I want each individual email address to send its own email address in the field, regardless of whether the same address was sent to other users.

Update: I made an e-mail for printing; and if I select only one email, it will print it, and then if I am more than 1, it will not print anything. Thus, this means that it does not detect more than 1 email.

$sql = "SELECT email from friend_email_ids WHERE my_id='$id'"; $result = mysql_query($sql); $query = mysql_query($sql) or die ("Error: ".mysql_error()); if ($result == "") { echo ""; } echo ""; $rows = mysql_num_rows($result); $emails = array(); if($rows == 0) { print(""); } elseif($rows > 0) { while($row = mysql_fetch_array($query)) { array_push($emails, $row['email']); print(""); } } $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; $headers .= "From: $usermail\r\n"; $subject = "$full_name added"; $message = "<html><body>"; $message .= "Hello, <br><br>$full_name posted someth<br><br>"; $message .= "<a href=www.domain.com/signup.php?t=&sign=>Click here.</a><br><br>"; $message .= "</body></html>"; foreach ($emails as $email) { mail($email, "Subject: $subject", $message, $headers); } echo ""; 
-3
source share
3 answers

in PHP, you usually have code that looks like

  mail($to, $subject, $msg, $mailheaders); 

and you can set multiple email recipients for example

  $to = ' one@example.com ' . ', '; // note the comma $to .= ' two@example.com '; 

Comma and. = allow multiple email recipients.

For more than two, you should use

  $to = ' one@example.com ' . ', '; $to .= ' two@example.com ' . ', '; $to .= ' three@example.com '; 

To do this automatically, you can tweak foreach with the last twist in the array

  foreach($email as $to) { $lastone = next($email)===FALSE; $to .= $email . ', '; if(!$lastone){ $to .= $email; } 
+1
source

You need to use MySQL GROUP_CONCAT to combine emails together, separated by commas. Filling in the value separated by commas, in the "To" field it will automatically send to several recipients:

 SELECT GROUP_CONCAT(DISINCT `email` SEPARATOR ',') from friend_email_ids WHERE my_id='$id'" GROUP BY `email; 

Concat MySQL Group:

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

The PHP Doc explains several email recipients in the mail() function:

http://php.net/manual/en/function.mail.php

+1
source

The problem is your request:

 $sql = "SELECT email FROM friend_email_ids WHERE my_id='$id'"; 

You get only one email address, because you select only one email address from the user with the identifier stored in $id . To fix this, you must select all the required identifiers. First, you need to make sure that you have a variable called $ids that stores a list of identifiers separated by commas. Then change your request to:

 $sql = "SELECT email FROM friend_email_ids WHERE my_id IN ($ids)"; 

If your identifiers are integers, there is no need to quote them as in the original query. If they are strings, be sure to list them in a comma-separated list. If you do not know how to make your $ids variable as I described, you need to read some php tutorials. This is the main material.

Otherwise, your script should work. But, as someone noted in the comments, you have a lot of extra code with empty lines, you can delete most of it.

0
source

All Articles