简体   繁体   中英

Multiple sending of email with attachment - repost

I am re-posting this because no one answered my previous post.

I am trying to send emails to multiple recipients with respective pdf files attached. I was successful with sending emails to multiple recipient but the recipients receive multiple emails. The number of emails received by a recipient is the number of email addresses stored in my database.

The second problem that I have encountered was that the attachment sent to the recipients were all the same file. The scenario should be like this: recipient A should have email attached with pdf A, recipient B with pdf B, so on and so fort.

Those pdf's have file names that correspond to the unique control number that each recipient has. Eg recipient A has control number 1234, so his pdf is named as 1234.pdf.

I tried to do a wile loop in $ctrl_no = mysql_result($ctrl, 0) but it gives an error saying that the memory limit of the server has reached.

Hope you could help solve my 2 problems.

$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.";

  }

UPDATE: The $mail function

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);

You can't send different attachments to different recipients in one email. You will need to send separate emails.

I think your problem can be solved, just by using a calling a function check below tutorial link

Check This

For this use while loop, for retriving the pdf file name, and email of sender and call the function

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

For Memory Limit reached,it depends on your email server, even php can send attachement of 100MB too..May be below link will help

Click here

hope it works!

    <?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM