SMTP Error: The following recipients failed hotmail for companydomain

1

I have a form in html that the user will enter the name, email, subject and message, considering that the user can enter email from hotmail, gmail, etc ..., and when I confirm I am using the php code below to try send the message to my email, using PHP Mailer , but the following error occurs:

  

SMTP Error: The following recipients failed

I noticed that this error occurs only when $mail->From is hotmail . If I put the same email as the $mail->Username , the error does not occur and usually email .

I've researched and tried practically all the alternatives that indicate on the internet but none worked, could anyone give me an idea of where the problem might be? maybe I'm not on the right track and would like to know if it's possible to send an email from any domain to my company email?

<?php 
    require('phpmailer/PHPMailerAutoload.php');
    require('phpmailer/class.phpmailer.php');
    require('phpmailer/class.smtp.php');
    require('phpmailer/class.pop3.php');

    $to = "[email protected]"; // this is your Email address       
    $name = $_POST['nmContato'];    
    $from = $_POST['email']; // this is the sender's Email address
    $subject = $_POST['assunto'];
    $message = $name . " enviou a mensagem:" . "\n\n" . $_POST['txt_contato'];  

    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->CharSet="UTF-8";
    $mail->Debugoutput = 'html';
    $mail->Host = 'email-ssl.com.br';
    $mail->Port = 465;  
    $mail->SMTPSecure = 'ssl';
    $mail->SMTPAuth = true;
    $mail->IsHTML(true);
    $mail->Username = '[email protected]';
    $mail->Password = 'senhadoemail';
    $mail->From = $from; //o erro ocorre quando esse valor é [email protected]
    $mail->FromName = $name;    
    $mail->AddAddress($to);
    $mail->Subject = $subject;
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
    $mail->Body = $message;
    $mail->SMTPDebug = 1;   
    if(!$mail->Send()){
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }else{
        echo 'Message has been sent';
    }
?> 

            <form class="form-horizontal" action="email.php" method="post" id="formContato">
                    <div class="form-group form-group-lg">
                      <label class="col-sm-3 control-label" for="nmContato">Nome</label>
                      <div class="col-sm-8" id="div_nmContato">
                        <input class="form-control" type="text" name="nmContato" id="nmContato">

                      </div>
                    </div>
                    <div class="form-group form-group-lg">
                      <label class="col-sm-3 control-label" for="email">Email</label>
                      <div class="col-sm-8" id="div_email">
                        <input class="form-control" type="text" name="email" id="email">
                      </div>
                    </div>
                    <div class="form-group form-group-lg">
                      <label class="col-sm-3 control-label" for="assunto">Assunto</label>
                      <div class="col-sm-8" id="div_assunto">
                        <input class="form-control" type="text" name="assunto" id="assunto">
                      </div>
                    </div>                          
                    <div class="form-group form-group-lg">
                      <label class="col-sm-3 control-label" for="txt_contato">Mensagem</label>
                      <div class="col-sm-8" id="div_mensagem">
                        <textarea id="txt_contato" name="txt_contato" class="form-control" rows="5"></textarea>
                      </div>
                    </div>  

                  <button id="btnContato" type="submit" name="submit" class="btn btn-primary" style="margin-left:149px;">Enviar</button>                
                  <button type="button" class="btn btn-primary" id="btn_limpa_contato">Limpar</button>                                                  
            </form> 

    
asked by anonymous 07.07.2017 / 19:14

1 answer

2

Many hosts block "make-up" the sender, this to prevent SPAMMERS from using this practice, then it will not be able to connect to a specific SMTP and change the to: , the server will "deny "

By the settings I assume it is locaweb, so I remember they also have this restricted policy to avoid SPAMs:

$mail->Host = 'email-ssl.com.br';
$mail->Port = 465;  
$mail->SMTPSecure = 'ssl';

It does not mean that you are a SPAMMER, it just means that spammers make use of it and so it is better to block them all.

What you can do is use Reply-To: instead of To:

$name = $_POST['nmContato'];    
$from = $_POST['email'];

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->Debugoutput = 'html';
$mail->Host = 'email-ssl.com.br';
$mail->Port = 465;  
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->Username = '[email protected]';
$mail->Password = 'senhadoemail';

$mail->AddReplyTo($from, $name); //AQUI vai o reply-to:

$mail->AddAddress($to);
$mail->Subject = $subject;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = $message;
$mail->SMTPDebug = 1;

Extra note

When sending to the production server remove this line $mail->SMTPDebug = 1; , the data that is displayed may cause some error.

    
07.07.2017 / 20:22