Can I send Emails

Deutsch:

URL der Website

https://dramag-theater.42web.io/

Frage

Kann ich E-Mails über php über die Website versenden ist das möglich, da ich die Fehlermeldung bekomme, dass das System keine E-Mail senden kann (Und wenn ich SMTP verwende, kann das System keine Verbindung zum Dienst herstellen:

2024-12-27 21:56:49 SMTP-FEHLER: Verbindung zum Server fehlgeschlagen: Zeitüberschreitung der Verbindung (110)

Sonstige Informationen

Ich verwende ein Helpdesk-System. Der Name ist Hesk

English:

Website URL

https://dramag-theater.42web.io/

Question

Can I send Emails via php over the website is that possible, because I get the Error that the system cannot send a Email (And when I use SMTP the system cannot connect to the service:


2024-12-27 21:56:49	SMTP ERROR: Failed to connect to server: Connection timed out (110)

Other Information

I use a help desk system. The name is Hesk

Share your SMTP code, hide the password

5 Likes

Here’s the SMTP code I use to work with gmail:

<?php
// Enable error reporting for errors only
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', 'php_errors.log'); // Save errors to a log file

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

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

$response = [];

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Collect the form data
    $name = htmlspecialchars($_POST['name']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    // Format the name to be used in the filename (replace spaces with underscores)
    $formattedName = preg_replace('/\s+/', '_', $name);

    // Create a unique filename using the name and timestamp
    $timestamp = date("Ymd_His");
    $filename = "mail/{$formattedName}_{$timestamp}.txt";

    // Create the mail directory if it doesn't exist
    if (!file_exists('mail')) {
        mkdir('mail', 0777, true);
    }

    // Prepare the content to be saved
    $content = "Name: $name\n";
    $content .= "Email: $email\n";
    $content .= "Message:\n$message\n";

    // Save the content to the file
    file_put_contents($filename, $content);
    
    // Time to email
    $to = "email";
    $subject = "New message on Louises Miniature Nicnaks";
    $body = "";
    $body .= "From: " . $name . "\r\n";
    $body .= "Email: " . $email . "\r\n";
    $body .= "Message: " . $message . "\r\n";
    
    $mail = new PHPMailer(true);
    
    try {
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;
        $mail->Username = "email";
        $mail->Password = "**********";
        
        // Set who the email is from and to
        $mail->setFrom($email, $name);
        $mail->addAddress($to, "Daniel");

        // Set the subject and body of the email
        $mail->Subject = $subject;
        $mail->Body = $body;
        
        // Send the email and log success or error
        if ($mail->send()) {
            $response = ['status' => 'success', 'message' => 'Your message has been sent. We will get back to you soon.'];
        } else {
            error_log("PHPMailer Error: " . $mail->ErrorInfo);
            
            // Send an error email
            $errorMail = new PHPMailer(true);
            $errorMail->isSMTP();
            $errorMail->SMTPAuth = true;
            $errorMail->Host = "smtp.gmail.com";
            $errorMail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $errorMail->Port = 587;
            $errorMail->Username = "email";
            $errorMail->Password = "**********";
            $errorMail->setFrom("[email protected]", "Error");
            $errorMail->addAddress($to, "Daniel");
            $errorMail->Subject = "Error with form submission";
            $errorMail->Body = "There was an error submitting a form. Please check the error log. Error: " . $mail->ErrorInfo;
            $errorMail->send();

            $response = ['status' => 'error', 'message' => "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"];
        }
    } catch (Exception $e) {
        error_log("Exception: " . $e->getMessage());
        
        // Send an error email
        $errorMail = new PHPMailer(true);
        $errorMail->isSMTP();
        $errorMail->SMTPAuth = true;
        $errorMail->Host = "smtp.gmail.com";
        $errorMail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $errorMail->Port = 587;
        $errorMail->Username = "[email protected]";
        $errorMail->Password = "irsf qpsy meaw tysv";
        $errorMail->setFrom("[email protected]", "Error");
        $errorMail->addAddress($to, "Daniel");
        $errorMail->Subject = "Error with form submission";
        $errorMail->Body = "There was an error submitting a form. Please check the error log. Exception: " . $e->getMessage();
        $errorMail->send();

        $response = ['status' => 'error', 'message' => "Message could not be sent. Exception: {$e->getMessage()}"];
    }
} else {
    $response = ['status' => 'error', 'message' => 'Something went wrong, please try again'];
}

// Ensure only JSON response is sent
header('Content-Type: application/json');
echo json_encode($response);
exit;
?>

bit long winded, but hopefully it can help. This also creates an error log if it fails.

But how should I implement that in a ready-made system. (hesk.com)

(hesk.com)

I have ready-made System named Hesk

The SMTP settings shown in your PHPMailer code all look good.

Did you follow this article to configure SMTP in Hesk?

https://www.hesk.com/knowledgebase/?article=72

Please make sure that you use port 587 with TLS encryption, because port 465, 25 and other potential SMTP ports may be blocked.

7 Likes


It isn’t working

Connection timed out
Required ports are probably blocked on your server’s firewall. Ask your host to configure the firewall to allow TCP connections on ports 587, 995, and/or 993.
Some hosting companies will not do that due to their policies. In that case, you will need to, in order to integrate Gmail with HESK, find another host or try the HESK cloud.

Since you’re using Gmail You may need to set up an app password rather than your normal password. You you can’t Two-Factor authentication over smtp

A Gmail app password is a 16-character code that allows third-party apps or email clients to access your Gmail account:
To create an app password, you must have 2-Step Verification enabled for your account
You can create an app password on the web or using the Gmail mobile app
To create an app password, you can:
Sign in to your Google Account
Click Security
Click App Passwords under Signing in to Google
Select the app and device you’re using
Click Generate
Copy the password

1 Like

still getting the same error

I have uploaded a little test script to your website to verify the SMTP connection works. You can access it here: https://dramag-theater.42web.io/_smtp-port.php?i=1

The script makes use of two different methods to connect to an external host and port. Both work, meaning it’s possible for your website to access the SMTP servers.

As for why Hesk isn’t able to send messages, I don’t know, I don’t have experience with Hesk. But I see nothing to suggest that there is any restriction on our end regarding your ability to use external SMTP severs from our hosting.

4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.