Пробовал phpword, работает только с docx
SELECT m.*, u.* FROM messages m
INNER JOIN (
SELECT receiver, sender, max(id) as id
FROM messages
GROUP BY receiver, sender
) m2 ON m.receiver = m2.receiver
AND m.sender = m2.sender
AND m.id = m2.id
INNER JOIN users u ON m.sender = u.id -- здесь сопоставляется отправитель
WHERE m.receiver = 1
ORDER BY m.date DESC
SELECT receiver, sender, text, date
FROM (
SELECT
@row_number:=CASE
WHEN @receiver_id = receiver AND @sender_id = sender
THEN
@row_number + 1
ELSE
1
END AS num,
@receiver_id:=receiver receiver,
@sender_id:=sender sender,
date,
text
FROM
messages,
(SELECT @receiver_id:=0,@sender_id:=0,@row_number:=0) as t
WHERE receiver = 1 -- ID получателя
ORDER BY
receiver, sender, date DESC
)
WHERE num = 1
ORDER BY date DESC
INNER JOIN users u ON m.sender = u.id
INNER JOIN users u ON m.sender = u.id
поставить вторым