0

When I get the value of the email field that the user typed in, the email doesn't arrive. There's not even an error when sending, but I don't receive the e-mail. I can only receive email when I add an email address with the website address, eg 'greenervasul@greenervasul.com.br'.

I've already researched, tried a few things and haven't got anything so far. Does anyone know why?

<?php
$url = $_SERVER['HTTP_REFERER'];
$url = strtok($url, '?');

$name = $_POST['name'];
$tel = $_POST['phone'];
$company = $_POST['company'];
$from = $_POST['email'];
$message = $_POST['message'];


require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

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


$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host       = 'smtp.kinghost.net';  // Specify main and backup SMTP servers
$mail->SMTPAuth   = true;                               // Enable SMTP authentication
$mail->Username   = 'nelson@greenervasul.com.br';
$mail->Password   = '@@@@@@@@@@';
$mail->SMTPSecure = 'startls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port       = '587';                                    // TCP port to connect to

$mail->setFrom($from, $name);
$mail->addAddress('gabrielruiztq@gmail.com');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Lojista';
$mail->Body    = 'Nome: '.$name. '<br>Telefone: '.$tel. '<br>Empresa: '.$company. '<br>Menagem: '.$message;
$mail->AltBody = $message;

if(!$mail->send()) {
    header("Location: $url?send=error");
} else {
    header("Location: $url?send=success");
}
                    
Bjorn Ruiz
  • 546
  • 2
  • 4
  • 15
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – CBroe Nov 08 '21 at 09:42
  • Go check the section titled **Don't use a faux `From:` sender** in the mentioned duplicate. – CBroe Nov 08 '21 at 09:43
  • 1
    Just sending an email to a GMail address will not work. It probably won't even make it to the spam folder, because Google blocks it. Have a look at "SPF records" and "DKIM". – KIKO Software Nov 08 '21 at 09:44
  • also, you have `startls` but is it should be spelled as `starttls`, have you tried SMTPSecure with tls + 587 port or ssl + 465 port? – Utmost Creator Nov 10 '21 at 02:48

1 Answers1

0

Several issues:

require 'phpmailer/PHPMailerAutoload.php';

This means you're running a very old version of PHPMailer. Get the latest version.

$mail->SMTPSecure = 'startls';

The permitted values for this property are ssl or tls.

$mail->Port       = '587';

This is an integer, not a string. The current recommendation is to use SMTPSecure = 'ssl' with Port = 465.

mail->setFrom($from, $name);

You're using the submitter's email address as the from address. This is forgery and means that your messages will usually be rejected outright or put in spam folders. Take a look at the contact form example provided with PHPMailer for how to do this safely.

This may be obvious, but enabling debug output will prevent your redirects from working, so be sure to disable that before using it in production.

Synchro
  • 35,538
  • 15
  • 81
  • 104