Site.eu

Email questions

Problem with sending emails with PHP

Welcome to the Site.eu Customer Forum!
Proving CLU
3 Aug 2024, 19:15

Problem with sending emails with PHP

I have a website that uses the native PHP mail() function to send emails and it doesn't work for me, since I've had problems with this function on other occasions I've changed all the code to use the PHPmailer library but it doesn't work for me either, the process completes successfully and doesn't give me any kind of error, in fact in my website's error control I try to catch if it gives any error but it doesn't give any, but it never reaches the destination, I've tested with many email accounts but it never arrives. This functionality is essential for any hosting because for any website that has user access it is necessary for example to change the password

Automatically translated
Site.es
3 Aug 2024, 20:44

Good evening.

To send emails through your website's PHP script, you need to send them via SMTP. Otherwise, they will not pass our spam filters.

If you are using WordPress, there are many plugins you can use to send emails via SMTP.

Otherwise, you need to use PHPMailer.

If you are not seeing errors, simply search for 'logs' in DirectAdmin and you will find a page where you can view all the error logs for your scripts. This can be very useful for debugging.

```

<?php

use PHPMailer\PHPMailer\PHPMailer;

use PHPMailer\PHPMailer\Exception;

// Cargar el autoloader de Composer

require 'vendor/autoload.php';

// Crear una instancia de PHPMailer

$mail = new PHPMailer(true);

try {

// Configuración del servidor

$mail->isSMTP();

$mail->Host = 'smtp.example.com'; // Configurar el servidor SMTP para enviar a través de él

$mail->SMTPAuth = true;

$mail->Username = 'tu-correo@example.com'; // Nombre de usuario SMTP

$mail->Password = 'tu-contraseña-de-correo'; // Contraseña SMTP

$mail->SMTPSecure = PHPMailer::ENCRYPTIONSTARTTLS; // Habilitar encriptación TLS; también se acepta PHPMailer::ENCRYPTIONSMTPS

$mail->Port = 587; // Puerto TCP para conectarse

// Destinatarios

$mail->setFrom('tu-correo@example.com', 'Tu Nombre');

$mail->addAddress('destinatario@example.com', 'Nombre del Destinatario'); // Añadir un destinatario

// $mail->addAddress('otro@example.com'); // El nombre es opcional

// $mail->addReplyTo('info@example.com', 'Información');

// $mail->addCC('cc@example.com');

// $mail->addBCC('bcc@example.com');

// Adjuntos (opcional)

// $mail->addAttachment('/var/tmp/file.tar.gz'); // Añadir adjuntos

// $mail->addAttachment('/tmp/image.jpg', 'nuevo.jpg'); // Nombre opcional

// Contenido

$mail->isHTML(true); // Configurar el formato del correo electrónico a HTML

$mail->Subject = 'Aquí está el asunto';

$mail->Body = 'Este es el cuerpo del mensaje HTML <b>en negrita!</b>';

$mail->AltBody = 'Este es el cuerpo en texto plano para clientes de correo que no admiten HTML';

$mail->send();

echo 'El mensaje ha sido enviado';

} catch (Exception $e) {

echo "El mensaje no pudo ser enviado. Error de correo: {$mail->ErrorInfo}";

}

?>

```

Automatically translated