使用包含内部循环发送邮件

时间:2019-03-20 10:56:07

标签: php html

我试图将我的循环自动插入数据库中的6条记录,唯一更改的是这些记录的日期,并且每当输入时,它都必须发送一封电子邮件,其中包含始终更新的数据,但是唯一的一条我要做的就是让它仅发送1封电子邮件,并且该电子邮件仅包含最后一个循环信息。

想法是每当您将电子邮件插入数据库时​​发送电子邮件

这是代码测试:

<?php
$replace1="27-02-2019 00:00";
$replace2="27-02-2029 00:00";
$strStart = '2019-09-10';
$strEnd = '2029-09-10'; 
$dteStart = new DateTime($strStart); 
$dteEnd   = new DateTime($strEnd); 
$dteDiff  = $dteStart->diff($dteEnd); 
$anos=$dteDiff->format("%Y");
$dias=$dteDiff->format("%d");
$meses=$dteDiff->format("%m");
$repete = ($anos * 12);
$rotina="6";
if($rotina == "6"){
    $assunto = "Rotina Semestral";
    $repete = ($repete / 6);
     for($i = 0; $i <= $repete; $i++){
        echo $i."==".$replace1." - ";
        echo $replace2."<br>";
        $replace1 = date('d-m-Y H:m',strtotime($replace1." +6 Months")); 
        include('email1.php');
    };
}elseif($rotina == "3"){
   //code...
   };
}elseif($rotina == "1"){
    $repete = $anos;
     for($i = 0; $i <= $repete; $i++){
        //code... 
    };    
};

这里是email1.php

<?php
 $path = $_SERVER['DOCUMENT_ROOT'] . '\dist\PHPMailer_5.2.0\\';
 $file = $path . 'class.phpmailer.php';
 include($file);

   $mail = new PHPMailer();
   $mail->CharSet = 'UTF8';

   $mail->IsSMTP();
   $mail->Host = "$smtp";
   $mail->SMTPAuth = false;

    $mail->From = "$emailhost";
    $mail->FromName = "Nova tarefa  SIGMA ";
    $mail->AddAddress ($email_post);

    $mail->WordWrap = 99999;
    $mail->IsHTML(true);


    $mail->Subject = "Nova tarefa SIGMA WO/NF/ID Cliente: ".$won1;
    $mail->AddEmbeddedImage('img/baixo.png', 'baixo');
    $mail->Body    = "<div style='margin-left:0px; padding:0px;width:800px;height:772px;style='color:#3366FF;'>
    <h1 style='text-align: center; background-color:#0B95ED; color:#FFFFFF;font-family: Arial, Helvetica, sans-serif;line-height:2.5em;'>Informação do Pedido - Novo Pedido</h1>
    <hr><br>

    <!-- texto entre linhas -->
      <table align='center' border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
      <tr>
          <td align='center' valign='top' width='50%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>WO/NF/ID Cliente: 
                      </td>
                  </tr>

                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <!-- WO/NF/ID Cliente: -->
                      $won
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>Responsável do pedido:
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <!-- Responsável do pedido:  -->
                      $res_2
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>Técnico 1: 
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <!-- Técnico 2:  -->
                      $tec1_1
                      </td>
                  </tr>
              </table>
          </td>
          <td align='center' valign='top' width='50%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
              <tr>
                      <td class='rightColumnContent' bgcolor='#B0C4DE'>
                     <b> Cliente: 
                      </td>
                  </tr>

                  <tr>
                      <td class='rightColumnContent' bgcolor=''>
                      $cli
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor='#B0C4DE'>
                      <font color='#B0C4DE'>Vazio</font>
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor=''>
                      <font color='#FFFFFF'>Vazio</font>
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor='#B0C4DE'>
                      <b>Técnico 2: 
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor=''>
                      $tec2_2
                      </td>
                  </tr>
              </table>
          </td>

  </table>
  <table border='0' cellpadding='0' cellspacing='0'  width='100%' id='templateColumns'>
      <tr>
          <td align='center' valign='top' width='100%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>Descrição da Tarefa:
                      </td>
                  </tr>
              </table>
          </td>        
      </tr>
  </table>
  <table border='0' cellpadding='0' cellspacing='0'  width='100%' id='templateColumns'>
      <tr>
          <td align='center' valign='top' width='100%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <div class='b'>
                      $descre
                      </div>                     
                      </td>
                  </tr>
              </table>
          </td>        
      </tr>
  </table>

    <div>
  </div>
  <hr>
  <table width='100%' border='0' cellspacing='0' cellpadding='0'>
    <tr>
      <td><center>
        <div>
          <!--[if mso]>
            <v:roundrect xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w='urn:schemas-microsoft-com:office:word' href='http://sigma.eda.pt' style='height:36px;v-text-anchor:middle;width:150px;' arcsize='5%' strokecolor='#EB7035' fillcolor='#EB7035'>
              <w:anchorlock/>
              <center style='color:#ffffff;font-family:Helvetica, Arial,sans-serif;font-size:16px;'>Bom Trabalho! &rarr;</center>
            </v:roundrect>
          <![endif]-->
          <a href='http://sigma.eda.pt' style='background-color:#EB7035;border:1px solid #EB7035;border-radius:3px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:16px;line-height:44px;text-align:center;text-decoration:none;width:150px;-webkit-text-size-adjust:none;mso-hide:all;'>Bom Trabalho! &rarr;</a>
        </div>
      </td>
    </tr>
  </table>
  ";
                   $mail->Send();

