2

Currently I am managing mailboxes with cPanel. I configured the mailboxes in cPanel, to pipe the emails to a PHP script. I am using cPanel only for that purpose, so I wanna get rid of all that gear.

All I need is:

  • Create a email server (from here I guess I will need some PHP library/application)
  • Create a email account/mailbox in the email server
  • For each email received by the mailbox: run a PHP script which processes the bytes

These PHP extensions rely on preexistant email server and account, so, these alone are not enough.

From what I have been researching, apparently I need to do it throught PHP command line, with CLI or CGI, not sure which one.

I work in a VPS with LAMP CentOS, maybe there is a open source PHP application or library that can be implemented here, just for the specific task.

mikl
  • 1,067
  • 1
  • 20
  • 34
  • What do you mean by "pre-existent email accounts/mailboxes"? What do you mean by "Create a email account/mailbox with PHP"? Certainly the typical application does access some existing mailbox, since it makes little sense to implement all that server stuff in php! So usually you rely on a server receiving emails and access those. Maybe you want to make more clear what it is you actually want to do. – arkascha Nov 27 '15 at 14:52
  • @arkascha, edited the context, althought what you quoted remains intact, because it makes more sense now. – mikl Nov 27 '15 at 15:20

2 Answers2

5

Sorry to say so, but what you plan to do does not make any sense at all.

Reasons:

To be able to receive and access emails you'd have to implement at least one fully fledged networking server: an SMTP server to receive and either additional servers for accessing the mailbox, like an IMAP4 server or similar. Plus all the overhead for ssl or tls encryption, authentication, and and and...

  • PHP is not a language well suited for such a project.
  • the amount of work such plan means is probably underestimated by you currently. If you take a look at existing implementations you will see that they are each pretty big and complex software packages. I'd say implementing such an amount of stuff will take a really good programmer months if not years.

Please don't get this wrong, but my impression is that you did not yet fully understand how email exchange actually works and what is required for that.

Instead I would suggest that you work into this field by getting hold of some standalone system you can work with, maybe a virtual private server you rent including a domain name and a static IP address. Note that some webspace with php is not enough for this. Then you install the typical packages, some Linux operating system, maybe the Postfix SMTP server, the Dovecot IMAP server and everything that is required by them. Then you configure that stuff and if you really succeed and are able to actually receive messages by that setup, then is the time to come back to this question. I'd expect two things:

  1. you will have learned quite a lot by that experiment and
  2. you will agree that your current plan does not make sense.
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • I am not looking to write the whole thing. Maybe there is a open source PHP library/application for the specific task of creating server -> account -> receiving emails, not even need to parse the email, as I get the byte string I am good to go. Probably I have no idea of what I am talking about, but had to do my last try. – mikl Nov 27 '15 at 16:01
  • @mikl I am not aware of such a "library" and I doubt there is such thing out of the same reasons: PHP is not a language suited for such a task. That simply does not make sense. – arkascha Nov 27 '15 at 16:34
3

For reading emails in the mailbox I would suggest using IMAP. Here emails are stored on a server and the IMAP protocol allows you to access the emails on the server.

You must enable IMAP on the server and use the imap_open function [1]

An example of this taken from php.net is here:

<?php
$mbox = imap_open("{imap.example.org:143}", "username", "password");

echo "<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox($mbox, "{imap.example.org:143}", "*");

if ($folders == false) {
    echo "Call failed<br />\n";
} else {
    foreach ($folders as $val) {
        echo $val . "<br />\n";
    }
}

echo "<h1>Headers in INBOX</h1>\n";
$headers = imap_headers($mbox);

if ($headers == false) {
    echo "Call failed<br />\n";
} else {
    foreach ($headers as $val) {
        echo $val . "<br />\n";
    }
}

imap_close($mbox);
?>

In terms of creating new accounts, if you have an HTML input form for account creation on the mail server a very hacky solution would be to use PHP cURL to submit POST variables to create a new account, a nice stackoverflow answer on the topic here [2].

When you submit information on a registration form you are essentially submitting a series of POST variables. If you are not familiar with cURL or the basics of HTTP requests this might take some reading.

This will probably involve using cURL to login to the admin panel of whichever management framework you have in place for the mail server. If you are a root user on the mail server in question there are much nicer solutions to this.

[1] http://php.net/manual/en/function.imap-open.php

[2] Passing $_POST values with cURL

Community
  • 1
  • 1
Parsa
  • 3,054
  • 3
  • 19
  • 35
  • Sorry, edited question. Althought you are still answering important part of it, but the part I am most intrigued is how to create the email server, because as I mentioned, I need to get rid of cPanel, and of course I wont start using gmail or anything, because it would be the same: a lot of complexity and functionalities that I do no need, because I just need to receive emails and process the bytes with PHP. – mikl Nov 27 '15 at 15:24
  • Do you have root access to the server in question? If this is shared hosting then you will not be able to get rid of cpanel. Gmail isn't installed on the server, the server will forward the emails to the gmail server which can then be accessed by logging onto gmail see here https://apps.google.com/intx/en_my/products/gmail/ – Parsa Nov 27 '15 at 23:48
  • @ParsaAkbani I have root access. But the thing is I have to create and manage a lot of different email accounts, it is not just one account, each account is piping emails to a different script. Using gmail accounts will probably be as much or more complex than using cPanel (hiring one more external service), in the other hand if I create my own email server, in the end it would be way more complicated than the other two options. Now I am thinking of keeping cPanel, but create and configure the accounts automaticaly with PHP, otherwise i will just have to deal with the cPanel GUI for now. – mikl Nov 28 '15 at 14:15
  • I don't think piping the emails to scripts is easily achievable, you should have the scripts periodically query for new mail and process when they do find them. The really easy solution is IMAP which will work with cpanel. – Parsa Nov 28 '15 at 16:39