There is an error in sending mail using SMTP

I’m using: SWI-Prolog version ???

I want the code to: fix the error

But what I’m getting is: error(type_error(evaluable,**** / 0),context(system:(>)/2,_10322))

My code looks like this:

% your code here
:- module(send_email, [forgot_password_handler/1, send_reset_password_handler/1]). 

:- use_module(library(http/http_dispatch)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_parameters)).
:- use_module(library(smtp)).
:- use_module(library(http/http_client)).
:- use_module(library(http/http_header)).

forgot_password_handler(_Request) :-
    reply_html_page(
        title('Forgot Password'),
        form([ action('/send_reset_password'), method('POST') ], [
            div(class('form-group'), [
                label([for=email], 'Email: '),
                input([type=email, name=email, id=email, class('form-control'), required])
            ]),
            div(class('form-group'), [
                input([type=submit, value='Reset Password', class('btn btn-primary')])
            ])
        ])
    ).

send_reset_password_handler(Request) :-
    http_parameters(Request, [ email(Email, [string]) ]),
    (   send_reset_email(Email)
    ->  reply_html_page(
            title('Email Sent'),
            div(class('email-success'), [
                p('A reset password email has been sent to your address.')
            ])
        )
    ;   reply_html_page(
            title('Email Failed'),
            div(class('email-error'), [
                p('Failed to send the reset password email. Please try again.')
            ])
        )
    ).

send_reset_email(Email) :-
    smtp_options(Options),
    catch(
        smtp_send_mail(
            Email,
            send_reset_message,
            [ subject('Reset Your Password'),
              from('kimdk9829@gmail.com')
            | Options ]
        ),
        Error,
        (   format('Error occurred while sending email: ~w~n', [Error]),
            false
        )
    ).

smtp_options(Options) :-
    Options = [ smtp('smtp.gmail.com'),  
                port(465),  
                auth('kimdk9829@gmail.com'-'ubxv zqei qgpw greq'),  
                security(ssl)  
              ].

send_reset_message(Out) :-
    format(Out, 'Hi,\n\n', []),
    format(Out, 'Please click the link below to reset your password:\n', []),
    format(Out, 'https://yourdomain.com/reset_password?email=~w&token=YOUR_GENERATED_TOKEN\n\n', [Out]),
    format(Out, 'Best regards,\nYour Website Team\n', []).

I’m working on a code to receive an email and send a simple test email to that email, but when I keep trying to send an email using SMTP, I’ll get an error such as Illegal HTTP parameter: Error occurred while sending email: error (type_error (valid,****/ 0), context (system:(>)/2,_10322) or >/2: Arithmetical: ****/ 0’ is not a function; errors occur in the process of sending an email. Is SMTP option wrong?

I’d try first calling send_reset_email/1 from the commandline. That should print a stack trace which may hint you where these strange values come from. Next, you can run

?- debug(smtp).

To get some verbose output on the progress and/or run

 ?- gtrace, send_reset_email('bob@example.com').

To see what is going on.

I solved it with the debugging method you mentioned. It was simply an error caused by not installing the smtp module pack. I installed the pack with the command pack_install(smtp), and it was solved. Thank you

A few days ago, I asked a question about smtp related errors and paring errors, but I solved the error. There was no problem with smtp and paring, and the problem occurred in the process of setting up the smtp option. The flow is as follows.

  1. Load Settings:
    • send_reset_email/1 is called, smtp_options/1 is called.
    • Get the settings of send_email module from smtp_options/1 and apply correctly to options.
    • At this point, the options are forwarded to Smtp_send_mail/3.
  2. smtp_send_mail/3’ 호출:
    • As Smtp_send_mail/3 is called, the settings within the smtp module are reloaded.
    • This is where the problem comes from. Get security, host’ and port settings from the Smtp module to the default values: ‘security’ is set to none, ‘host’ is set to 1 localhost’, and ‘port’ is set to 0.
  3. Use Defaults:
    • The default port (‘0’) in the default_port/3’ predicate of the smtp module Compares security settings with 0.
    Problems arise at this point.
    The problem appears to occur when the comparison of 0>0 fails.
    That’s why error (type_error (valid,****/ 0), context (system:(>)/2,_10322) occurs.

I thought it was simply a problem because I didn’t download the smtp module, but that wasn’t it :slight_smile: I was able to solve the problem thanks to the trace debugging you taught me. If it wasn’t for that, it would have been really hard to notice. Thank you so much!