Posts Tagged ‘php’

Connect to strato.de with imap PHP functions

Connecting to strato.de with imap isn’t easy at all, as there’s little documentations on the Internet, and they are mainly in dutch. So I will present in the following lines how you can access your e-mails from a php file.

$server = ‘{imap.strato.de:143}’;
$connection = imap_open($server, “youremail@yourdomain.nl”, “yourpassword”);

print_r(imap_errors());

$mailboxes = imap_list($connection, $server, ‘*’);

foreach($mailboxes as $mailbox)
$shortname = str_replace($server, ”, $mailbox);

imap_reopen($connection, $server.’INBOX’);

$count = imap_num_msg($connection);

for($i = 1; $i <= $count; $i++) {
$header = imap_headerinfo($connection, $i);
$raw_body = imap_body($connection, $i);
}

$server – the connection parameteres, differs from server to server. For exemple:

  • Google: {imap.gmail.com:993/ssl/novalidate-cert}
  • Google Apps: {imap.googlemail.com:993/ssl/novalidate-cert}

$connection: this is the IMAP stream

$mailboxes: will contain all the mailboxes you have (Inbox, Trash, Sent + persobal mailboxes)

$shortname: will contain just the name of the mailboxes, without their server address

imap-reopen: you can make a new stream, connecting to a given mailbox (INBOX in my case)

$count: how many mails do you have in the selected mailbox

- the last for cycle runs through the mailbox, and for every e-mail gets it’s header information ($header) and the mail’s content ($raw_body)

Copy / duplicate database with PHP

How to copy / clone / duplicate an entire database, with all the tables and data contained?

Here are some definitions for start…:

define(“DB_HOST”, “localhost”);
define(“DB_DATABASE”, “is_general”);
define(“DB_DATABASE_NEW”, “is_general”);
define(“DB_USERNAME”, “general”);
define(“DB_PASSWORD”, “g3n3r@l”);

Connect to the original database:

mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
mysql_select_db(DB_DATABASE);

Create the new database and add an existing user to it (in our case, general) with all privileges:

mysql_query(“CREATE DATABASE “.DB_DATABASE_NEW);
mysql_query(“GRANT ALL PRIVILEGES ON “.DB_DATABASE_NEW.”.* TO ‘general’@'%’ WITH GRANT OPTION”);

Get all the table names from the original table (here you must be careful, because if there are dependecies, as foreign keys from one table the another, it can cause some problems, as the order of the tables is important when they will be recreated):

$r = mysql_query(“SELECT Table_name,Table_rows FROM information_schema.tables WHERE TABLE_SCHEMA = ‘is_default’”);
while ($d= mysql_fetch_assoc($r))
$tables[] = $d['Table_name'];

If the database is not empty, so it contains tables:

if (is_array($tables))
foreach ($tables as $v){

Connect to the new database and create the tables with their structure:

$r = mysql_query(“SHOW CREATE TABLE `”.$v.”`”);
$d= mysql_fetch_assoc($r);
mysql_select_db(DB_DATABASE_NEW);
mysql_query($d['Create Table']);

Copy the data from original table:

$data = ”;
mysql_select_db(DB_DATABASE);
$r = mysql_query(“SELECT * FROM `”.$v.”`”);
while ($d= mysql_fetch_assoc($r))
$data[] = $d;

Paste the data into the corresponding table in the new database:

mysql_select_db(DB_DATABASE_NEW);
if (is_array($data))
foreach ($data as $d)
// and here you will insert the data “$d” into the table “$v”, as this is not truly related to this post, and you should learn from yourself too… :)

}