TheFox / smtpd

SMTP server (library) for receiving emails, written in pure PHP.
https://fox21.at
MIT License
121 stars 30 forks source link

I tried to auth but the event is not calling my function #6

Closed itabrezshaikh closed 7 years ago

itabrezshaikh commented 8 years ago

Code: ---------- inside example.ph ---------------- ... $authEvent = new Event(Event::TRIGGER_AUTH_ATTEMPT, null, function ($event, $type, $credentials) { // Do stuff: Check credentials against database, ... die('auth no');

return true;

});

...

Run like below: php example.php Output: PHP Notice: Use of undefined constant TEST - assumed 'TEST' in /var/www/html/qunovamail/deliverybox/smtpd/src/TheFox/Smtp/Server.php on line 67 [2016-07-25 10:47:40-0400] server.NOTICE: listen ok

[2016-07-25 10:47:43-0400] server.DEBUG: client 1 data send: "220 localhost.localdomain SMTP Service Ready" [2016-07-25 10:47:43-0400] server.DEBUG: client 1 data send: "250-localhost.localdomain\n250-AUTH PLAIN LOGIN\n250 HELP" [2016-07-25 10:47:43-0400] server.DEBUG: client 1 data send: "334 VXNlcm5hbWU6" [2016-07-25 10:47:43-0400] server.DEBUG: client 1 data send: "334 UGFzc3dvcmQ6" [2016-07-25 10:47:43-0400] server.DEBUG: client remove: 1

Problem: The above does not call die or any other code inside the passing anonymous function.

itabrezshaikh commented 8 years ago

This is the code: <?php require_once DIR.'/vendor/autoload.php'; use TheFox\Smtp\Server; use TheFox\Smtp\Event;

//$server = new Server('127.0.0.1', 20025); $server = new Server('0.0.0.0', 25); $server->init(); $server->listen();

$sendEvent = new Event(Event::TRIGGER_MAIL_NEW, null, function($event, $from, $rcpts, $mail){ // Do stuff: DNS lookup the MX record for the recipient's domain, ...

// For example, use PHPMailer to reply the mail through mail servers.
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = 'tls';
$mailer->Host = 'smtp.example.com';
$mailer->Port = 587;
$mailer->Username = 'example@example.com';
$mailer->Password = 'your_password';
$mailer->SetFrom('example@example.com', 'John Doe');
$mailer->Subject = $mail->getSubject();
$mailer->AltBody = $mail->getBody();
$mailer->MsgHTML($mail->getBody());

foreach($rcpts as $rcptId => $rcpt){
    $mailer->AddAddress($rcpt);
}

if(!$mailer->Send()){
    throw new Exception($mailer->ErrorInfo);
}

});

$authEvent = new Event(Event::TRIGGER_AUTH_ATTEMPT, null, function ($event, $type, $credentials) { // Do stuff: Check credentials against database, ... die('auth no');

return true;

});

$server->eventAdd($sendEvent); $server->eventAdd($authEvent);

// loop() is only a loop with run() executed. // So you need to execute run() in your own project to keep the SMTP server updated. $server->loop();

ashleyhood commented 8 years ago

What email client are you using? Do you have root access to be able to listen on port 25?

I would try running the server on port 20025 and try the following code as the client that sends the email:

require_once "vendor/autoload.php";

$mail = new PHPMailer;

//Enable SMTP debugging. 
$mail->SMTPDebug = 3;                               
//Set PHPMailer to use SMTP.
$mail->isSMTP();            
//Set SMTP host name                          
$mail->Host = "127.0.0.1";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
$mail->AuthType = 'LOGIN';
//Provide username and password     
$mail->Username = 'name@example.com';                 
$mail->Password = 'super_secret_password';                           
//If SMTP requires TLS encryption then set it
// $mail->SMTPSecure = "tls";                           
//Set TCP port to connect to 
$mail->Port = 20025;                                   

$mail->From = "name@example.com";
$mail->FromName = "Full Name";

$mail->addAddress("recpt@example.com", "Recepient Name");

$mail->isHTML(true);

$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}
itabrezshaikh commented 8 years ago

The example you have given is for calling smtp server with username and password. And I am able to successfully sends the request. The problem is I am not able to trigger function in which I can use my own database and username for verifying the account.

Yes I do have superuser access.

ashleyhood commented 8 years ago

I have done a fresh git clone. In the example.php file, I removed the phpmailer for the Event::TRIGGER_MAIL_NEW function and added die('auth no') in the Event::TRIGGER_AUTH_ATTEMPT function (as you have above).

I then on the cli, I ran php example.php and in another terminal window ran php example-client.php (with the client code I posted above). This caused the example.php script to die with the message 'auth no' as expected.

Are you sure you have version 0.2 of smtpd installed?

itabrezshaikh commented 8 years ago

I rechecked the version ..its v.0.2 but still not working git log commit 7bd6a951bff6168adacaa850371592b8c3b490ac Author: Christian Mayer christian@fox21.at Date: Tue Jul 19 17:48:25 2016 +0200

Version 0.2.0.

..

ashleyhood commented 8 years ago

This is what I have done to get it to work.

Created a new directory: mkdir smtpd-test cd smtpd-test

Install smtpd via composer: composer require thefox/smtpd

Create server.php:

<?php
require_once __DIR__.'/vendor/autoload.php';

use TheFox\Smtp\Server;
use TheFox\Smtp\Event;

$server = new Server('127.0.0.1', 20025);
$server->init();
$server->listen();

$authEvent = new Event(Event::TRIGGER_AUTH_ATTEMPT, null, function ($event, $type, $credentials) {
    die('no auth');

    return true;
});

$server->eventAdd($authEvent);

$server->loop();

Create client.php:

<?php
require_once "vendor/autoload.php";

$mail = new PHPMailer;

$mail->isSMTP();            
$mail->Host = "127.0.0.1";
$mail->SMTPAuth = true;
$mail->AuthType = 'LOGIN';
$mail->Username = 'name@example.com';                 
$mail->Password = 'super_secret_password';                           
$mail->Port = 20025;                                   
$mail->From = "name@example.com";
$mail->FromName = "Full Name";
$mail->addAddress("recpt@example.com", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";

if(!$mail->send()) 
{
    echo "Mailer Error: " . $mail->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}

Run server.php: php server.php

In a separate terminal, run client.php: php client.php

When client.php has finished running it will return the error:

2016-07-27 16:16:15     SMTP ERROR: Password command failed:
2016-07-27 16:16:15     SMTP Error: Could not authenticate.
2016-07-27 16:16:15     SMTP NOTICE: EOF caught while checking if connected
2016-07-27 16:16:15     Connection: closed

The server.php script will die and print no auth as expected.

How does your code differ from this?

TheFox commented 7 years ago

Works for me fine as well. I also get auth no as expected. @itabrezshaikh Are you still having problems?