R
R
ReActor Dmitry Vershansky2020-07-16 10:11:52
PHP
ReActor Dmitry Vershansky, 2020-07-16 10:11:52

How to use the value of a variable/array to send to mail from under the Smarty template engine?

Good afternoon!

The essence of the problem:
There is a trivial html-form with a set of inputs, for which the php-handler "send.php" is written. It receives the data entered by the visitor (from under the $_POST superglobal array), some of their transformation and subsequent sending to email using the mail () function;

Along with sending data from the form fields, it is necessary to "take" data from the current state of the shopping cart, namely two parameters: The name of the commodity / commodity item and their corresponding quantity. The basket itself, as I understand it, and, as the Habr community clarified this point to me yesterday, is organized using Smarty. The file itself has the ".tpl" extension. (However, the whole site is organized by this templating engine)

Cart snippet:

<td class="cart_description" data-title="{l s='Description'}">
    {capture name=sep} : {/capture}
    {capture}{l s=' : '}{/capture}
    <p class="product-name"><a href="{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute, false, false, true)|escape:'html':'UTF-8'}">{$product.name|escape:'html':'UTF-8'}</a></p>
    {if $product.reference}
      <small class="cart_ref">
        {l s='SKU'}{$smarty.capture.default}{$product.reference|escape:'html':'UTF-8'}
      </small>
    {/if}
    {if isset($product.attributes) && $product.attributes}
      <small>
        <a href="{$link->getProductLink($product.id_product, $product.link_rewrite, $product.category, null, null, $product.id_shop, $product.id_product_attribute, false, false, true)|escape:'html':'UTF-8'}">
          {$product.attributes|@replace: $smarty.capture.sep:$smarty.capture.default|escape:'html':'UTF-8'}
        </a>
      </small>
    {/if}
  </td>


Handler code

<?php

    // обработчик формы оформления заказа наложенным платежом
    session_start();

    // получение данных
    $payment_number = $_POST['payment_number'];
    $fio = $_POST['fio'];
    $tel = $_POST['tel'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $message = $_POST['message'];
    $position = $product['name'];

    // обработка полученных данных
    $payment_number = htmlspecialchars($payment_number);
    $fio = htmlspecialchars($fio);
    $tel = htmlspecialchars($tel);
    $email = htmlspecialchars($email);
    $address = htmlspecialchars($address);
    $message = htmlspecialchars($message);
    
    $payment_number = trim($payment_number);
    $fio = trim($fio);
    $tel = trim($tel);
    $email = trim($email);
    $address = trim($address);
    $message = trim($message);
    
    $payment_number = urldecode($payment_number);
    $fio = urldecode($fio);
    $tel = urldecode($tel);
    $email = urldecode($email);
    $address = urldecode($address);
    $message = urldecode($message);

        // отправка письма
        if (mail("[email protected]",
    		 	 "ВсеЛуковицы : \"Заказ наложенным платежом\"",
    		 	 "Номер заказа: ".$payment_number."\n".
    		 	 "ФИО: ".$fio."\n".
    		 	 "Телефон: ".$tel."\n".
    		 	 "email: ".$email."\n".
    		 	 "Адрес доставки: ".$address."\n".
    		 	 "Товарные позиции: ".$position."\n".
    		 	 "Комментарий к заказу: ".$message,
    	 		 "From: [email protected] \r\n")) {
            $_SESSION['message_complete'] = 'Благодарим за заявку, оператор перезвонит Вам в ближайшее время.';
            // релокация
            header("Location: /index.php");
        }
    	else {
    		$_SESSION['message_error'] = 'Error! Проверьте данные или попробуйте снова!';
            // релокация
            header("Location: /index.php");
    	}


The essence of the problem:
My "pain" is that I can't figure out how to get data from two specific variables, or rather associative arrays (if I'm not mistaken). I know that one of the two target data is in $product.name, but how can I get its contents, and most importantly, use it in the php handler (send.php) to send to email, in conjunction with the data received from the fields forms. I tried to include php-code in tags of the form: {php}code php{/php}, but even putting these tags as such, without the content of the code in them, provokes the 500th server response (500 error).

I beg your help, dear colleagues.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Bedulin, 2021-01-28
@micro-CMS

Have you resolved the issue? Can you tell me how to transfer the smarty variable inside {php}..{/php} to use it, and from it again transfer it to Smarty?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question