1 个答案:

答案 0 :(得分:0)

我暂时无法测试,但是新文件将是这样的

<?php
 $path = $_SERVER['DOCUMENT_ROOT'] . '\dist\PHPMailer_5.2.0\\';
 $file = $path . 'class.phpmailer.php';
 **include_once($file);**
 function my_send_mail($smtp,$emailhost,$email_post,$won1,$won,$res_2,$tec1_1,$cli,$tec2_2,$descre){
   $mail = new PHPMailer();
   $mail->CharSet = 'UTF8';

   $mail->IsSMTP();
   $mail->Host = "$smtp";
   $mail->SMTPAuth = false;

    $mail->From = "$emailhost";
    $mail->FromName = "Nova tarefa  SIGMA ";
    $mail->AddAddress ($email_post);

    $mail->WordWrap = 99999;
    $mail->IsHTML(true);


    $mail->Subject = "Nova tarefa SIGMA WO/NF/ID Cliente: ".$won1;
    $mail->AddEmbeddedImage('img/baixo.png', 'baixo');
    $mail->Body    = "<div style='margin-left:0px; padding:0px;width:800px;height:772px;style='color:#3366FF;'>
    <h1 style='text-align: center; background-color:#0B95ED; color:#FFFFFF;font-family: Arial, Helvetica, sans-serif;line-height:2.5em;'>Informação do Pedido - Novo Pedido</h1>
    <hr><br>

    <!-- texto entre linhas -->
      <table align='center' border='0' cellpadding='0' cellspacing='0' width='100%' id='templateColumns'>
      <tr>
          <td align='center' valign='top' width='50%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>WO/NF/ID Cliente: 
                      </td>
                  </tr>

                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <!-- WO/NF/ID Cliente: -->
                      $won
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>Responsável do pedido:
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <!-- Responsável do pedido:  -->
                      $res_2
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>Técnico 1: 
                      </td>
                  </tr>
                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <!-- Técnico 2:  -->
                      $tec1_1
                      </td>
                  </tr>
              </table>
          </td>
          <td align='center' valign='top' width='50%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
              <tr>
                      <td class='rightColumnContent' bgcolor='#B0C4DE'>
                     <b> Cliente: 
                      </td>
                  </tr>

                  <tr>
                      <td class='rightColumnContent' bgcolor=''>
                      $cli
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor='#B0C4DE'>
                      <font color='#B0C4DE'>Vazio</font>
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor=''>
                      <font color='#FFFFFF'>Vazio</font>
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor='#B0C4DE'>
                      <b>Técnico 2: 
                      </td>
                  </tr>
                  <tr>
                      <td class='rightColumnContent' bgcolor=''>
                      $tec2_2
                      </td>
                  </tr>
              </table>
          </td>

  </table>
  <table border='0' cellpadding='0' cellspacing='0'  width='100%' id='templateColumns'>
      <tr>
          <td align='center' valign='top' width='100%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
                  <tr>
                      <td class='leftColumnContent' bgcolor='#B0C4DE'>
                      <b>Descrição da Tarefa:
                      </td>
                  </tr>
              </table>
          </td>        
      </tr>
  </table>
  <table border='0' cellpadding='0' cellspacing='0'  width='100%' id='templateColumns'>
      <tr>
          <td align='center' valign='top' width='100%' class=' '>
              <table border='0' cellpadding='10' cellspacing='0' width='100%'>
                  <tr>
                      <td class='leftColumnContent' bgcolor=''>
                      <div class='b'>
                      $descre
                      </div>                     
                      </td>
                  </tr>
              </table>
          </td>        
      </tr>
  </table>

    <div>
  </div>
  <hr>
  <table width='100%' border='0' cellspacing='0' cellpadding='0'>
    <tr>
      <td><center>
        <div>
          <!--[if mso]>
            <v:roundrect xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w='urn:schemas-microsoft-com:office:word' href='http://sigma.eda.pt' style='height:36px;v-text-anchor:middle;width:150px;' arcsize='5%' strokecolor='#EB7035' fillcolor='#EB7035'>
              <w:anchorlock/>
              <center style='color:#ffffff;font-family:Helvetica, Arial,sans-serif;font-size:16px;'>Bom Trabalho! &rarr;</center>
            </v:roundrect>
          <![endif]-->
          <a href='http://sigma.eda.pt' style='background-color:#EB7035;border:1px solid #EB7035;border-radius:3px;color:#ffffff;display:inline-block;font-family:sans-serif;font-size:16px;line-height:44px;text-align:center;text-decoration:none;width:150px;-webkit-text-size-adjust:none;mso-hide:all;'>Bom Trabalho! &rarr;</a>
        </div>
      </td>
    </tr>
  </table>
  ";
                   $mail->Send();
 }

