Roundcube Community Forum

Third Party Contributions => Old Style Plug-Ins => Topic started by: namit on October 19, 2006, 02:55:52 PM

Title: Import address book from csv file
Post by: namit on October 19, 2006, 02:55:52 PM
Pass down the name of the file you want
And the users id that is in the sql table

csv layout
Name, Email

Hope it helps ya all.

Code: [Select]
<?php

$dbhost 
'localhost';
$dbuser 'root';
$dbpass 'password';
$conn mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
$dbname 'roundcubemail';

mysql_select_db($dbname$conn);
echo 
mysql_errno($conn) . &quot;: &quot; . mysql_error($conn). &quot;\n&quot;;

///Fin connection

$user_id $_GET['user_id'];
$file $_GET['file'];

if(
$user_id == &quot;&quot; || $file == &quot;&quot;)
{
echo &quot;You have no user id $user_id<br />OR files&quot;;
}
if(
$user_id != &quot;&quot; || $file != &quot;&quot;)
{

echo 
$user_id;
echo &
quot;<br />$file&quot;;

$row 1;
$handle fopen($file, &quot;r&quot;);
echo &
quot;$handle&quot;;

while ((
$data fgetcsv($handle1000, &quot;,&quot;)) !== FALSE)
{

echo &quot;<br />&quot;;

$row++;

$name $data[0];
$email $data[1];

echo &quot;<b>Name: </b>&quot; . $name. &quot;\n&quot;;
echo &quot;<B>Email:</B> &quot; . $email . &quot;\n&quot;;

$query = &quot;INSERT INTO `contacts` ( `contact_id` , `user_id` , `changed` , `del` , `name` , `email` , `firstname` , `surname` , `vcard` ) VALUES '''$user_id''1985-09-15 00:00:00''0''$name''$email''''''' );&quot;;
echo &quot;<br />&quot;;

mysql_query($query$conn);
echo mysql_errno($conn) . &quot;: &quot; . mysql_error($conn) . &quot;\n&quot;;

}

fclose($handle);

}


?>
Title: Re: Import address book from csv file
Post by: CReber on October 30, 2006, 04:14:26 PM
Can you give me a sample of the csv file? Don't wanna crash the db ;)

Thanks!
Title: Re: Import address book from csv file
Post by: namit on October 30, 2006, 05:46:39 PM
csv layout

Name, Email
Title: Re: Import address book from csv file
Post by: Nonsense on October 30, 2006, 06:16:48 PM
May seem like a stupid question, but where exactly (in which lines) do I insert username and file?
Title: Re: Import address book from csv file
Post by: namit on October 30, 2006, 07:25:03 PM
well you will have on your webserver the csv file then you will have

http://yourwebsite.com/cvsupload.php?user_id=THE_USER_ID_THAT_U_HAVE_IN_SQL_TABLE&file=FILE_NAME
Title: Re: Import address book from csv file
Post by: CReber on October 30, 2006, 08:57:52 PM
link doesn't work :P just quote an exact sample of a csv file you would recommend! only 3 lines...

thanks anyway....
Title: Re: Import address book from csv file
Post by: namit on October 31, 2006, 03:12:15 AM

Create a directory on your server called cvsupload.php
upload the cvs file to your server
format is
bob,boobs@boobs.com
chris noen,namit@something.org

create a file called cvsupload.php in the direcotry csv-upload.

find in the sql tables the userid within the roundcube tables then replaec that iin the link below.

replace yourwebsite.com with your website
http://yourwebsite.com/cvsupload.php?user_id=THE_USER_ID_THAT_U_HAVE_IN_SQL_TABLE&file=FILE_NAME
Title: Re: Import address book from csv file
Post by: Nonsense on October 31, 2006, 03:37:14 AM
That didn't work.

Code: [Select]
0: user_id (my emailaddress)
file.csvResource id #3
Name: Name Name Email: email
0:
Name: Name Name Email: email
0:
Title: Re: Import address book from csv file
Post by: CReber on October 31, 2006, 06:45:37 AM
same like me! the script only imports the first 3 contacts and then it stops...

Code: [Select]
0: 1
contacts.csvResource id #3
Name: Berlin - Michael Email: [email]michael@berlin.de[/email]
0:
Name: Dublin - Billy Email: [email]billy@dublin.com[/email]
0:
Name: Dublin - William Email: [email]william@dublin.com[/email]


The output log seems ok, so what else can that be?
Title: Re: Import address book from csv file
Post by: namit on October 31, 2006, 07:19:45 AM
Can you check the sql tables and see if the are being entered?

You also have to logout and then login to roundcube.
Title: Re: Import address book from csv file
Post by: Nonsense on October 31, 2006, 09:13:07 AM
Solved.
It was a typo.

Thanks.
Title: Re: Import address book from csv file
Post by: namit on October 31, 2006, 09:27:52 AM
in my code or your code? if it was my code give it to me and i will change my code.
Title: Re: Import address book from csv file
Post by: Nonsense on October 31, 2006, 09:52:44 AM
My code. So don't worry.
Title: Re: Import address book from csv file
Post by: rantdepot on November 06, 2006, 04:00:24 PM
Worked well, thanks! 8)
Title: Re: Import address book from csv file
Post by: klemen on May 16, 2007, 03:06:22 AM
I have an csv file format:

firstname,surname,name,,name@email.com,,,,,,,,,,,,,,,,,,,,,,,

I get surname where I need email. How to edit the script to get right insert.

thanks.
Title: Re: Import address book from csv file
Post by: klemen on May 16, 2007, 03:54:06 AM
I figured it out, if anyone need it:

    $firstname = $data[0];
           $surname = $data[1];
           $name = $data[2];
           $email = $data[3];

   echo "name: " . $firstname. "\n";
   echo "surname: " . $surname. "\n";
   echo "name:" . $name. "\n";
   echo "email:" . $email. "\n";

 $query = "INSERT INTO `contacts` ( `contact_id` , `user_id` , `changed` , `del` , `name` , `email` , `firstname` , `surname` , `vcard` ) VALUES ( '', '$user_id', '1985-09-15 00:00:00', '0', '$name', '$email', '$firstname', '$surname', '' );";
   echo "
";
Title: Re: Import address book from csv file
Post by: FlexV on October 01, 2007, 08:20:19 PM
Thanks a lot!
Works perfektly! ;D
I save one hour or more !
Thanks