Send Cart Session by email with Phpmailer

Asked

Viewed 43 times

0

I have this code that shows the results of the items in the cart

<?php 
    session_start();
    require_once "functions/product.php";
    require_once "functions/cart.php";

    $pdoConnection = require_once "connection.php";

    if(isset($_GET['acao']) && in_array($_GET['acao'], array('add', 'del', 'up'))) {

        if($_GET['acao'] == 'add' && isset($_GET['id']) && preg_match("/^[0-9]+$/", $_GET['id'])){ 
            addCart($_GET['id'], 1);            
        }

        if($_GET['acao'] == 'del' && isset($_GET['id']) && preg_match("/^[0-9]+$/", $_GET['id'])){ 
            deleteCart($_GET['id']);
        }

        if($_GET['acao'] == 'limpar'){ 
            limpacart($_GET['id'], 1);
        }

        if($_GET['acao'] == 'up'){ 
            if(isset($_POST['prod']) && is_array($_POST['prod'])){ 
                foreach($_POST['prod'] as $id => $qtd){
                        updateCart($id, $qtd);
                }
            }
        } 
        header('location: carrinho.php');
    }

    $resultsCarts = getContentCart($pdoConnection);
    $totalCarts  = getTotalCart($pdoConnection);


?>

How I could email items with Phpmailer?

// Instância do objeto PHPMailer
$mail = new PHPMailer;

$mail->CharSet = 'UTF-8';
$mail->SetLanguage("br");
$mail->Body = utf8_decode($body);

// Configura para envio de e-mails usando SMTP
$mail->isSMTP();

// Servidor SMTP
$mail->Host = 'mail.xxx.com.br';

// Usar autenticação SMTP
$mail->SMTPAuth = true;

// Usuário da conta
$mail->Username = '[email protected]';

// Senha da conta
$mail->Password = 'xxx@';

// Tipo de encriptação que será usado na conexão SMTP
$mail->SMTPSecure = 'ssl';

// Porta do servidor SMTP
$mail->Port = 465;

// Informa se vamos enviar mensagens usando HTML
$mail->IsHTML(true);

// Email do Remetente
$mail->From = '[email protected]';

// Nome do Remetente
$mail->FromName = $email_cart;

// Endereço do e-mail do destinatário
$mail->addAddress('[email protected]');

// Assunto do e-mail
$mail->Subject = 'xxxxx';

// Mensagem que vai no corpo do e-mail
$mail->Body = ' AQUI OS INTENS DO CARRINHO';

exit

Array ( [0] => Array ( [id] => 1906 [name] => GLVC 3100 [price] => [quantity] => 1 [subtotal] => 0 ) [1] => Array ( [id] => 1907 [name] => HDRC 3153 [price] => [quantity] => 1 [subtotal] => 0 ) [2] => Array ( [id] => 2385 [name] => HDN 01 [price] => [quantity] => 1 [subtotal] => 0 ) [3] => Array ( [id] => 2390 [name] => HDN 06 [price] => [quantity] => 1 [subtotal] => 0 ) [4] => Array ( [id] => 2392 [name] => HDN 08 [price] => [quantity] => 1 [subtotal] => 0 ) [5] => Array ( [id] => 2421 [name] => Santa Ceia [price] => [quantity] => 1 [subtotal] => 0 ) [6] => Array ( [id] => 2422 [name] => Tartaruga [price] => [quantity] => 1 [subtotal] => 0 ) [7] => Array ( [id] => 2423 [name] => Tijolo [price] => [quantity] => 1 [subtotal] => 0 ) [8] => Array ( [id] => 2451 [name] => Argamassa Refrataria [price] => [quantity] => 1 [subtotal] => 0 ) [9] => Array ( [id] => 2452 [name] => Massa Refrataria em saca [price] => [quantity] => 1 [subtotal] => 0 ) )
  • Welcome marelevou! Mate, put an example of the output of the search results of the cart. All you need to do is create your email message, would it just be written or HTML? ... To edit the question and add these details, click on edit below your question.

  • Thanks @Rbz yes I just need to create an html with the cart items, how would I do? I edited the question with the output

1 answer

0


Mounting a table, simple example:

# gera cabeçalho
$conteudo = '<table><tr><th>Nome</th><th>Preço</th><th>Qtd</th></tr>';
# gera conteúdo
foreach($array as $v) {
    $conteudo .= "<tr>";
    $conteudo .= "<td>$v['name']</td>";
    $conteudo .= "<td>$v['price']</td>";
    $conteudo .= "<td>$v['quantity']</td>";
    $conteudo .= "</tr>";
}
# fecha a table
$conteudo .= '</table>';

# inclui o conteúdo
$mail->Body = $conteudo;
  • is returning this error the server responded with a status of 500 (Internal Server Error)

  • Is e-mail sending working normally? The way you did $mail->Body = ' AQUI OS INTENS DO CARRINHO'; is receiving, correct?

  • 1

    was the concatenation the problem now worked

  • I saw that you changed my answer, but it was an example I used $array instead of $resultsCarts, because I didn’t know if I would use the other one too $totalCarts. Just assemble it the way you think best now, as if you were building a page. Just remember that to use style, you will have to use inline.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.