,您的默认文件将是

<?php
include('email1.php');
$replace1="27-02-2019 00:00";
$replace2="27-02-2029 00:00";
$strStart = '2019-09-10';
$strEnd = '2029-09-10'; 
$dteStart = new DateTime($strStart); 
$dteEnd   = new DateTime($strEnd); 
$dteDiff  = $dteStart->diff($dteEnd); 
$anos=$dteDiff->format("%Y");
$dias=$dteDiff->format("%d");
$meses=$dteDiff->format("%m");
$repete = ($anos * 12);
$rotina="6";
switch($rotina){
case "6":
    $assunto = "Rotina Semestral";
    $repete = ($repete / 6);
     for($i = 0; $i <= $repete; $i++){
        echo $i."==".$replace1." - ";
        echo $replace2."<br>";
        $replace1 = date('d-m-Y H:m',strtotime($replace1." +6 Months")); 

        my_send_mail($smtp,$emailhost,$email_post,$won1,$won,$res_2,$tec1_1,$cli,$tec2_2,$descre);
    };
    break;
case "3":
   //code...
break;
case "1":
    $repete = $anos;
     for($i = 0; $i <= $repete; $i++){
        //code... 
    };    
break;
}

但是我不知道您将mail1.php文件中使用的变量放在哪里

我不知道您用于sendmail的PHP库是什么,但是在邮件发送http://php.net/manual/en/language.exceptions.php中实现异常

对不起,我的英语不好

替换

$mail->Send()

这是

try {
  if ( !$mail->Send() ) {
    $error = "Unable to send to: " . $to . "<br />";
    throw new phpmailerAppException($error);
  } else {
    echo 'Message has been sent using <?php echo strtoupper($_POST['test_type']); ?><br /><br />';
  }
} catch (phpmailerAppException $e) {
  $errorMsg[] = $e->errorMessage();
}

if ( count($errorMsg) > 0 ) {
  foreach ($errorMsg as $key => $value) {
    $thisError = $key + 1;
    echo $thisError . ': ' . $value;
  }
}