K
K
Konstantin Spirin2018-10-09 11:13:50
Yii
Konstantin Spirin, 2018-10-09 11:13:50

Sending mail in yii2?

Good afternoon . I'm making a store in yii2. Can't send data from input button to email via swiftmailer. The mail itself is working fine. But I want to insert from input into a letter in a certain place, I can’t help pliz.
button - <?= $form->field($order, 'email') ?>
$email = Yii::$app->request->post('order[email]');
$email = post('order[email]'); - not suitable
Here are the main texts:
CartController.php :

public function actionView(){
$session = Yii::$app->session;
$session->open();
$this->setMeta('Корзина');
$order = new Order();
if( $order->load(Yii::$app->request->post()) ){
$order->qty = $session['cart.qty'];
$order->sum = $session['cart.sum'];

if($order->save()){
$this->saveOrderItems($session['cart'], $order->id);
Yii::$app->session->setFlash('success', 'Ваш заказ принят. Менеджер вскоре свяжется с Вами.');
Yii::$app->mailer->compose('order', ['session' => $session])
->setFrom(['[email protected]'])
->setTo($order->email)
->setSubject('Заказ')
->setTextBody('Текст сообщения')
->send();
$email = $oder->email;echo"$email";
Yii::$app->mailer->compose('order', ['session' => $session])
->setFrom(['[email protected]'])
->setTo('[email protected]')
->setSubject('Заказ')
->send();


$session->remove('cart');
$session->remove('cart.qty');
$session->remove('cart.sum');
return $this->refresh();
}else{
Yii::$app->session->setFlash('error', 'Ошибка оформления заказа');
}
}
return $this->render('view', compact('session', 'order',['order' => $order]));
}

in the folder mail - order.php:
<?php
use yii\helpers\Html;
?>

<div class="table-responsive">
<table style="width: 100%; border: 1px solid #ddd; border-collapse: collapse;">
<thead>
<tr style="background: #f9f9f9;">
<th style="padding: 8px; border: 1px solid #ddd;">Наименование</th>
<th style="padding: 8px; border: 1px solid #ddd;">Кол-во</th>
<th style="padding: 8px; border: 1px solid #ddd;">Цена</th>
<th style="padding: 8px; border: 1px solid #ddd;">Сумма</th>
</tr>
</thead>
<tbody>
<?php foreach($session['cart'] as $id => $item):?>
<tr>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['name']?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['qty']?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['price']?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $item['qty'] * $item['price']?></td>
</tr>
<?php endforeach?>
<tr>
<td colspan="3" style="padding: 8px; border: 1px solid #ddd;">Итого: </td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $session['cart.qty']?></td>
</tr>
<tr>
<td colspan="3" style="padding: 8px; border: 1px solid #ddd;">На сумму: </td>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $session['cart.sum']?></td>
</tr>
</tbody>

<thead>
<tr style="background: #f9f9f9;">
<th style="padding: 8px; border: 1px solid #ddd;">Телефон</th>
<th style="padding: 8px; border: 1px solid #ddd;">Емаил</th>

</tr>
</thead>
<tbody>

<?php


//if ($order->load(Yii::$app->request->post()) && $order->validate()) {
// выполнение кода, если данные введены правильно
// иначе будет показа ошибка (Yii выводит ошибки автоматически)
// $email=$order->email; // используется вместо $_POST['name']


//}



?>


<tr>
<td style="padding: 8px; border: 1px solid #ddd;"><?= $order->email?></td>
<td style="padding: 8px; border: 1px solid #ddd;"><?php echo"$email"; ?></td>

</tr>
<?php //endforeach?>

</tbody>






</table>
</div>

в папке views/cart/view.php:
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
?>
<div class="container">

<?php

$email = $oder->email;echo"$email";
if (isset($order->addres)) {
$email = $order->addres;
echo"$email";

} else {
$Err.="Ошибка передачи данных с поля id!<br>";

echo"ошибка";

};
?>

<?php if( Yii::$app->session->hasFlash('success') ): ?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<?php echo Yii::$app->session->getFlash('success'); ?>
</div>
<?php endif;?>

