多个收件人电子邮件php

时间:2015-11-06 22:42:34

标签: php email

如何在php中发送给多个收件人? 我只有一个输入字段供用户输入所有收件人。代码已经有一个javascript,可以创建标签和自动填充。代码仅发送到第一个输入。 这是代码:



    <div class="container">
    <div class="column-center"></div>
    <div class="column-left"></div>
    <div class="column-right"><img src="castle1.png"></div>
    <div class="column-center-top"><img src="top.png"></div>
    <div class="menu"></div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

基本上,发送给一对收件人的操作是:

$message = "Email Body goes here";
$to = $recepient1.",".$recepient2.",".$recepient3; 
$subject = 'Some Subject';
$from = $sender_email;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
mail($to, $subject, $message, $headers);

答案 1 :(得分:-1)

您没有发布您的(完整)电子邮件代码,只是将某些数据放入数据库的部分。

看看这篇文章:

PHP form send email to multiple recipients

如果您无法自行实施,请使用完整的电子邮件代码更新您的问题,以便我可以为您完成。

修改 我实现了这个片段;你必须在旧的html表单字段中输入带有(逗号+空格)的收件人;

<?php
$recipient = "recipienta, recipientb";
$recipients =  explode(", ",$recipient);    
foreach ($recipients as $recipient) { 
    echo $recipient;
}
?>

以下是您的代码的相关部分与实现的代码段,我没有所有的依赖项,所以我无法测试它,但它应该工作,如果它不只是让我知道你的问题,我'修复它&lt; 3;

<?php
error_reporting(0);
session_start();
include_once 'config.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
$res=mysql_query("SELECT * FROM user WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

$deptm = mysql_query("SELECT * FROM departments WHERE dep_ID = ".$userRow['department_id']."");
$deptment = mysql_fetch_array($deptm);

if(isset($_POST['send-btn']))
{
    $recipient = $_POST['TO'];
    $recipients =  explode(", ",$recipient);   
    foreach ($recipients as $recipient) { 

        $check = mysql_query("SELECT * FROM user WHERE faculty_number='$recipient'");
        $rec = mysql_fetch_array($check);
        if($userRow['position'] == 'Department Head')
        {
            if($rec['position'] != 'Dean' && $rec['position'] != 'Faculty Secretary')
            {
                $subject = $_POST['subject'];
                $message = $_POST['message'];
                $indicator = $_POST['indicator'];
                $counter = $_POST['typ_cnt'];
                $date = date('M-j');
                /*$cnt=array();
                $cnt=count($_POST['department']);
                for($i=0;$i<$cnt;$i++)
                {
                    $recipient=$_POST['department'][$i];*/
                    if($del=mysql_query("INSERT INTO memo(memo_date, iso, memo_subject, content, memo_sender) VALUES ('$date', 'UST:".$indicator." - ".$counter."/AY 2015-2016', '$subject','$message','".$userRow['faculty_number']."')"))
                    {
                        $getID = mysql_query("SELECT * FROM memo ORDER BY memo_ID DESC LIMIT 1");
                        $id = mysql_fetch_array($getID);
                        if($recipient=mysql_query("INSERT INTO recipients(id,type,iso,memo_sender,recipient_num,read_status,read_timestamp,acknowledgement,ack_timestamp,delete_status) VALUES ('".$id['memo_ID']."','memo','UST:".$indicator." - ".$counter."/AY 2015-2016','".$userRow['faculty_number']."', '$recipient', 'unread', '---', 'Not Acknowledged', '---', 'active')"))
                        {
                                ?>
                                <script>alert('Memo Sent');</script>
                                <?php
                        }
                        else{
                        ?>
                            <script>alert('Failed');</script>
                        <?php
                    }
                    }
                    else{
                    ?>
                        <script>alert('Failed');</script>
                    <?php
                    }
                //}
            }else{
                ?>
                <script>alert('Your recipient is not allowed');</script>
                <?php
            }
        }else if($userRow['position'] == 'Dean')
        {
            $subject = $_POST['subject'];
                $message = $_POST['message'];
                $indicator = $_POST['indicator'];
                $counter = $_POST['typ_cnt'];
                $date = date('M-j');
                /*$cnt=array();
                $cnt=count($_POST['department']);
                for($i=0;$i<$cnt;$i++)
                {
                    $recipient=$_POST['department'][$i];*/
                    if($del=mysql_query("INSERT INTO memo(memo_date, iso, memo_subject, content, memo_sender) VALUES ('$date', 'UST:".$indicator." - ".$counter."/AY 2015-2016', '$subject','$message','".$userRow['faculty_number']."')"))
                    {
                        $getID = mysql_query("SELECT * FROM memo ORDER BY memo_ID DESC LIMIT 1");
                        $id = mysql_fetch_array($getID);
                        if($recipient=mysql_query("INSERT INTO recipients(id,type,iso,memo_sender,recipient_num,read_status,read_timestamp,acknowledgement,ack_timestamp,delete_status) VALUES ('".$id['memo_ID']."','memo','UST:".$indicator." - ".$counter."/AY 2015-2016','".$userRow['faculty_number']."', '$recipient', 'unread', '---', 'Not Acknowledged', '---', 'active')"))
                        {
                                ?>
                                <script>alert('Memo Sent');</script>
                                <?php
                        }
                        else{
                        ?>
                            <script>alert('Failed');</script>
                        <?php
                    }
                    }
                    else{
                    ?>
                        <script>alert('Failed');</script>
                    <?php
                    }
                //}
        }
    }
}
?>