簡體   English   中英

包括 function 郵寄 php smtp

[英]Include function to mail php smtp

我想將 email 連同它支付的所有元素一起發送給用戶。

我嘗試了下一個:首先創建一個名為listItemsBought的function

function listItemsBought() {

  $counter=0;

  foreach ($_SESSION["shopping_cart"] as $product) {  ++$counter;  ?>

    <div class="card overflow-hidden">
      <div class="ribbon ribbon-top-right text-danger"><span class="bg-danger"><?php echo $product["PreuProducte2"]; ?> €</span></div>
      <div class="card-body  item-user">
        <div class="profile-pic mb-0">
          <div class="d-md-flex">
            <img src="imatges/projectes/<?php echo $product["NomProducte2"]; ?>/Logo/<?php echo $product["IMGProducte2"]; ?>" class="w-150 h-150 br-2" alt="user">
            <div class="ml-4">
              <a href="userprofile.html" class="text-dark"><h4 class="mt-1 mb-1 font-weight-bold"><?php echo $product["NomProducte2"]; ?><small class="text-muted text-small fs-13 ml-2">para <?php echo $product["CategoriaProducte2"]; ?></small></h4></a>
              <span class="text-gray"><?php echo $product["Descrip2"]; ?></span><br>
              <span class="text-muted">version: 1.6</span><br>
              <div class="rating-stars d-inline-flex mb-2 mr-3">
                <input type="number" readonly="readonly" class="rating-value star" name="rating-stars-value" value="4">
                <div class="rating-stars-container mr-2">
                  <div class="rating-star sm is--active">
                    <i class="fa fa-star"></i>
                  </div>
                  <div class="rating-star sm is--active">
                    <i class="fa fa-star"></i>
                  </div>
                  <div class="rating-star sm is--active">
                    <i class="fa fa-star"></i>
                  </div>
                  <div class="rating-star sm is--active">
                    <i class="fa fa-star"></i>
                  </div>
                  <div class="rating-star sm">
                    <i class="fa fa-star"></i>
                  </div>
                </div> 4.0
              </div>
              <h6 class="mb-0"><a href="payments.html" class="btn btn-green font-weight-bold fs-14 mr-3">$ 250.00 Buy</a> <a href="cart.html" class="btn btn-default  font-weight-semibold text-green fs-14"><i class="ti-medall fs-16 mr-2"></i>Add To wishlist</a></h6>
            </div>
          </div>
        </div>
      </div>
    </div>


    <?php 

    }

}

然后,我通過 SMTP 將 Email 發送給客戶端:

$output="
  <table style=\"padding:15px 15px;background:#f4f4f4;width:100%;font-family:arial\" cellpadding=\"0\" cellspacing=\"0\">
  <link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v5.8.1/css/all.css\" integrity=\"sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf\" crossorigin=\"anonymous\">
  <tbody>
  <tr>
  <td>

  <table style=\"max-width:540px;min-width:320px\" align=\"center\" cellspacing=\"0\">
  <tbody>

  <tr>
  <td style=\"background:#fff;border:1px solid #d8d8d8;padding:30px 30px\" align=\"center\">

  <table align=\"center\">
  <tbody>

  <tr>
  <td style=\"border-bottom:1px solid #d8d8d8;color:#666;text-align:center;padding-bottom:30px\">

  <table style=\"margin:auto\" align=\"center\">
  <tbody>
  <tr>
  <td style=\"color:#005f84;font-size:22px;font-weight:bold;text-align:center;font-family:arial\">
  ITEMS BOUGHTED 
  </td>
  </tr>
  </tbody>
  </table>
  </td>
  </tr>

  <tr>
  <td style=\"color:#666;padding:15px;padding-bottom:0;font-size:14px;line-height:20px;font-family:arial;text-align:left\">

  <div style=\"font-style:normal;padding-bottom:15px;font-family:arial;line-height:20px;text-align:left\">

  <p> Hola <span style=\"font-weight:bold;color:#4296ce;font-size:16px\">$NomYCognom</span> <br>
  <br>
  You have completed payment on our website www.example.com </p>
  <br>
  <hr>
  <br>
  <b>You bought:<br></b>
  $ItemsBoughted
  </td>
  </tr>

  </tbody>
  </table>

  </td>
  </tr>

  <tr>
  <td style=\"background:#f9f9f9;border:1px solid #d8d8d8;border-top:none;padding:15px 10px\" align=\"center\">

  <table style=\"width:100%;max-width:650px\" align=\"center\">
  <tbody>
  <tr>
  <td style=\"font-size:14px;line-height:20px;text-align:center;max-width:650px\">
  <a style=\"text-decoration:none;color:#69696c\" target=\"_blank\">
  <span style=\"color:#00ce00;font-weight:bold;max-width:180px\">BEST REGARDS:</span> 
  Payment System from <b>Example.com</b>
  </a>
  </td>
  </tr>
  </tbody>
  </table>

  </td>
  </tr>

  </tbody>
  </table>
  </td>
  </tr>

  <tr>
  <td>
  <table style=\"max-width:650px\" align=\"center\">
  <tbody>
  <tr>
  <td style=\"color:#b4b4b4;font-size:11px;padding-top:10px;line-height:15px;font-family:arial\">
  <span>  </span>
  </td>
  </tr>
  </tbody>
  </table>
  </td>
  </tr>
  </tbody>
  </table>
  ";

