1 year ago
#69131
XAZG Неизвестный
Laravel 8 and PHPMiler
I want to send forms via Phpmailer to Laravel, taking them from my modal window that appears on js.The data from the form should always come to the same email
How I did:
-installed phpmailer via composer (composer require phpmailer/phpmailer)
-Created a controller (php artisan make:controller ContactController)
-Created a router on the web.php
-In env registered his mail password (application password) and mail login
What is the error , what actions have I forgotten or done incorrectly
My code in Laravel
HTML My modalWindow
Title @csrf input type="text" class="name" id="name" name="name" placeholder="Имя" required> <div class="mb-3">
<label for="name" class="form-label"></label>
<input type="text" class="name" id="phone" name="phone" placeholder="+1(***)-***-**-**" required>
</div>
<div class="mb-3">
<button class="btn-modal" type ="submit" name="submit" value="submit">Send</button>
</div>
</form>
</div>
<div class ="modalEl">
<button class="btn-modal" id="close-modal">Close</button>
</div>
web.php
use App\Http\Controllers\ContactController;
Route::post('/send',[ContactController::class,'send'])->name('email.send');
Controller
use Illuminate\Http\Request;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
class ContactController extends Controller
{
public function send(Request $request) {
$name = $request->name;
$phone = $request->phone;
$mail = new PHPMailer(true);
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = env('EMAIL_HOST');
$mail->SMTPAuth = 'true';
$mail->Username = env('EMAIL_USERNAME');
$mail->Password = env('email_password');
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->setFrom('','$name');
$mail->addAddress('my@email.com');
$mail->isHTML(true);
$mail->Subject = '';
$mail-> Body = $phone;
$result = $mail->send();
if($result) {
echo 'sucessful';
}else{
echo'error';
}
my ENV
EMAIL_HOST= smtp.yandex.ru
EMAIL_PORT=465
EMAIL_USERNAME=myemail@yandex.ru
EMAIL_PASSWORD= myPassword
( email password for applications)
php
laravel
phpmailer
send
0 Answers
Your Answer