0

When I include test.com in the subject of an email the email fails to send. I am using phpmailer and office365

This fails:

<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;


    require '../../src/Exception.php';
    require '../../src/PHPMailer.php';
    require '../../src/SMTP.php';
    //production Email
    $mail = new PHPMailer;
    $mail->SMTPDebug = 3; 
    $mail->Host = "smtp.office365.com";
    $mail->Port = 587; // TLS only
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;  
    $mail->SMTPAuth = true;
    $mail->Username = "email@domain.com";
    $mail->Password = "*********";
    $mail->setFrom('email@domain.com', 'reports');
    $mail->addAddress('email@domain.com', 'Name');

    $mail->Subject = "TEST.com";
    $mail->IsHTML(true); 
    $body = "This is the body.";
    
    $mail->Body="$body"; //$mail->msgHTML(file_get_contents('contents.html'), __DIR__);         //Read an HTML message body from an external file, convert referenced images to embedded,
    //$mail->AltBody = 'HTML messaging not supported';
    // $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file
    if(!$mail->send()){
        $display = "Mailer Error: " . $mail->ErrorInfo;
    }else{
        $display = "Thank you for your submission! ";
    }
 ?>

This works: (the only difference is the a space between Test and .com)

<?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;


    require '../../src/Exception.php';
    require '../../src/PHPMailer.php';
    require '../../src/SMTP.php';
    //production Email
    $mail = new PHPMailer;
    $mail->SMTPDebug = 3; 
    $mail->Host = "smtp.office365.com";
    $mail->Port = 587; // TLS only
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;  
    $mail->SMTPAuth = true;
    $mail->Username = "email@domain.com";
    $mail->Password = "*********";
    $mail->setFrom('email@domain.com', 'reports');
    $mail->addAddress('email@domain.com', 'Name');

    $mail->Subject = "TEST .com";
    $mail->IsHTML(true); 
    $body = "This is the body.";
    
    $mail->Body="$body"; //$mail->msgHTML(file_get_contents('contents.html'), __DIR__);         //Read an HTML message body from an external file, convert referenced images to embedded,
    //$mail->AltBody = 'HTML messaging not supported';
    // $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file
    if(!$mail->send()){
        $display = "Mailer Error: " . $mail->ErrorInfo;
    }else{
        $display = "Thank you for your submission! ";
    }
 ?>

I have no idea after 8 hours of trying and retrying. I looked at Mail flow on EAC and there was nothing about the emails.

I tried everything from Phpmailer error "Could not instantiate mail function" but it gets me this error (this includes adding $mail->isSMTP()):

2023-07-18 15:51:50 Connection: opening to smtp.office365.com:587, timeout=300, options=array()
2023-07-18 15:51:58 Connection failed. Error #2: stream_socket_client(): unable to connect to smtp.office365.com:587 (Connection refused) [/home/hmktgroup/public_html/src/SMTP.php line 388]
2023-07-18 15:51:58 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
  • https://stackoverflow.com/questions/1297084/phpmailer-error-could-not-instantiate-mail-function might help? – aynber Jul 18 '23 at 15:38
  • Your examples look identical to me? – ADyson Jul 18 '23 at 15:42
  • @ADyson "This works: (the only difference is the a space between Test and .com)" – Justin Phenix Jul 18 '23 at 15:52
  • Yes, you already said that. But in both your samples, there is no space between Test and .com . So it's unclear what difference we are supposed to be observing. – ADyson Jul 18 '23 at 15:55
  • `unable to connect to smtp.office365.com:587 (Connection refused)` ...hmm. You sure you don't have any outbound firewall rules preventing use of port 587 for outgoing traffic? Can you `telnet smtp.office365.com 587` from a command prompt on the same machine where the PHP code is running? If that connects, then there's no firewall issue. – ADyson Jul 18 '23 at 16:02
  • I want to ensure I understand the question. You get "smtp.office365.com:587 (Connection refused)" when you do `$mail->Subject = "TEST.com"` but it works fine when you add a space between the domain components, being that the only difference? If that's the way it is, it totally looks like an overzealous security tool (antivirus, firewall, whatever). – Álvaro González Jul 18 '23 at 16:07
  • @ÁlvaroGonzález I think you are correct. I have eliminated office365 by using another STMP and my personal email. I am thinking it might be a go-daddy firewall issue. – Justin Phenix Jul 18 '23 at 16:25
  • You can't think Alvaro is correct, _and_ think it's a firewall issue. Those are two entirely different issues. – ADyson Jul 18 '23 at 16:42
  • @ADyson How so? Alvaro said "overzealous security tool (antivirus, firewall, whatever)." – Justin Phenix Jul 18 '23 at 16:54
  • @JustinPhenix Apologies I missed the "firewall" bit of the remark, my bad. And I was thinking of something filtering on the O365 end of the process, rather than your local machine. You're right, those are not opposing possibilities. – ADyson Jul 19 '23 at 07:28

1 Answers1

1

You've gone to great effort to set SMTP properties, but have not actually told PHPMailer to use SMTP, so it's ignoring all those settings and falling back to mail() delivery through your local mail server, which doesn't appear to exist or is broken in some way. Presumably this is not what you intended. Try adding this:

$mail->isSMTP();

Comments following your question suggest that you might have added this, but now you're getting connection refused errors, which most likely suggests that your hosting provider is blocking outbound SMTP, in which case you need to read the troubleshooting guide on that subject.

Synchro
  • 35,538
  • 15
  • 81
  • 104