Mailing with PHPMailer

0

I have the following code in Java to send an email

   props.setProperty("mail.smtp.host", host);
   props.setProperty("mail.smtp.starttls.enable", "false");
   props.setProperty("mail.smtp.port", port);
   props.setProperty("mail.smtp.user", user);
   props.setProperty("mail.smtp.auth", "true");
   htmlBody = Disenio(tercero, titulo, mensaje);
   Session session = Session.getDefaultInstance(props);
   MimeMessage message = new MimeMessage(session);
   message.setFrom(new InternetAddress(user));
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailDestino));
   message.setSubject(titulo); //adjuntar asunto...
   message.setContent( htmlBody, "text/html");
   Transport t = session.getTransport("smtp");
   t.connect(user, pass);
   t.sendMessage(message, message.getAllRecipients());
   t.close();

I have the following code in php to send the mail

        $mail=new PHPMailer();
        $mail->isSMTP();
        $mail->SMTPOptions  = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );            
        $mail->SMTPAuth=true;
        $mail->Host=$host;
        $mail->Username=$user;
        $mail->Password=$pass;
        $mail->SMTPSecure= 'tls';                            
        $mail->Port= 25; 

        $mail->AddAddress($mailDestino);
        $mail->Subject = $titulo;
        $mail->isHTML(true);
        $mail->Body=$this->Disenio($tercero,$titulo,$mensaje);
        $mail->AltBody=$mensaje;
        $mail->setLanguage('es');
        if($mail->send()){
            $result="OK";
        }else{
            $result="Mail Error".$mail->ErrorInfo;
        }

I use the same host, user and password in both, in Java I WORK but in Php NO, I get the following error:

  

Mail ErrorError SMTP: The following destinations failed: [email protected]: Verification failed for   Unrouteable address   Sender verify failed

Reading on the internet I read that the error is due to a problem with the mail server because of some configuration or something, but if Java was not so, it should not work for me, that's why I wanted to place the comparison of the two languages, which are two different languages but I do not think it's because of the mail server because in java it works perfectly for me.

    
asked by Lemi 03.03.2017 в 21:28
source

1 answer

0

First, what @juanpinzon says is true, you should use port 587. But suppose you hit the correct port.

In your java code you are not checking for a successful mailing of your mail. You simply execute the shipment and close. The equivalent in PHP, instead of

if($mail->send()){
    $result="OK";
}else{
    $result="Mail Error".$mail->ErrorInfo;
}

It would simply be done

$mail->send();

Secondly, you say that java works but did you verify that the recipient received the mail?

What I see is that the error in your question is equivalent to a header 550. And that error occurs not because of the client (you) but because of the server (them), for two possible reasons: Email address does not exist, or the server is doing Sender Verify.

In the second case, it means that if your mail provider is not on your whitelist, it will not let the message in. That you can not fix on your side more than going through an official SMTP like Amazon SES, SendGrid, Mailchimp, etc.

There is a thread with a similar case in StackOverflow (not this one the one on the side) but it does not really offer solutions other than turning on the debug mode.

    
answered by 03.03.2017 / 23:06
source