多次发送带附件的电子邮件 - 重新发布

时间:2013-05-08 10:49:44

标签: php mysql phpmailer

我正在重新发帖,因为没有人回复我以前的帖子。

我正在尝试向附加了相应pdf文件的多个收件人发送电子邮件。我成功向多个收件人发送电子邮件,但收件人收到多封电子邮件。收件人收到的电子邮件数是我数据库中存储的电子邮件地址数。

我遇到的第二个问题是发送给收件人的附件都是同一个文件。场景应该是这样的:收件人A应该附上带有pdf A的电子邮件,带有pdf B的收件人B,依此类推。

这些pdf的文件名对应于每个收件人拥有的唯一控件号。例如。收件人A的控制号为1234,因此他的pdf命名为1234.pdf。

我试图在$ ctrl_no = mysql_result($ ctrl,0)中执行一个wile循环,但是它给出了一个错误,说服务器的内存限制已经达到。

希望你能帮我解决我的两个问题。

$input = addslashes($_POST['dep']);                                                                                                     

$email = "select email_address  from student y where y.center = '$input'"; 


if ($p_address=mysql_query($email))
{ 

 while($row = mysql_fetch_array($p_address))
 {     

 $mail->AddAddress($row[0]);

 $input = addslashes($_POST['dep']);                        


 $control = "select control_no  from student y where y.center = '$input'";

 if($ctrl=mysql_query($control)){

 $ctrl_no = mysql_result($ctrl, 0);


 $mail->AddAttachment("reports/".$ctrl_no.".pdf");  


 }
  else{

   echo "No attached pdf.";

  }

更新:$ mail函数

require_once('phpmailer/class.phpmailer.php');
include("phpmailer/class.smtp.php"); 

$mail             = new PHPMailer();

$body             = file_get_contents('phpmailer/body.html');
$body             = preg_replace('/\/b]/','',$body);

$mail->IsSMTP(); 
$mail->Host       = "smtp.gmail.com"; 
$mail->SMTPDebug  = 1;                   

$mail->SMTPAuth   = true;                  
$mail->SMTPSecure = "tls";                 
$mail->Host       = "smtp.gmail.com";      
$mail->Port       = 587;                   
$mail->Username   = "me@gmail.com";  
$mail->Password   = mypass;           

$mail->SetFrom("me@gmail.com", "Office");

$mail->AddReplyTo("me@gmail.com"," Office");

$mail->Subject    = "My Subject";

$mail->AltBody    = "Subject file";

$mail->MsgHTML($body);

3 个答案:

答案 0 :(得分:0)

您无法在一封电子邮件中向不同的收件人发送不同的附件。您需要发送单独的电子邮件。

答案 1 :(得分:0)

我认为您的问题可以通过在教程链接

下面调用函数检查来解决

Check This

用于while循环,用于检索pdf文件名和发件人的电子邮件并调用函数

if (sendEmail("From Name", "from.email@yours.probably", "emailwhere@itgoes.to", "Email Subject", $msg,$_FILES['Attachment Name'])) {
echo "Email sent"; }

对于内存限制到达,这取决于你的电子邮件服务器,即使php也可以发送100MB的附件..可能会在链接下方提供帮助

Click here

希望它有效!

答案 2 :(得分:0)

    <?php 
    $i = 0;
      while($view = mysqli_fetch_array($result)){ 
        $i++; ?>
        <tr>
          <td>
            <label><?php echo $i . ". " . $view['name']; ?>&nbsp</label>
          </td>
          <td>
            <input type="text" name="receiverEmail<?php echo $i; ?>" id="receiverEmail<?php echo $i; ?>" value="<?php echo $view['email'] ?>" readonly style="width: 300px">
          </td>
          <td>
            <input type="file" name="receiverFile<?php echo $i; ?>" id="receiverFile<?php echo $i; ?>">
          </td>
        </tr>
        <tr style="height: 5px"></tr>


  <?php }?>
        <tr style="height: 15px"></tr>
        <tr>
          <td></td>
          <td></td>
          <td style="float: right">
            <input type="submit" name="submit" id="submit">
          </td>
        </tr>
</form>
相关问题