Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPMailer uses local Sendmail to send email despite is set other way. #2914

Closed
ZaltorSupport opened this issue Jun 13, 2023 · 6 comments
Closed

Comments

@ZaltorSupport
Copy link

ZaltorSupport commented Jun 13, 2023

Hello

I've got an issue with PHPMailer.

I've made that code

<?php
ini_set("display_errors", "1");
ini_set("display_startup_errors", "1");
error_reporting(E_ALL);

set_include_path(get_include_path() . PATH_SEPARATOR ."/usr/share/vendor"); //That one added is where PHPMailer is

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

require 'phpmailer/phpmailer/src/Exception.php';
require 'phpmailer/phpmailer/src/PHPMailer.php';
require 'phpmailer/phpmailer/src/SMTP.php';
require 'autoload.php';

$mail = new PHPMailer(true); // Passing `true` enables exceptions

date_default_timezone_set('Europe/Madrid');

try {
        $mail->SMTPOptions = array(
                'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
                )
        );
    $mail->SMTPDebug = 4; // Enable verbose debug output
    $mail->isSMTP(); // Set mailer to use SMTP
    $mail->host = "mx10.zaltor.es"; //that solves to 5.200.27.68
    $mail->Username = '[email protected]';  // Nombre de usuario SMTP
    $mail->Password = 'not_that_pass';  // Contraseña SMTP
   // Configuración del remitente y destinatario
    $mail->setFrom('[email protected]', 'Info Zaltor');  // Remitente
    $mail->addAddress($_POST['des_email'], $_POST['des_usuario']);  // Destinatario
    $subject = "Informe de fichaje de salida";
     $body = '<p>Hola, tu fichaje de salida ha sido en el momento '.$hora.' y tu motivo para salir es <b>'.$_POST['des_motivo'].'</b></p>';
    $text = 'Hola, tu fichaje de salida ha sido en el momento '.$hora.' y tu motivo para salir es '.$_POST['des_motivo'];

// Contenido del correo
    $mail->isHTML(true);  // Habilitar contenido HTML
    $mail->Subject = $subject;
    $mail->Body    = $body;
    $mail->AltBody = $text;
    $mail->send();
} catch (Exception $e) {
    echo 'Error al enviar el correo: ' . $mail->ErrorInfo;
}

?>

That should send the mail using that email server, but as I have the debug activated:

2023-06-13 16:01:58 Connection: opening to localhost:25, timeout=300, options=array ( 'ssl' => array ( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ),)
2023-06-13 16:01:58 Connection: opened
2023-06-13 16:01:58 SMTP INBOUND: "220 fichar ESMTP Sendmail 8.15.2/8.15.2; Tue, 13 Jun 2023 18:01:58 +0200"
2023-06-13 16:01:58 SERVER -> CLIENT: 220 fichar ESMTP Sendmail 8.15.2/8.15.2; Tue, 13 Jun 2023 18:01:58 +0200
2023-06-13 16:01:58 CLIENT -> SERVER: EHLO 192.168.75.75
2023-06-13 16:01:58 SMTP INBOUND: "250-fichar Hello localhost [127.0.0.1], pleased to meet you"
2023-06-13 16:01:58 SMTP INBOUND: "250-ENHANCEDSTATUSCODES"
2023-06-13 16:01:58 SMTP INBOUND: "250-PIPELINING"
2023-06-13 16:01:58 SMTP INBOUND: "250-8BITMIME"
2023-06-13 16:01:58 SMTP INBOUND: "250-SIZE"
2023-06-13 16:01:58 SMTP INBOUND: "250-DSN"
2023-06-13 16:01:58 SMTP INBOUND: "250-ETRN"
2023-06-13 16:01:58 SMTP INBOUND: "250-AUTH GSSAPI"
2023-06-13 16:01:58 SMTP INBOUND: "250-STARTTLS"
2023-06-13 16:01:58 SMTP INBOUND: "250-DELIVERBY"
2023-06-13 16:01:58 SMTP INBOUND: "250 HELP"