// Indicamos que el Contenido del mensaje está en la variable $output
  $body = $output; 

// Indicamos que el Asunto del mensaje es "ESRP - Reinici de contrasenya"
  $subject = "Example - Payed Completed";

// Indicamos que debe enviar este correo a la dirección del usuario
  $email_to1 = "$Email";

// El Email del remitente que envia el mensaje es "noreply@esrp.net"
  $fromserver = "noreply@example.com"; 

// Requiere este archivo para enviar este mensaje
  require("assets/enviarCorreos/class.phpmailer.php");
  $mail = new PHPMailer();
  $mail->IsSMTP();

// --------------------------------------------------------------------------------------------------- //
//     Introducimos las credenciales de nuestro servidor SMTP
// --------------------------------------------------------------------------------------------------- //
$mail->SMTPSecure = 'tls'; // Tipo de Conexión
$mail->Host = "          "; // Host del servidor SMTP
$mail->SMTPAuth = true; // ¿El servidor SMTP requiere identificación? (true/false)
$mail->Username = "      "; // Nombre usuario del servidor SMTP (normalmente es un Email)
$mail->Password = "      "; // Contraseña usuario del servidor SMTP
$mail->Port = 587; // Puerto del servidor SMTP
$mail->IsHTML(true); // El formato del mensaje a enviar, ¿es HTML?
$mail->From = "no-reply@example.com"; // El Email del remitente que envia el mensaje es "noreply@esrp.net"
$mail->FromName = "Example"; // El Nombre del remitente que envia el mensaje es "esrp"
$mail->Sender = $fromserver; 
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($email_to1);

if(!$mail->Send()){
  echo "Mailer Error: " . $mail->ErrorInfo; 
}
// Si no ocurre ningún error, se pone un Alert al usuario con el siguiente mensaje:
else{
  // echo "Enviado";
}



我想在變量$ItemsBoughted中顯示 function listItemsBought()的結果,其中包含客戶購買的物品列表。

我怎么能這樣做?

在您的第一個 function 中,使用您需要的所有 html 創建文本字符串並將其返回。

function listItemsBought() {

$counter=0;

$html = ''; //edit made

foreach ($_SESSION["shopping_cart"] as $product) 
{  
    ++$counter;

    $PreuProducte2 = $product['PreuProducte2'];
    $NomProducte2 = $product['NomProducte2'];
    $IMGProducte2 = $product['IMGProducte2'];
    $product['NomProducte2'];
    $CategoriaProducte2 = $product['CategoriaProducte2'];
    $Descrip2 = $product['Descrip2'];

    $html .= <<<EOD
     <div class='card overflow-hidden'>
            <div class='ribbon ribbon-top-right text-danger'><span class='bg-danger'>$PreuProducte2 €</span></div>
            <div class='card-body  item-user'>
            <div class='profile-pic mb-0'>
                <div class='d-md-flex'>
                <img src='imatges/projectes/$NomProducte2/Logo/$IMGProducte2' class='w-150 h-150 br-2' alt='user'>
                <div class='ml-4'>
                    <a href='userprofile.html' class='text-dark'><h4 class='mt-1 mb-1 font-weight-bold'>$NomProducte2<small class='text-muted text-small fs-13 ml-2'>para $CategoriaProducte2</small></h4></a>
                    <span class='text-gray'>$Descrip2</span><br>
                    <span class='text-muted'>version: 1.6</span><br>
                    <div class='rating-stars d-inline-flex mb-2 mr-3'>
                    <input type='number' readonly='readonly' class='rating-value star' name='rating-stars-value' value='4'>
                    <div class='rating-stars-container mr-2'>
                        <div class='rating-star sm is--active'>
                        <i class='fa fa-star'></i>
                        </div>
                        <div class='rating-star sm is--active'>
                        <i class='fa fa-star'></i>
                        </div>
                        <div class='rating-star sm is--active'>
                        <i class='fa fa-star'></i>
                        </div>
                        <div class='rating-star sm is--active'>
                        <i class='fa fa-star'></i>
                        </div>
                        <div class='rating-star sm'>
                        <i class='fa fa-star'></i>
                        </div>
                    </div> 4.0
                    </div>
                    <h6 class='mb-0'><a href='payments.html' class='btn btn-green font-weight-bold fs-14 mr-3'>$ 250.00 Buy</a> <a href='cart.html' class='btn btn-default  font-weight-semibold text-green fs-14'><i class='ti-medall fs-16 mr-2'></i>Add To wishlist</a></h6>
                </div>
                </div>
            </div>
            </div>
        </div>
EOD;

  }

  return $html;

}

然后在你的第二個文件中連接 function 因為它將返回一個字符串:

$output = " <sample html>" . listItemsBought() . "</sample html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM