如何将电子邮件发送到多个电子邮件地址? [英] How to send email to multiple email addresses?
问题描述
我的这张表格可以正常运行,只是它不会向多人发送电子邮件。它仅将其发送给一个人。我该如何解决?我希望能够向每个人发送一封电子邮件,并在收件人字段中添加其电子邮件地址,无论该电子邮件是否已发送给其他用户。
I have this form that is working perfectly except that it won't send emails to multiple people. It only sends it to one person. How do i fix this? I want to be able to send each individual a individual email with their email address in the to field regardless if that same email was sent to other users.
更新:我打印了$ email;如果我只选择一封电子邮件,它将打印它,如果我大于1,则它不会打印任何内容。因此,这意味着其检测到的电子邮件不超过1个。
Update: I did a print $email; and if i select only one email it would print it and then if i more than 1 then it wouldn't print anything. So that means its not detecting 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 "";
推荐答案
在PHP中,您通常会看到一些代码例如
in PHP, you usually have some code that will look like
mail($to, $subject, $msg, $mailheaders);
您可以设置多个电子邮件收件人,如
and you can set multiple email recipients like so
$to = 'one@example.com' . ', '; // note the comma
$to .= 'two@example.com';
逗号和。=将允许多个电子邮件收件人。
The comma and the .= will allow multiple email recipients.
对于两个以上的对象,您将使用
For more than two, you would use
$to = 'one@example.com' . ', ';
$to .= 'two@example.com' . ', ';
$to .= 'three@example.com';
要自动执行此操作,您可以为数组中的最后一个条目设置一个带有扭曲的foreach
To do this automatically, you could set up a foreach with a twist for the last entry in the array
foreach($email as $to) {
$lastone = next($email)===FALSE;
$to .= $email . ', ';
if(!$lastone){
$to .= $email;
}
这篇关于如何将电子邮件发送到多个电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!