0

I'm building an API and on the aspect of sending a mail. I'm getting mail not sent error. In my API, the front-end sends a JSON request consisting of the mail parameters.

{
    "to":"gideonappoh@gmail.com",
    "subject":"Testing Reviewer's Page",
    "body": "Hello Gideon",
    "headers":"oksana.v@scopicsoftware.com"
}

I then decode them and pass them through a PHPMail method in my controller. But its not working, and I can't find whats wrong. Can someone help me. These are my codes.

    public function actionSendMail() {
    //Getting request from frontend
    $request = file_get_contents('php://input');

    //Decoding input into an array
    $input = json_decode($request, true);

    //Validating request
    if (is_null($input)) {
        $response = json_encode(['error' => 'Bad Input']);
        die($response);

    } else {
        //mail parameters
        $to      = $input['to'];
        $subject = $input['subject'];
        $body    = $input['body'];
        $headers = $input['headers'];

        //Sending mail 
        if(!$this->sendMail($to, $subject, $body, $headers)) {
            $response = json_encode(['error' => 'Mail Not Sent']);
            die($response);
        } else {
            $response = json_encode(['success' => true]);
            echo $response;
        }
    }
}

private function sendMail ($to, $subject, $body, $headers) {
    //Configurating PHP Mailer
    $mail = new PHPMailer();
    $mail->Host = 'stmp.emailsrvr.com';
    $mail->Username = 'gideon.a@scopicsoftware.com';
    $mail->Password = '*******';
    $mail->Mailer = 'stmp';
    $mail->Port = 25;
    $mail->SMTPAuth = true;

    //Sending mail
    $mail->SetFrom($headers);
    $mail->Subject = $subject;
    $mail->IsHTML(true);
    $mail->Body = $body;
    $mail->AddAddress($to, "");

    return $mail->Send();
}

I have read many articles on configuring xampp and I think I should work but still not working. Always getting the response { "error":"Mail not sent" }

Thanks for your help.

2
Contributors
11
Replies
29
Views
2 Years
Discussion Span
Last Post by Gideon_1
0

Hi!

Add to the sendMail() method:

$mail->SMTPDebug = 2; # switch to 3 or 4 to increase verbosity

Then change the return of the sendMail() method to:

if( ! $mail->Send())
    return $mail->ErrorInfo;

return TRUE;

And in actionSendMail() replace the previous sending mail code block with:

//Sending mail 
if($result = $this->sendMail($to, $subject, $body, $headers)) {
    $response = json_encode(['success' => true]);
    echo $response;
} else {
    $response = json_encode(['error' => $result]);
    die($response);
}

Now you should be able to read what is sent to the SMTP server and see why it fails.

Edited by cereal

0

Thanks Cereal, very very much. But it response is both an error and a success.

This is the response

Could not instantiate mail function.
{"success":true}

Could you brief me about that

0

Sorry, I forgot to set the boolean check in:

if($result = $this->sendMail($to, $subject, $body, $headers)) {

Change it to:

if(($result = $this->sendMail($to, $subject, $body, $headers)) === TRUE) {

And it will show the success message only when sendMail() explicitly returns boolean TRUE.

In reference to the error message switch the debug level to 3 to get more information. But I think it's due to the $headers argument, you are setting setFrom() with the sender email address.

Instead use the email that authenticates within the SMTP server, you are not authenticating the sender so you cannot use it in the FROM header. Set the sender email address in AddReplyTo(), so:

$mail->setFrom('gideon.a@scopicsoftware.com');
$mail->AddReplyTo($headers);
0

Cereal, the same but now the succeess message is replaced with the error message.

0

Now this is what I got

SMTP -> ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.(10060)

<br />The following From address failed: gideon.a@scopicsoftware.com : Called Mail() without being connected

 {"error":"Mail Not Sent"}
0

Do I need to configure something in xampp when I'm still using PHPMailer

0

Do I need to configure something in xampp when I'm still using PHPMailer

No, the error seems related to the SMTP server.

As long SMTP server, port number and credentials are correct then you should be able to send the message, at the moment it seems that the connection attempt timeouts, is the SMTP server url correct?

Make sure these values are correct, especially Host and Port:

$mail->Host = 'stmp.emailsrvr.com';
$mail->Username = 'gideon.a@scopicsoftware.com';
$mail->Password = '*******';
$mail->Mailer = 'stmp';
$mail->Port = 25;
$mail->SMTPAuth = true;
0

Okay Cereal, thank you very much for your help. I will contact my Tech Lead to clarify my codes.

0

I have corrected my smtp with some other credentials. My status is 200 but no json response is sent back to the front end

0

Ok, what's your current code?

Consider that you should change the code block that returns the results by removing the echo and the die constructs and by replacing them with return:

if(($result = $this->sendMail($to, $subject, $body, $headers)) === TRUE)
    return json_encode(['success' => true]);

else
    return json_encode(['error' => $result]);

Consider that the die construct stops also the execution of the calling script, so when you include your mailer class into another file it will not return all the expected output, which is not good for production. This construct should be used when you have to debug code.

By the way, if you don't want to show error details then you can also change the IF statement to something simplier:

if($this->sendMail($to, $subject, $body, $headers))
    return json_encode(['success' => true]);

else
    return json_encode(['error' => 'something really wrong just happened']);

And switch back to a boolean return in sendMail():

return $mail->Send();

Edited by cereal

0

Thanks cereal it after what you told me it wasn't the codes which were wrong but the server configuration. Thanks for your time.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.