Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send email phpmailer and javax.mail

I have two similar part of code, written on java and php. PHP did not send email because of certificate error -

  Connection failed. Error #2: stream_socket_enable_crypto():
  Peer certificate CN=*.hosting.com' did not match
  expected CN=smtp.anotherhosting.com'

But java code send emails without any problems, and I can't understand why. ( from everywhere I see questions - how to skip ssl checks with java?

Here is code:

php:

<?php
    require './PHPMailer.php';
    require './SMTP.php';

    use PHPMailer\PHPMailer\PHPMailer;

    $mail = new PHPMailer(true);
    try {
        $mail->SMTPDebug = 4;
        $mail->isSMTP();
        $mail->Host = 'smtp.anotherhosting.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail->Password = 'password';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        //Recipients
        $mail->setFrom('[email protected]');
        $mail->addAddress('[email protected]');
        $mail->isHTML(true);
        $mail->Subject = 'Here is the subject12';
        $mail->Body    = 'This is the HTML message bo22dy <b>in bold!</b>';

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    }
    try {
        $mail->smtpClose();
    } catch (Exception $e) {
        echo $e->getTraceAsString();
    }

and java:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        final String username = "[email protected]";
        final String password = "password";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.anotherhosting.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        session.setDebug(true);
        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("[email protected]"));
            message.setSubject("Testing Subject");
            message.setText("Dear Mail Crawler,"
                    + "\n\n No spam to my email, please!");

            Transport.send(message);

            System.out.println("Done");

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

My task is implement email send functionality with php code. From my current perspective, it fail because of smtp redirect from one host to another host. Most probably, phpmailer got host1, receive redirect to host2, take certificate from host2 and compare this certificate with host1. In same time, java client do everything fine. If anybody know how to solve such problem please let me know.

Also, php code fail on line 402 when it try to call stream_socket_enable_crypto.

Here is log files: java:

DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.anotherhosting.com", port 587, isSSL false
220 mailpod.hosting.com ESMTP
DEBUG SMTP: connected to host "smtp.anotherhosting.com", port: 587

EHLO degr [most probably my computer name]
250-mailpod.hosting.com
250-STARTTLS
250-PIPELINING
250-8BITMIME
250-SIZE 65000000
250 AUTH LOGIN PLAIN CRAM-MD5
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "65000000"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN CRAM-MD5"
STARTTLS
220 ready for tls
EHLO degr
250-mailpod.hosting.com
250-PIPELINING
250-8BITMIME
250-SIZE 65000000
250 AUTH LOGIN PLAIN CRAM-MD5
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "SIZE", arg "65000000"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN CRAM-MD5"
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 [auth hash here]
[auth hash here]
334 [auth hash here]
[auth hash here]
235 ok, go ahead (#2.0.0)
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 ok
RCPT TO:<[email protected]>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 go ahead
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Testing Subject
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Dear Mail Crawler,

 No spam to my email, please!
.
250 ok 1537892155 qp 173024
QUIT
221 mailpod.hosting.com
Done

php:

2018-09-25 16:26:35 Connection: opening to smtp.anotherhosting.com:587, timeout=300, options=array()
2018-09-25 16:26:35 Connection: opened
2018-09-25 16:26:35 SMTP INBOUND: "220 mailpod.hosting.com ESMTP"
2018-09-25 16:26:35 SERVER -> CLIENT: 220 mailpod.hosting.com ESMTP
2018-09-25 16:26:37 CLIENT -> SERVER: EHLO localhost
2018-09-25 16:26:38 SMTP INBOUND: "250-mailpod.hosting.com"
2018-09-25 16:26:38 SMTP INBOUND: "250-STARTTLS"
2018-09-25 16:26:38 SMTP INBOUND: "250-PIPELINING"
2018-09-25 16:26:38 SMTP INBOUND: "250-8BITMIME"
2018-09-25 16:26:38 SMTP INBOUND: "250-SIZE 65000000"
2018-09-25 16:26:38 SMTP INBOUND: "250 AUTH LOGIN PLAIN CRAM-MD5"
2018-09-25 16:26:38 SERVER -> CLIENT: 250-mailpod.hosting.com250-STARTTLS250-PIPELINING250-8BITMIME250-SIZE 65000000250 AUTH LOGIN PLAIN CRAM-MD5
2018-09-25 16:26:38 CLIENT -> SERVER: STARTTLS
2018-09-25 16:26:38 SMTP INBOUND: "220 ready for tls"
2018-09-25 16:26:38 SERVER -> CLIENT: 220 ready for tls
2018-09-25 16:26:38 Connection failed. Error #2: stream_socket_enable_crypto(): Peer certificate CN=*.hosting.com' did not match expected CN=smtp.anotherhosting.com' [C:\project\SMTP.php line 402]
SMTP Error: Could not connect to SMTP host.
2018-09-25 16:26:39 CLIENT -> SERVER: QUIT
2018-09-25 16:26:39 
2018-09-25 16:26:39 
2018-09-25 16:26:39 
2018-09-25 16:26:39 Connection: closed
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

PS smpt service provided by https://www.networksolutions.com/

like image 222
degr Avatar asked Sep 25 '18 16:09

degr


People also ask

Does PHPMailer use SMTP?

PHPMailer can use a non-local mail server (SMTP) if you have authentication. Further advantages include: It can print various kinds of error messages in more than 40 languages when it fails to send an email. It has integrated SMTP protocol support and authentication over SSL and TLS.

What is PHPMailer used for?

PHPMailer is a code library and used to send emails safely and easily via PHP code from a web server. Sending emails directly via PHP code requires a high-level familiarity to SMTP standard protocol and related issues and vulnerabilities about Email injection for spamming.

How many emails can I send with PHPMailer?

PHPMailer does not set any limits, nor does the mail() function, it's only ISPs like GoDaddy that do. However, they do so by blocking access to normal SMTP ports, and/or redirecting SMTP connections to their own servers ( *. secureserver. * ).

Is it safe to use PHPMailer?

PHPMailer doesn't create/use any SQL itself, nor does it have anything to do with javascript, so it's secure on those fronts. It is often used alongside code that does both, but that's not PHPMailer's concern. It applies filtering to headers in order to avoid header injection attacks, and (as far as I'm aware!)


1 Answers

Right. So your Java code has a bug that allows man-in-the-middle attacks to succeed. In PHP, it's successfully preventing that, by doing exactly what TLS is designed to do.

What's happening is TCP redirection at your ISP's firewall, which is invisible to both clients. You can disable the certificate checks (as described in the troubleshooting guide), but you really shouldn't. Either connect explicitly to the correct name (mailpod.hosting.com), or use a hosting provider that doesn't tamper with your traffic.

like image 187
Synchro Avatar answered Oct 06 '22 08:10

Synchro