如何将传入的电子邮件插入mySQL数据库?

Just to say... I have no idea how would I do this :).

In other words, the thing is: You get a mail message from some guy pinky@pinky.com, you make a reply with your own content, and pinky@pinky.com have your reply stored in his database.

Additional: I use shared hosting, I code in PHP, I understand ASP. If you have an idea how to write this script in another language, don't bother explaining me, because I won't understand anything.

A link to a solution is also welcomed.

Thanks in advance.

So
- you have a server
- you get emails
- you want to save them int a mysql database

Cpanel config
- go to cpnal email forwarder
- add a new one
- redirect to PATH -> /home/your_user/whatever/php.script.php

Php script (you may need to change the "/usr/bin/php -q" path depending on your server configuration)

#!/usr/bin/php -q
<?php
chdir(dirname(__FILE__));
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
    $email .= fread($fd, 1024);
}
fclose($fd);

if(strlen($email)<1) {
    die(); 
}

// handle email
$lines = explode("
", $email);

// empty vars
$from = "";
$to="";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
    if ($splittingheaders) {
        // this is a header
        $headers .= $lines[$i]."
";
        // look out for special headers
        if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
            $subject = $matches[1];
        }
        if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
            $from = $matches[1];
        }
        if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
            $to = $matches[1];
        }
    } else {
        // not a header, but message
        $message .= $lines[$i]."
";
    }
    if (trim($lines[$i])=="") {
        // empty line, header section has ended
        $splittingheaders = false;
    }
}

Works on shared hosting too! :)

All you need to add is the mysql insert and use the above-defined variables. Do you know how to use a mysql database from php? Or do you need help with that too?

If you can set up your email server to forward email received at a certain address to a script residing on your server, this is a great one to use:

http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-3/

Greg

I had this same issue and was looking for something in PHP, so I looked for a parser first and then a DB Importer.

So I put them together and got this: https://github.com/escobar022/php-imap-ToDB

I found that the most important part was to know how an email breaks down(header,body,etc). So I found a parser class (https://github.com/barbushin/php-imap) that takes apart emails and retrieves the email in its essential parts and has the ability to download the items. I would recommend trying split up a complex email into parts and seeing what exactly is going on.

Once I understood this, I tried to send it using PHPmailer, and figured out how each part is needed to replicate the email. Once I figured this out, I stored these parts, breaking down the headers/body/attachments(relative paths) into the database.

Hope this helps!

You can alternatively use a service such as Parseur (https://parseur.com) and/or Zapier (https://zapier.com) to directly insert the email into the database.

With Parseur+Zapier, you can even select which piece of data you want to extract into which column in the database.

It saves time. But if you love to code, it's not the way to go :)

As @sylvain mentioned above, if you:

  1. Don't like to code
  2. Like to start out doing this for free

Then try out Parserr and combine that with either Microsoft Flow or Zapier to make sure you can send your data to pretty much any database you require, including MySql.

There is also a step-by-step guide located here if you need it too!