D
D
Dovitwtu2018-05-29 20:50:20
PHP
Dovitwtu, 2018-05-29 20:50:20

Why are emails not being sent using PHPMailer?

My code:

<?php 

$phone = $_POST['user'];

require_once('phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '[email protected]';                 // Наш логин
$mail->Password = 'xxxxxxx';                           // Наш пароль от ящика
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465;                                    // TCP port to connect to
 
$mail->setFrom('[email protected]', 'Иван Иванов');   // От кого письмо 
$mail->addAddress('[email protected]');     // Add a recipient
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Новая заявка сайта';
$mail->Body    = '
  Пользователь оставил свои данные <br> 
  Имя: неизвестно <br>
  Телефон: ' . $phone . '';
$mail->AltBody = 'Это альтернативный текст';

if(!$mail->send()) {
  echo 'Письмо не может быть отправлено. ';
  echo 'Ошибка: ' . $mail->ErrorInfo;
} else {
  header('location: ../thankyou.html');
}                            
?>

Here is the HTML
<form action="mailer/smart.php" method="POST" class="main-form_post">
<input type="tel" class="popup-form__input" id="phone" name="user" required placeholder="+7 (ХХХ) ХХХ-ХХ-ХХ">

I upload to hosting.
I'm getting an error "Mail could not be sent". Error: SMTP connect() failed.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2018-05-29
@butteff

>SMTP connect() failed
1. Is all data correct? Maybe port 587?
2. Are all php libraries for working with smtp installed?
3. Is there a firewall on the server (iptables\ufw)? If this is an Amazon server, is it allowed to connect to the smtp port in the security groups?

A
alekssamos, 2018-07-01
@alekssamos

Sign-in attempt blocked: untrusted application
Someone wanted to sign into your account through an untrusted application. We blocked this attempt.

Solution: We go here https://myaccount.google.com/security?hl=ru&pli=1
and turn on:
Untrusted applications are allowed
That's all. We save and everything should be sent.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question