0

I am working on my jquery code to send the data to PHP via ajax so I can send the emails. I have got a problem with my code, because when I send the emails for each 20 seconds until it will get to the last email that send, it will not hide the element notify-outer.

Jquery:

$(document).on('click','#domodal_send', function(e) {

    if ($(".notify-outer").css('display') == 'none') {


        var subject = $("#domodal_subject").val();
        var from_name = $("#avater_name").text();
        var from_email = $('#avater_email').text();
        var to_email = $("#domodal_email_receipt").val();
        var to_emails = to_email.split(',');
        var emailbody = $("#domodal_message").html();
        var attachments = [];
        var i = 0;
        $("#notify-msg").text('Sending...');
        $(".notify-outer").show();
        $("#domodal_message").text('');
        $('.popup-window').hide();

        $(".file-list_yhs .vI").each(function() {
            attachments.push('uploads/' + $(this).text());
        });


        setTimeout(function(){

            $.ajax({
                url: 'sendMail.php',
                type: 'POST',

                data : {
                    send_to: to_emails,
                    from: from_name,
                    from_email: from_email,
                    emailsubject: subject,
                    emailbody: emailbody,
                    emailattachment: attachments
                },
                dataType: 'json',

                success: function(result)
                {
                    $(this).attr("disabled", false);
                    alert(result['success']);

                    if (result['success'] == 'successfully') {

                        $("#notify-msg").text('Your message has been sent!');
                        $('#inbox_total').html(result.total_inbox);
                        $('#spam_total').html(result.total_spam);
                        $('.popup-window').remove();

                        setTimeout(function(){
                            $("#loading").hide();
                            $("#notify-msg").text('');
                            $(".notify-outer").hide();
                        }, 3000);
                    }
                }
            });
        }, 2500);
    }
});

SendMail.php

<?php
require_once "Mail.php";
require_once "Mail/mime.php";
require_once('Mail/IMAPv2.php');

//Connect to the database
include('config.php');

$inbox = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX';
$spam = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX.spam';
$mailserver = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX.Sent';

if (isset($_POST['send_to']))
{
    $from = "Chris <chris@example.com>";
    $to_email = $_POST['send_to'];
    //$to = $firstname . " <$to_email>";
    $subject = $_POST['emailsubject'];
    $message = $_POST['emailbody'];
    $sent_message = $_POST['emailbody'];
    $email_attachments =  $_POST['emailattachment'];
    $username = 'myusername';
    $password = 'mypassword';
    $smtp_hostname = "smtp.example.com";
    $port = "587";


    foreach ($to_email as $to_emails) {

        $messageID = sprintf("<%s.%s@%s>",
                base_convert(microtime(), 10, 36),
                base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
                'example.com');
        $date = date('Y-m-d H:i:s');


        $body = $mime->get($mime_params);
        $headers = $mime->headers($headers);
        $smtp = Mail::factory ('smtp', $params);
        $mail = $smtp->send($to_emails, $headers, $body);

        if (PEAR::isError($mail)) 
        {
            echo("<p>" . $mail->getMessage() . "</p>");
        }
        else 
        {

            $mailbox = imap_open($mailserver, $username, $password);

            imap_append($mailbox, $mailserver,
                "From: ".$from."r\n".
                "To: ".$to_emails."\r\n".
                "Subject: ".$subject."\r\n".
                "Date: ".date("r", strtotime("now"))."\r\n".
                "Message-ID: ".$messageID."\r\n".
                "MIME-Version: 1 \r\n".
                "Content-Type: multipart/mixed; boundary=\"$boundary1\"\r\n".
                "\r\n\r\n".
                "--$boundary1\r\n".
                "Content-Type: multipart/alternative; boundary=\"$boundary2\"\r\n".
                "\r\n\r\n".
                // ADD Plain text data
                "--$boundary2\r\n".
                "Content-Type: text/plain; charset=\"utf-8\"\r\n".
                "Content-Transfer-Encoding: quoted-printable\r\n".
                "\r\n\r\n".
                $sent_message."\r\n".
                "\r\n\r\n".
                // ADD Html content
                "--$boundary2\r\n".
                "Content-Type: text/html; charset=\"utf-8\"\r\n".
                "Content-Transfer-Encoding: quoted-printable \r\n".
                "\r\n\r\n".
                html_entity_decode($sent_message)."\r\n".
                "\r\n\r\n".
                "--$boundary1\r\n".
                "\r\n\r\n".
                // ADD attachment(s)
                $attachment1.
                "\r\n\r\n".
                "--$boundary1--\r\n\r\n",
                "\\Seen"
            );

            $response = array("total_inbox"=>$total_inbox_unread,"total_spam"=>$total_spam_unread,"success"=>"successfully");
            echo json_encode($response);

        }
        sleep(20);
    }
}

// Close connection
imap_close($inbox);
imap_close($spam);
mysqli_close($link);
?>

I have also tried this:

    }    
   sleep(20);
}
$response = array("total_inbox"=>$total_inbox_unread,"total_spam"=>$total_spam_unread,"success"=>"successfully");
echo json_encode($response);

The alert for result['success'] have return as empty. I can't get pass it and it will not hide the element .notify-outer after when the last email is sent.

What I am trying to achieve is when I send the emails, I want to disply the element #notify-msg with text sending... until it get to the last email that is sent, then I want to change the text Your emails has been sent! in the element #notify-msg for 3 seconds before I could hide the element .notify-outer.

Can you please show me an example how I could change the text for few seconds before I could hide the element?

Thank you.

  • Please post the client side ajax code. The code you posted will not help with your question. – Jamie_D Sep 07 '19 at 15:19
  • @Jamie_D I have already posted sendmail.php code. Please check again. –  Sep 07 '19 at 15:25
  • notify-outer is a class, not an id. Are you sure notify-outer is a class and not an ID? – Jamie_D Sep 07 '19 at 15:45
  • @Jamie_D Yes I am sure the notify-outer is a class not the id. –  Sep 07 '19 at 15:54
  • Add your code in:`$.ajax({beforeSend: function (){}, complete: function (){}})` – Ritesh Khandekar Sep 07 '19 at 16:18
  • `this` is not what you think it is inside `success`. See [How to access the correct `this` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – charlietfl Sep 07 '19 at 16:26

0 Answers0