Mail() function does not send email

Asked

Viewed 5,103 times

0

I just downloaded a site I’m working on to a remote server and I just noticed that mail doesn’t reach my gmail inbox. I used a very simple method of code but it doesn’t work, although it says ""Your email was sent successfully! ":

    <?php
header('Content-type: text/html; charset=UTF-8');
$name = $_POST['name'];
$email = $_POST['email'];
$carga_type = $_POST['carga_type'];
$weight = $_POST['weight'];
$local = $_POST['local'];
$destiny = $_POST['destiny'];
$date = $_POST['date'];
$company = $_POST['company'];
$tele = $_POST['tele'];
$vol_number = $_POST['vol_number'];
$volume = $_POST['volume'];
$size = $_POST['size'];
$notes = $_POST['notes'];

$to = "[email protected]";
$subject = "Nova messagem";
$message = "De: ".$name."<br>
            Email: ".$email."<br>
            Tipo de carga: ".$carga_type."<br>
            Peso: ".$weight."<br>
            Local: ".$local."<br>
            Destino: ".$destiny."<br>
            Data: ".$date."<br>
            empresa: ".$company."<br>
            Telefone: ".$tele."<br>
            Número de volumes: ".$vol_number."<br>
            Volume: ".$volume."<br>
            Medidas C x L x A: ".$size."<br>
            Notas: ".$notes."<br><br>";

echo ($message);
if (($name == "") || ($carga_type == "") || ($date == "") || ($local == "") || ($tele == "") || ($vol_number==""))
{
echo 'Preencha todos os os campos mínimos necessários (Nome, Tipo de carga, Data, Local e Numero de telefone.)';
}
else
{
    mail($to, $subject, $message);
    echo "O seu email foi enviado com sucesso! ";
}
?>
  • you set the from? see if return tbm mail() is false by adding an if.

  • I don’t think I need to be precise because only these three fields are necessary (to, Subject, message) http://www.w3schools.com/php/php_mail.asp

  • do the following test, add these 2 lines at the beginning of your page: ini_set('display_errors', true); error_reporting(E_ALL); and the mail line for: if(!mail($to, $subject, $message)){&#xA; echo error_get_last();&#xA;}

  • You have configured the remote SMTP server in PHP ? http://www.php.net/manualen/mail.configuration.php

  • @Lost Thanks but there is no error, everything runs normally ("Your email has been sent successfully! ")

  • How to configure this SMTP server on remote?

  • @Miguel usually leaves the mailing mechanism on the machine itself. What is the operating system and/or hosting?

Show 2 more comments

5 answers

2

Try this (keeping in mind that the SMTP service is working correctly on the server):

    $name = $_POST['name'];
    $email = $_POST['email'];
    $carga_type = $_POST['carga_type'];
    $weight = $_POST['weight'];
    $local = $_POST['local'];
    $destiny = $_POST['destiny'];
    $date = $_POST['date'];
    $company = $_POST['company'];
    $tele = $_POST['tele'];
    $vol_number = $_POST['vol_number'];
    $volume = $_POST['volume'];
    $size = $_POST['size'];
    $notes = $_POST['notes'];

    $to = "[email protected]";
    $subject = "Nova messagem";
    $message = "De: ".$name."<br>
                Email: ".$email."<br>
                Tipo de carga: ".$carga_type."<br>
                Peso: ".$weight."<br>
                Local: ".$local."<br>
                Destino: ".$destiny."<br>
                Data: ".$date."<br>
                empresa: ".$company."<br>
                Telefone: ".$tele."<br>
                Número de volumes: ".$vol_number."<br>
                Volume: ".$volume."<br>
                Medidas C x L x A: ".$size."<br>
                Notas: ".$notes."<br><br>";


            $headers = "From: $from \r\n".

                       "MIME-Version: 1.0" . "\r\n" .

                       "Content-type: text/plain; charset=UTF-8" . "\r\n"; 

            if(mail($to,$subject,$message,$headers)){
echo "email enviado com sucesso";
}

1

see if it is not postfix (not sendmail), if you just add ('-r ' . $Sender) at the end of the mail function

ex:

 if( mail($to,$subject,$message,$headers, '-r'.$sender) ){
  echo "enviado";
 }

where $Sender must be the email that is sending the message, it is an additional setup besides the headers needed by postfix.

1

This has happened to me before and I added a header and it worked in my case. Take an example:

$headers = 'From: Titulo da aplicacao <[email protected]>'."\r\n" .
        'Reply-To: [email protected] '. "\r\n" .
        'X-Mailer: MyFunction/' . phpversion().
        'MIME-Version: 1.0' . "\n".
        'Content-type: text/html; charset=UTF-8' . "\r\n";

mail($email,$titulo,$HTML,$headers);

-1

Some hosting servers require authentication to send emails programmatically. And no error is being displayed, because the way you wrote your code, regardless of the result of the mail() function, you give an echo with the phrase "Your email was sent successfully! ". Consider using the Phpmailer class, it greatly facilitates these email sending settings, including with authenticated smtp.

  • 2

    The initial part of your statement is incorrect. The mail function does not require authentication to send emails, it only requires the SMTP server to work correctly.

  • 2

    The middle part too, because echo is completely bored.

-2

I posted the script in a file . rar on Google Drive for you to download.

I always use on my websites this script that has validation and check server output (SMTP) to prevent your email from failing or falling into the spam box. Mostly works smoothly, just configure the data in the file email.php don’t forget this!

Don’t forget to send the folder together PHPMAILER to the server.

DOWNLOAD HERE

*To download, just go to the File tab >> Download or CTRL+S

Browser other questions tagged

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