Sorting Imap mailbox by date using ImapMailbox.php - php

I have a customer support system which creates emails when an email is received. I used to postfix and a special configuration to get a hold of the emails to add extra features.
For example I want to include attachments that were sent from an email. The system doesnt do this , but creates an email with the subject , so I can include the attachments by matching the subjects.
I used ImapMailBox.php to read through the email contents.
It all works fine but I am getting an issue fetching the last email, so I am gettign contents from any other email with the same subject , so I need to fetch the latest email.
$mailboxP = new ImapMailbox('{127.0.0.1:143/novalidate-cert}',POSTFIX_EMAIL,POSTFIX_PASSWORD,ATTACHMENT_DIR, 'utf-8');
foreach($mailbox->searchMails('ALL') as $mailId)
$mail = $mailbox->getMail($mailId);
$mailx=(array)$mail;
$att=$mailx['attachments'];
I have tried using usort to the object $mail , with a function like this
function
mysort($a,$b) {
return strtotime($a->date)-strtotime($b->date);
}
and to the array with a function like this
function mysort($a,$b) {
return strtotime($a['date'])-strtotime($b['date']);
}
I have also tried using imap_sort to $mail and $mailx , but none of this works.
errors I am getting
imap_sort() expects parameter 1 to be resource, array given
imap_sort() expects parameter 1 to be resource, object given
usort() expects parameter 1 to be array, object given
when passing an array I get undefined index date but it defined ..
Can anyone please be kind enough to point me in the right direction.

You can add a function like this on ImapMailbox.php :
public function searchMailsSorted($imapCriteria = 'ALL') {
$this->checkConnection();
$mailsIds =imap_sort($this->mbox,SORTDATE,1,SE_UID,$imapCriteria,$this->serverEncoding);
return $mailsIds ? $mailsIds : array();
}
And then use it in your code like this:
foreach($mailbox->searchMailsSorted('ALL') as $mailId)
{
///insert code here
}