<?php if( Yii::$app->session->hasFlash('error') ): ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<?php echo Yii::$app->session->getFlash('error'); ?>
</div>
<?php endif;?>
<?php if(!empty($session['cart'])): ?>
<div class="table-responsive">
<table class="table table-hover table-striped">
<thead>
<tr>
<th>Фото</th>
<th>Наименование</th>
<th>Кол-во</th>
<th>Цена</th>
<th>Сумма</th>
<th><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></th>
</tr>
</thead>
<tbody>
<?php foreach($session['cart'] as $id => $item):?>
<tr>
<td><?= \yii\helpers\Html::img("@web/images/products/{$item['img']}", ['alt' => $item['name'], 'height' => 50]) ?></td>
<td><a href="<?= Url::to(['product/view', 'id' => $id])?>"><?= $item['name']?></a></td>
<td><?= $item['qty']?></td>
<td><?= $item['price']?></td>
<td><?= $item['qty'] * $item['price']?></td>
<td><span data-id="<?= $id?>" class="glyphicon glyphicon-remove text-danger del-item" aria-hidden="true"></span></td>
</tr>
<?php endforeach?>
<tr>
<td colspan="5">Итого: </td>
<td><?= $session['cart.qty']?></td>
</tr>
<tr>
<td colspan="5">На сумму: </td>
<td><?= $session['cart.sum']?></td>
</tr>
</tbody>
</table>
</div>
<hr/>
<?php


$form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
]) ?>
<?= $form->field($order, 'name')?>
<?= $form->field($order, 'email') ?>
<?= $form->field($order, 'phone')?>
<?= $form->field($order, 'address')?>
<?= Html::submitButton('Заказать', ['class' => 'btn btn-success'])?>
<?php ActiveForm::end()?>
<?php else: ?>
<h3>Корзина пуста</h3>
<?php endif;?>
</div>

That is, the code for transferring data from the input button to the letter.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Spirin, 2018-10-12
@konst2018

He decided everything himself. Saving the drowning, as they say. Special thanks to Maxim Timofeev, he at least sent him in the right direction.
Here is the corrected code for anyone who may need it:
Cart.php
public function actionView(){
$session = Yii::$app->session;
$session->open();
$this->setMeta('Cart');
$order = new Order();
if( $order->load(Yii::$app->request->post()) ){
$order->qty = $session['cart.qty'];
$order->sum = $session['cart.sum'];
if($order->save()){
$email = [$order->email];
$name = [$order->name];
$phone = [$order->phone];
$address = [$order->address];

$this->saveOrderItems($session['
Yii::$app->session->setFlash('success', 'Your order has been accepted. The manager will contact you shortly.');
->setFrom(['[email protected]'])
->setTo($order->email)
->setSubject('Order')
->send();
->setFrom(['[email protected]'])
->setTo('[email protected]'
) ->setSubject('Order')
->send();
$session->remove('cart');
$session->remove('cart.qty');
$session->remove('cart.sum');
return $this->refresh();
}else{
Yii::$app->session->setFlash('error', 'Order checkout error');
}
}
return $this->render('view',compact('session', 'order'));
}
Order.php:
<?php
use yii\helpers\Html;
?>
Product details

NameQtyPriceSum
<?= $item['name']?><?= $item['qty']?><?= $item['price']?><?= $item['qty'] * $item['price']?>
Total:<?= $session['cart.qty']?>
For the amount:<?= $session['cart.sum']?>
<?php $i = 1; foreach($name as $name): ?>
<?= "Name" ?>
<?= $name ?>
<?php $i++; endforeach; ?>
<?php $i = 1; foreach($email as $email): ?>
<?= "Email" ?>
<?= $email ?>
<?php $i++; endforeach; ?>
<?php $i = 1; foreach($phone as $phone): ?>
<?= "Phone" ?>
<?= $phone ?>
<?php $i++; endforeach; ?>
<?php $i = 1; foreach($address as $address): ?> <?= "Address" ?>
<?= $address ?>
<?php $i++; endforeach; ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question