Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

POP3/IMAP with Phalcon.

Hi.

I have this picture host with a friend and for each picture can the user click on a button and submit a form for abuse of the picture. This will add it to a MySQL database.

My plan is to make it look and function like a chat, because the end user will enter a email where they can reply on, kinda like a professional ticket system.

My backend will look like http://cl.ly/image/2C251Z2W3E19

I guess i need to make a script that runs every XX min in cron to check for new emails and add them to the mysql table.

But does this exist in phalcon or do i need to use clean php with imap? http://php.net/manual/en/book.imap.php

And how should it be design architecture wise.



14.2k

Thanks for reply. That is only SMTP (sending mail), not fetching :) i have already seen it .

Maybe this might help: https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Mailer



1.5k

Oh sorry, you're totally right, I didn't pay enough attention.

But maybe this might help^^: http://www.wynia.org/wordpress/2007/02/alternate-imap-solution-for-php-pear-net%5imap

It's no phalcon extension, but at least you wouldn't have to use "clean" php



14.2k

Thanks, but i think i would go for something newer than 2007.

I use gmail for this.

Oh sorry, you're totally right, I didn't pay enough attention.

But maybe this might help^^: http://www.wynia.org/wordpress/2007/02/alternate-imap-solution-for-php-pear-net%5imap

It's no phalcon extension, but at least you wouldn't have to use "clean" php



178
Accepted
answer
edited Oct '14

I guess i need to make a script that runs every XX min in cron to check for new emails and add them to the mysql table.

I'd opt for a daemon over a cron job if you can. Aside from picking up new email quicker (eg. using IMAP IDLE), a dameon avoids the possibility of a large amount of email piling up between 'check email' jobs which would slow down the ingress of email into your database and could lead to cron jobs overlapping in severe cases.

But does this exist in phalcon or do i need to use [vanilla] php with imap?

Phalcon doesn't have any built-in IMAP/POP functionality, but a quick search turned up a few PHP libraries like Fetch that fit your requirements. PHP's IMAP/POP libraries are a bit fidly if you're not familiar with the protocol, so using a library with a nice abstraction will probably help you keep your code maintainable.

how should it be design architecture wise.

Here's one possiblity (pretty much reflects what you've said you plan to do). If the system is getting a lot of message traffic, you could push outgoing messages to a queue and have them processed by another daemon in the background, instead of sending emails directly from the front-end.

Diagram



14.2k
edited Oct '14

Thanks for good input, i can use that to something.

Can you maybe give some examples for daemons i totally understand that and would 100% use that instead of a cronjob.

And what do you think of these SQL tables i have at the moment.

CREATE TABLE IF NOT EXISTS `abuse` (
`id` int(11) NOT NULL,
  `pId` int(11) NOT NULL,
  `name` varchar(255) COLLATE utf8_bin NOT NULL,
  `email` varchar(255) COLLATE utf8_bin NOT NULL,
  `message` longtext COLLATE utf8_bin NOT NULL,
  `category` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `ip` varchar(15) COLLATE utf8_bin NOT NULL,
  `status` varchar(50) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `abuseCategories` (
`id` int(11) NOT NULL,
  `name` varchar(255) COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


INSERT INTO `abuseCategories` (`id`, `name`) VALUES
(1, 'Defekt billede'),
(2, 'Misbrug');

CREATE TABLE IF NOT EXISTS `abuseResponse` (
`id` int(11) NOT NULL,
  `aId` int(11) NOT NULL,
  `pId` int(11) NOT NULL,
  `message` longtext COLLATE utf8_bin NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `username` varchar(50) COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

ALTER TABLE `abuse`
 ADD PRIMARY KEY (`id`), ADD KEY `status` (`status`), ADD KEY `category` (`category`), ADD KEY `pId` (`pId`);

ALTER TABLE `abuseCategories`
 ADD PRIMARY KEY (`id`), ADD KEY `name` (`name`);

ALTER TABLE `abuseResponse`
 ADD PRIMARY KEY (`id`), ADD KEY `aId` (`aId`), ADD KEY `pId` (`pId`);

ALTER TABLE `abuse`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `abuseCategories`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;

ALTER TABLE `abuseResponse`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `abuse`
ADD CONSTRAINT `abuse_ibfk_1` FOREIGN KEY (`category`) REFERENCES `abuseCategories` (`name`) ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT `abuse_ibfk_2` FOREIGN KEY (`pId`) REFERENCES `pictures` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
ADD CONSTRAINT `abuse_ibfk_3` FOREIGN KEY (`status`) REFERENCES `statuses` (`name`) ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE `abuseResponse`
ADD CONSTRAINT `abuseResponse_ibfk_1` FOREIGN KEY (`aId`) REFERENCES `abuse` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE,
ADD CONSTRAINT `abuseResponse_ibfk_2` FOREIGN KEY (`pId`) REFERENCES `pictures` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE;

Don't mind my native language as content on categories :)

I guess i need to make a script that runs every XX min in cron to check for new emails and add them to the mysql table.

I'd opt for a daemon over a cron job if you can. Aside from picking up new email quicker (eg. using IMAP IDLE), a dameon avoids the possibility of a large amount of email piling up between 'check email' jobs which would slow down the ingress of email into your database and could lead to cron jobs overlapping in severe cases.

But does this exist in phalcon or do i need to use [vanilla] php with imap?

Phalcon doesn't have any built-in IMAP/POP functionality, but a quick search turned up a few PHP libraries like Fetch that fit your requirements. PHP's IMAP/POP libraries are a bit fidly if you're not familiar with the protocol, so using a library with a nice abstraction will probably help you keep your code maintainable.

how should it be design architecture wise.

Here's one possiblity (pretty much reflects what you've said you plan to do). If the system is getting a lot of message traffic, you could push outgoing messages to a queue and have them processed by another daemon in the background, instead of sending emails directly from the front-end.

Diagram

edited Oct '14

Can you maybe give some examples for daemons

Essentially a daemon is just a long running script. You can generally let another program (like Ubuntu's upstart [tutorial]) handle the running in the background part, and just focus on writing a command line program that continues doing its job until you terminate it:

<?php

while (true) {
    $newMessages = waitForNewMessages();

    if ($newMessages) {
        processMessages($newMessages);
    }
}

You can use the CLI components in Phalcon or something like the Symfony Console component to wrap your program and provide autoloading, option parsing, etc.

what do you think of these SQL tables i have at the moment

What you have looks adequete to provide data to the web interface, though you'll probably need to consider a way to build the emails you're sending from the system - particularly the replies to incoming messages as email clients require certain headers to trigger threaded views (ie. count an email as a reply to one the user sent), and there are email conventions that the recipient might expect to see. Have a look at email message IDs, or do some research into the details of email replies (there are some useful examples in the Internet Message Format RFC appendices).



5.9k

you can use this library for fetching email https://github.com/tedious/Fetch

and use task runner like https://github.com/Codegyre/Robo to manage your background task



14.2k

Thanks for response.

My friend told me a much better way to handle my "problem". Instead of having pop/imap to handle it.

Will I just add a reply system so in the email they get there will be a link to the ticket and then they can reply there. :)