The easiest way is to use Php rsort() function.
<?php
$emailId = rsort($mailbox->searchMails('ALL');
?>

Related

Return in function not working

I am subscribing to data from a MQTT broker with phpMQTT. I have successfully set up a pub / sub routine based on their basic implementation. I can echo the information just fine inside the procmsg() function.
However, I need to take the data I receive and use it for running a few database operations and such. I can't seem to get access to the topic or msg received outside of the procmsg() function. Using return as below seems to yield nothing.
<?php
function procmsg($topic, $msg){
$value = $msg * 10;
return $value;
}
echo procmsg($topic, $msg);
echo $value;
?>
Obviously I am doing something wrong - but how do I get at the values so I can use them outside the procmsg()? Thanks a lot.
I dont know about that lib, but in that code
https://github.com/bluerhinos/phpMQTT/blob/master/phpMQTT.php ,
its possible see how works.
in :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"procmsg");
you are telling it that topic "edafdff398fb22847a2f98a15ca3186e/#" will have Quality of Service (qos) = 0, and an "event" called 'procmsg'.
That's why you later wrote this
function procmsg($topic,$msg){ ... }
so in the while($mqtt->proc()) this function will check everytime if has a new message (line 332 calls a message function and then that make a call to procmsg of Source Code)
thats are the reason why you cannot call in your code to procmsg
in other words maybe inside the procmsg you can call the functions to process message ej :
function procmsg($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Note that you can change the name of the function simply ej :
$topics['edafdff398fb22847a2f98a15ca3186e/#'] = array("qos"=>0, "function"=>"onMessage");
and then :
function onMessage($topic,$msg){
$value = $msg * 10;
doStuffWithDataAndDatabase($value);
}
Sorry for my english, hope this help !

Sending multiple mail issue with laravel 5

I have users database, and I want to send email to all my one form So I am trying like this:
$d=Mail::send('admin.email_template', $data, function ($message) use ($data,$emailIds) {
$message->from('dinesh224401#gmail.com', 'Dinesh Laravel');
$message->to($emailIds)->subject($data['subject']);
});
where $emailIds have 'dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'
but I am getting this error:
Address in mailbox given ['abc1#rediffmail.com', 'abc#rediffmail.com'] does not comply with RFC 2822, 3.6.2.
if I use directly emails in mail function like this:
$message->to('abc1#rediffmail.com', 'abc#rediffmail.com')
then it works,
Updated:
I am making this string from array as:
$aa=implode("', '",array('dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'));
//print_r("'".$aa."'");
$emailIds="['".$aa."']"; //I have used [] here but it did not work also
echo $emailIds
//output ['dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com']
I do not know what is problem, Thanks in advance.
Because, $emailIds = 'dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'; is a string, comma separaed.
the to method of Mail requires an array.
Try this:
$emailIds = ['dinesh.rkgit#rediffmail.com', 'anu.rkgit#rediffmail.com'];
$d=Mail::send('admin.email_template', $data, function ($message) use ($data,$emailIds) {
$message->from('dinesh224401#gmail.com', 'Dinesh Laravel');
$message->to($emailIds)->subject($data['subject']);
});

What does the is_final parameter in xml_parse() do?

I'm working on creating a class for xml parsing, and need to use this method from PHP called xml_parse.
It has 3 parameters like so:
int xml_parse ( resource $parser , string $data [, bool $is_final = false ] )
According to the PHP manual is_final it means whether its the last piece of data sent in this parse, but what does this mean? does this something have to do with resource $parser? as far as I'm aware this function does not allow an input stream of data, thus my confusion.
Someone please explain what it does
is_final means that if you are parsing the last line of your $data you must set this parameter to true.
in addition there is a note in the Doc :
Entity errors are reported at the end of the parse. And will only show if the "end" parameter is TRUE
See the sample below from w3schools
<?php
$parser=xml_parser_create();
function char($parser,$data)
{
echo $data;
}
xml_set_character_data_handler($parser,"char");
$fp=fopen("test.xml","r");
while ($data=fread($fp,4096))
{
xml_parse($parser,$data,feof($fp)) or
die (sprintf("XML Error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
xml_parser_free($parser);
?>

How to pass a 2 dimensional array value in a swift mailer setTo function

I am getting a 2 dimensional array value as a result after a for loop.The value is $chunk[$i][$j].And when I passed that value into setTo function,
the error showing as
Warning: preg_match() expects parameter 2 to be string, array given in H:\xampp
\htdocs\sngmarket\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mime\Headers
\MailboxHeader.php line 350.
How do I solve this?.Here my code
$query = $em->createQuery("SELECT DISTINCT u.emailaddress FROM AcmeRegistrationBundle:userlist u");
$grp_emails[] = $query->getResult();
$chunk = array_chunk($grp_emails, 10);
$get_chunk_count = count($chunk);
for($i=0;$i<$get_chunk_count;$i++)
{
$count_inside_count = count($chunk[$i]);
for($j=0;$j<=$count_inside_count;$j++)
{
$mails=$chunk[$i][$j];
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('marketplace#socialnetgate.com')
->setTo($mails)
->setReturnPath('gowtham#ephronsystems.com')
->setBody('Hello World');
$this->get('mailer')->send($message);
return array();
}
}
I think you are overthinking this.
Have you looked at the documentation on how to send batch emails WITHOUT recipients being aware of each other? In your snippet each email contains up to 10 recipients, which may be better then sending all recipients, but still is pretty bad.
Have a look at Sending emails in batch and also at the plugins to make sure you don't reach the limit of emails you are allowed to send in a certain time frame.

How to return multiple values from a function [duplicate]

This question already has answers here:
Multiple returns from a function
(32 answers)
Closed 6 years ago.
I'm building a script to work with PxPost from Payment Express and I've used their sample code as can be found at http://www.paymentexpress.com/Technical_Resources/Sample_code_-_PHP/PX_Post_-_cURL.aspx
How it works: It's built into an automated script that queries orders from my database, processes them, and returns a value.
My only problem is I want the function to return more than one value, so this is what I've done.
Code to run through functions (Line 201):
$once_complete = process_request($billingID, $order_total, $merchRef);
Which send the payment to be processed, that then gets the returns and processes the XML using the sample code. At the end of the code I've removed all the $html info and just replaced it with the following (line 111):
return $CardHolderResponseDescription.":".$MerchantResponseText.":".$AuthCode.":".$MerchantError;
Which should as far as I understand, return that to what started it. I then want to split those values and return them as strings using the following (line 202):
list($RespDesc, $MerchResp, $AuthCode, $MerchError) = explode(":", $once_complete);
But for some reason that's not working.
I've tried echo-ing the return and it works fine then, but then after that it seems to disappear. What may be going wrong?
You can see the entire page's code at http://pastebin.com/LJjFutne. This code is a work in progress.
Return an array.
function process_request(){
...
return array( $CardHolderResponseDescription, $MerchantResponseText, $AuthCode, $MerchantError );
}
And pick it up via:
$_result = process_request();
$CardHolderResponseDescription = $_result[0];
$MerchantResponseText = $_result[1];
...
Tip: use shorter vars for better reading :)
In your function process_request:
return array($CardHolderResponseDescription, $MerchantResponseText, $AuthCode, $MerchantError);
When calling your function:
list($RespDesc, $MerchResp, $AuthCode, $MerchError) = process_request($billingID,$order_total,$merchRef);
The simplest thing you can do is putting the return values in an array which you can access later:
return array("CardHolderResponseDescription"=>$CardHolderResponseDescription, "MerchantResponseText" => $MerchantResponseText, "AuthCode" => $AuthCode );
And later:
list($RespDesc, $MerchResp, $AuthCode, $MerchError) = $my_return_value

Categories