I don't see any fault in my code. You can see is connecting to the local Sendmail, instead of the Email Server that is on mx10.zaltor.es

What could be happening...

When I go to the SSH console

[root@fichar vendor]# pwd
/usr/share/vendor
[root@fichar vendor]# cd phpmailer/
[root@fichar phpmailer]# cd phpmailer/
[root@fichar phpmailer]# ll
total 80
-rw-r--r-- 1 root root  2092 mar  6 15:43 COMMITMENT
-rw-r--r-- 1 root root  2675 mar  6 15:43 composer.json
-rw-r--r-- 1 root root  6226 mar  6 15:43 get_oauth_token.php
drwxr-xr-x 2 root root  4096 mar  6 15:43 language
-rw-r--r-- 1 root root 26529 mar  6 15:43 LICENSE
-rw-r--r-- 1 root root 16429 mar  6 15:43 README.md
-rw-r--r-- 1 root root  7584 mar  6 15:43 SECURITY.md
drwxr-xr-x 2 root root   154 mar  6 15:43 src
-rw-r--r-- 1 root root     5 mar  6 15:43 VERSION
[root@fichar phpmailer]# cd src
[root@fichar src]# ll
total 256
-rw-r--r-- 1 root root   6919 mar  6 15:43 DSNConfigurator.php
-rw-r--r-- 1 root root   1240 mar  6 15:43 Exception.php
-rw-r--r-- 1 root root   3774 mar  6 15:43 OAuth.php
-rw-r--r-- 1 root root   1522 mar  6 15:43 OAuthTokenProvider.php
-rw-r--r-- 1 root root 179441 mar  6 15:43 PHPMailer.php
-rw-r--r-- 1 root root  12259 mar  6 15:43 POP3.php
-rw-r--r-- 1 root root  47594 mar  6 15:43 SMTP.php
[root@fichar src]# php -v
PHP 7.4.33 (cli) (built: Oct 31 2022 10:36:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
[root@fichar src]#

@Synchro
Copy link
Member

Synchro commented Jun 13, 2023

PHP is case-sensitive for property names and variables, so change this:

$mail->host = "mx10.zaltor.es"

to

$mail->Host = "mx10.zaltor.es"

Separately, disabling certificate verification is a really bad idea. If it's causing problems, fix them properly instead of hiding the evidence; there's plenty of info in the troubleshooting guide about that.

@Synchro Synchro closed this as completed Jun 13, 2023
@XL-2000
Copy link

XL-2000 commented Jun 13, 2023

First of all, the reason for this happening is that to set the Host, you need to set the Host attribute with a capital H.
So; $mail->host = "mx10.zaltor.es"; shoud be $mail->Host = "mx10.zaltor.es";
Since you did not supply any Host, you default to localhost, which is exactly what happens in your case.

@XL-2000
Copy link

XL-2000 commented Jun 13, 2023

Next, please take note;
Providing ssl like

'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true

is bad! At least remove this in production.

Check if you need settings like "SMTP authentication" to true? Like;
$mail->SMTPAuth = true
Check if you need settings like SMTP encryption? Like;
$mail->SMTPSecure = 'tls';

@XL-2000
Copy link

XL-2000 commented Jun 13, 2023

PHP is case-sensitive for property names and variables, so change this:

$mail->host = "mx10.zaltor.es"

to

$mail->Host = "mx10.zaltor.es"

Separately, disabling certificate verification is a really bad idea. If it's causing problems, fix them properly instead of hiding the evidence; there's plenty of info in the troubleshooting guide about that.

What he said ;) He beat me to it ;)

@Synchro
Copy link
Member

Synchro commented Jun 13, 2023

@XL-2000 I'm starting to feel like we might be the same person... 😛

@ZaltorSupport
Copy link
Author

Hello

Thank you to all of you.

Seems it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants