So I've seen other big companies such as Facebook do this, you have the ability to post via email. So here's what I'm trying to do.
Now here's what I got so far. I'm already generating a unique key that's a-z and 0-9, which is 15 characters long. So it seems fine for a small scale project. This part of the code is done.
Then I have a table called Keys, this table has the keys with the email the user registered with (The post must be coming from that email, so if the database is compromised it'll still have a bit more security).
Now the real question is how do I accept the emails? I know this is done through the mail server, but how do I automatically add emails to the server? It sure would be a pain to add each email by hand. So I just want to create working emails users can send content to, and that content will be inserted into the database. So its pretty much a secret email that can accept emails and pop them into the database. I have seen 0 posts or tutorials about this, so I'm not sure where to kick off at all. Any help would be great. I'd just like to make it clear that I'm not asking for you guys to code it for me or anything in that nature.
Configuration: PostFix, PHP, All on Ubuntu 12.04, Apache
PostFix Solution
First Setup the postfix to pipe all the emails to a script there are so many tutorials covering that. I can't give you the complete steps to set up the postfix piping check the provided url for more information on this.
1. Postfix Piping Incoming Mail
2. Postfix Piping Incoming mail
You can use the below script which will parse the emails handed by postfix. You will get From, To, Subject, Message etc you can customize the script
Note: this is not complete its rather a copy paste version just want to show the concept. There are so many libraries to parse emails .
#!/usr/bin/php -q
<?php
//this code will read the piped mail from the postfix
$fd = fopen("php://stdin", "r");
$email_content = "";
while (!feof($fd)) {
$email_content .= fread($fd, 1024);
}
fclose($fd);
//split the string into array of strings, each of the string represents a single line, received
$lines = explode("\n", $email_content);
// initialize variable which will assigned later on
$from = "";
$subject = "";
$headers = "";
$message = "";
$is_header= true;
//loop through each line
for ($i=0; $i < count($lines); $i++) {
if ($is_header) {
// hear information. instead of main message body, all other information are here.
$headers .= $lines[$i]."\n";
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
//Split sender information portion
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
//Split To information portion
if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
preg_match('/<(.*?)>/s', $matches[1], $to);
$key = $to[1];
}
} else {
// content/main message body information
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
// empty line, header section has ended
$is_header = false;
}
}
print $key;
Once you have the key you can use that to validate. You can Insert them into database and notify related users or post topics etc.
Mandrill solution
use this link to to configure mandrill to a url example example.com/parse.php once this is configured you use below php script to insert body of the email to your databse
$mails = json_decode($_POST['mandrill_events']);
foreach ($mails as $mail) {
$stmt = $con->prepare("INSERT INTO mail (text) VALUES (:mail)");
$stmt->bindValue(':mail', $mail->msg->text);
$stmt->execute();
}
the above script is only taking email body. you can use Mandrill Help to know more options like from_email, to, subject etc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With