Error in mail() - Must Issue the STARTTLS command first

Asked

Viewed 2,232 times

1

Well, I researched about the error, many users said that should be added the ssl:// at the smtp.google.com or the tls:// at the imap.google.com, using ports 465(or 993 as an alternative) and 587 respectively. I tested several user settings of the stackoverflow itself, none managed to put an end to this problem.

php.ini

[mail function]
SMTP=imap.gmail.com
smtp_port=587

sendmail_from = [email protected]

mail.add_x_header=On

sendmail.ini

smtp_server=imap.gmail.com

smtp_port=587

smtp_ssl=auto

error_logfile=error.log

[email protected]
auth_password=xxxxxxx

sending script

<?php

$nome = $_POST["nome"];
$assunto = $_POST["assunto"];
$mensagem = $_POST["mensagem"];
$mensagem = wordwrap($mensagem, 70);
$mensagem = str_replace("\n.", "\n..", $mensagem);
$email = $_POST["email"];
$telefone = $_POST["telefone"];
#Montagem do e-mail
$from = $email;
$to = "[email protected]";
$subject = $assunto;
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: {$from}" . "\r\n";
$headers .= "To: {$to}" . "\r\n";
$headers .= "X-Mailer: " . phpversion();
$message = "<p>{$nome}</p><br />
            <p>{$email}</p><br />
            <p>{$telefone}</p><br />
            <br />
            <p>{$assunto}</p><br />
            <p>{$mensagem}</p>";
mail($to, $subject, $message, $headers);

Notice returned:

mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. b62-v6sm6183858qkj.48 - gsmtp in C:\xampp\htdocs\willy\contact_form.php on line 25

How can I fix the problem ?

1 answer

1


This is really wrong:

imap.gmail.com

Imap is to receive and not to send emails, they are completely different protocols, probably the correct one:

SMTP="ssl://smtp.gmail.com";
smtp_port=465

As quoted in this reply: https://stackoverflow.com/a/5265792/1518921 or:

SMTP="tsl://smtp.gmail.com";
smtp_port=587

If SSL doesn’t work (I don’t know how Gmail is)

Note that in the php.ini shall not contain spaces in the values:

sendmail_from = [email protected]

The right thing would be:

[email protected]

Only that it is important to keep in mind that you need to have SSL active in php, via php.ini, then take the ; out of front of the ;

If it’s windows (php to version 7.1):

;extension=php_openssl.dll

Should stay like this:

extension=php_openssl.dll

If it’s linux or mac (php to version 7.1):

;extension=openssl.so

Should stay like this:

extension=openssl.so

If PHP7.2 is windows, linux or mac independent it should look like this:

;extension=openssl

Should stay like this:

extension=openssl

After editing php.ini you need to restart your HTTP server, it’s usually apache.

  • that’s right, it gave error with both, but apparently it’s blocking on the gmail server itself

  • 1

    @Murilo Sure it’s exactly the same message right? Well, I’ll trust :) ... make a test, remove the spaces of sendmail_from = [email protected], thus [email protected].

  • rotated without spaces

  • 1

    @Murilogambôa edited the answer and added this, I will delete all my comments, if you need anything else let me know

Browser other questions tagged

You are not signed in. Login or sign up in order to post.