SSilence / php-imap-client

a easy solution for simple IMAP email access in php
MIT License
268 stars 136 forks source link

How do I use convertToUtf8() #173

Closed abdullahseba closed 7 years ago

abdullahseba commented 7 years ago

Hi how do I use convertToUtf8() ? Docs just say "Apply encoding defined in header". And when I try using it it says at least one argument is expected. I want $imap->getMessages() to be returned in UTF-8 otherwise I get all sorts of problems.

mattparksjr commented 7 years ago

This is the function convertToUtf8:

    public function convertToUtf8($str) {
        if (mb_detect_encoding($str, "UTF-8, ISO-8859-1, GBK")!="UTF-8") {
            $str = utf8_encode($str);
        }
        $str = iconv('UTF-8', 'UTF-8//IGNORE', $str);
        return $str;
    }

All this code does is convert the provided string into utf8.

abdullahseba commented 7 years ago

Dayum My error log is full of these:


** (http://watt.u (errflg=3) in Unknown on line 0
[30-May-2017 11:26:14 Europe/London] PHP Notice:  Unknown: Invalid quoted-printable sequence: =TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256)       5 sec

Origin (errflg=3) in Unknown on line 0
[30-May-2017 11:26:24 Europe/London] 5
[30-May-2017 11:26:49 Europe/London] PHP Notice:  Unknown: Invalid quoted-printable sequence: =sendgrid&utm_campaign=free-post_DHN&utm_medium=email
mattparksjr commented 7 years ago

Is this with or without convertToUtf8?

abdullahseba commented 7 years ago

Without.

its whats been returned by getMessages() .

And I cant convert it to JSON or XML for javascript on my client.


From: Matthew Parks notifications@github.com Sent: 30 May 2017 12:26 To: SSilence/php-imap-client Cc: abdullahseba; Author Subject: Re: [SSilence/php-imap-client] How do I use convertToUtf8() (#173)

Is this with or without convertToUtf8?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/SSilence/php-imap-client/issues/173#issuecomment-304849752, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AYXX0tRNto0rJSkvpLip6n22pOuSrtd5ks5r-_zjgaJpZM4NpgEt.

mattparksjr commented 7 years ago

Try it with, everything that is printed.

abdullahseba commented 7 years ago

but its an array, You said string?

mattparksjr commented 7 years ago

How do you print your array?

abdullahseba commented 7 years ago

print_r() as the ajax response.

abdullahseba commented 7 years ago

But I need it as a JSON otherwise I'm stuck

mattparksjr commented 7 years ago

So if im correct your making an ajax front end which connects to the backend?

abdullahseba commented 7 years ago

yes

abdullahseba commented 7 years ago

basically a waaay better alternative to squirrelmail and roundcube baseed on your library

abdullahseba commented 7 years ago

image

abdullahseba commented 7 years ago

its part of a bigger ERP project but I think I will have it separate too.

mattparksjr commented 7 years ago

Let's say this is your workflow workflow As you can see, your front end, ajax, connects to your code which connects to ours. Let's say a user clicks an open email link. This makes ajax send the request to your code which requests ours. Let says the user clicked an email with the id of 1. Ajax goes to your code and says hey, I need the sender, the receiver, the subject and the message of the email with id one. Your code request to ours. Your code then says alright, this is the sender, receiver, subject, and message, but first let me convert them to utf8 so you can echo this. Your code THEN builds the response from ours into a proper json array(https://www.w3schools.com/js/js_json_php.asp). Then your code sends it to your front end. You can then process it from there

** Excuse terrible ms paint skills :)

abdullahseba commented 7 years ago

Thats exactly what I'm doing so your library in a service provider and than my classes is in the controller. Its using the Slim framework.


public function getMessage($request, $response, $args)
  {
    $imap = $this->ci->imap;
    //Get request data from POST.
    $params = $request->getParsedBody();
    //Select mailbox
    $imap->selectFolder($this->getMailboxName($mailbox = $params['currentMailbox']));

    $emails = $imap->getMessages($number = $params['number'], $start = $params['start'], $order = 'DESC', IncomingMessage::NOT_DECODE);

    $emailsBody = array();

    foreach ($emails as $email) {
      $emailsBody[] =   $email->message->plain.PHP_EOL;
    }
    json_encode($emailsBody);

    print_r(array_reverse($emailsBody));
  }
abdullahseba commented 7 years ago

Usually I would just return the JSON object, but that causes the UTF-8 error

mattparksjr commented 7 years ago

So, try and hand make that arary. So if you get the subject of the email, utf8 it, add it to and array and do the same for all the other info, then return that array.

abdullahseba commented 7 years ago

whooo! Works! Thanks :) I did mess around with mb_x_encoding previously but I messed up.


  foreach ($emails as $email) {
      $body =  $imap->convertToUtf8($email->message->plain.PHP_EOL);
      $emailsBody[] =   $body;
    }
    return  json_encode($emailsBody);