Export Contact script

Hi guys i was thinking this is possible create a script that runs with a cron job to export the contact in SuiteCRM like if i manually do that (same format etc) but using a script that sends the result to an email address for upload to MailChimp or any other app

Hello,
yes it’s simple… look my post here https://suitecrm.com/forum/developer-help/5928-integrate-phpoffice-in-suitecrm

What you need, is just make a scheduler and send result with mail.
Sorry, can give at this moment a sample scheduler script,

Regards

1 Like

Oh cool, you think you can write the script please for the community… went you have some free time of course

Hi.
Like so

file : custom/modules/Schedulers/Ext/SchedulerTasks/exportto.php


$job_strings[] = 'ExportToJob';

function ExportJob() {
    include('custom/modules/Accounts/ExportTo.php');
    upExportTo();
    return true;
}

file : custom/modules/Schedulers/Ext/Language/en_us.ext.lang.php


$mod_strings['LBL_EXPORTTOJOB'] = 'Export To JOB';

custom/modules/Accounts/ExportTo.php




function upExportTo(){
    global $db;
    your SQL
  $result = $GLOBALS['db']->query($sSQL);
		
while($row = $GLOBALS['db']->fetchByAssoc($result) ){

  here you can find script for CSV on php.net
}

here you can send email... with csv attachment on php.net

}

Regards


Im lost mate can you guide me this are custom modules or what…

I just want to Export all my contact every week automatic to a email address

Loolll,

just for that, i will do that for this week (before monday)

Please, give field to export… and what format… give give all info… i am not medium !!!
We can not export “description” field because carriage return…

Let go, i am not medium, not find another video… give information… :slight_smile:

:slight_smile:

I want to export all the contacts that i have in SuiteCRM like if was using the SuiteCRM Contact Export option i attach an image of the SuiteCRM Export Option.

Here,
You have all info. Just you need to debug it.
Dont forget to make quick repair rebuild and go to scheduler module to add new job.
use $GLOBALS[‘log’]->fatal(""); for debug.

Regards


File : custom/Extension/modules/Schedulers/Ext/Language/en_us.lang.ext.php
Content :
<?php
$mod_strings['LBL_EXPORTCONTACTSJOB'] = 'Export Contacts JOB';


File : custom/Extension/modules/Schedulers/Ext/SchedulerTasks/ExportContacts.php
Content :
<?php

$job_strings[] = 'ExportContactsJob';

function exportContactsJob() {
    include('custom/modules/Contacts/ExportContacts.php');
    ExportContacts();
    return true;
}

File : custom/modules/Contacts/ExportContacts.php
Content :

<?php
include_once('include/SugarPHPMailer.php');

function ExportContacts(){
    
    $oContact = BeanFactory::newBean('Contacts');
    $oContacts = $oContact->get_full_list();
    $csv = '';
    foreach ($oContacts as $oCon) {
    	$csv .= '"' .$oCon->first_name .'";"' .$oCon->last_name .'";"' .BeanFactory::getBean('Users', $oCon->assigned_user_id) .'"\n'; 		
    }

    $temp_file = tempnam(sys_get_temp_dir(), 'tmp.csv');
    file_put_contents($temp_file, $csv); 
    sleep(1);  
	$mail = new SugarPHPMailer();
	$mail->From = "no-reply@example.com";
	$mail->FromName = "Example Company";
	$mail->ClearAllRecipients();
	$mail->ClearReplyTos();
	$mail->AddAddress('johndoe@email.com', 'John Doe');
	$mail->Subject = "Welcome";
	$mail->Body_html = "<b>html body</b>";
	$mail->Body = "text body";
	$mail->isHTML(true);
	$mail->AddAttachment($temp_file, 'contacts.csv', 'base64', 'text/csv');
	$mail->prepForOutbound();
	$mail->setMailerForSystem();

    //readfile($temp_file); 

    if (!$mail->Send()) {
    	$GLOBALS['log']->fatal("ERROR: Contacts Export Mail sending failed!");
	}
	unlink($temp_file);
}


Hi,

Scheduler is complex to debug, here a code who work perfectly :

file : custom/include/MVC/Controller/entry_point_registry.php
content :


$entry_point_registry['test'] = array('file' => 'custom/test.php', 'auth' => false);

file : custom/test.php
content :


<?php
include_once('include/SugarPHPMailer.php');
include_once('modules/Contacts/Contact.php');
include_once('modules/Users/User.php');

   
    $oContact = BeanFactory::newBean('Contacts');
    $oContacts = $oContact->get_full_list( );
    
    
    $csv = '';
    foreach ($oContacts as $oCon) {
    	$csv .= $oCon->first_name .'\n'; 		
    }

    $temp_file = tempnam(sys_get_temp_dir(), 'tmp.csv');
    file_put_contents($temp_file, $csv); 
    sleep(1);  
	$mail = new SugarPHPMailer();
	$mail->From = "no-reply@exemple.com";
	$mail->FromName = "it me";
	$mail->ClearAllRecipients();
	$mail->ClearReplyTos();
	$mail->AddAddress('tome@gmail.com', 'it's me');
	$mail->Subject = "Welcome";
	$mail->Body_html = "<b>html body</b>";
	$mail->Body = "text body";
	$mail->isHTML(true);
	$mail->AddAttachment($temp_file, 'contacts.csv', 'base64', 'text/csv');
	$mail->prepForOutbound();
	$mail->setMailerForSystem();

    //readfile($temp_file); 

    if (!$mail->Send()) {

    	$GLOBALS['log']->fatal("ERROR: Contacts Export Mail sending failed!");
	}

	unlink($temp_file);


?>

And you can receive email with this url index.php?entryPoint=test

Hi,
All you need is adapt just here : (but CSV file dont like carriage return in field content !!)


   $csv = 'PUT HEADER HERE';
    foreach ($oContacts as $oCon) {
    	$csv .= $oCon->first_name .PUTALLFIELDHEREWITH"AND; ."\n"; 		
    }

Ok good it but how i make the CSV file look like this
[table]
[tr]
[td]First Name[/td]
[td]Last Name[/td]
[td]Email[/td]
[/tr]
[tr]
[td]John[/td]
[td]Done[/td]
[td]johndoneATmail.com[/td]
[/tr]
[/table]

Not easy forum for code writing.

$csv = ‘“FRIST NAME”;“LAST NAME”;’;
foreach ($oContacts as $oCon) {
$csv .= ‘"’ .$oCon->first_name .’";"’ .$oCon->last_name .’";’ ."\n";
}

item i follow you advise
and i modify the code to the follow

    $csv = '"FRIST NAME";"LAST NAME";"ACCOUNT";"EMAIL";;';
foreach ($oContacts as $oCon) {
$csv .= '"' .$oCon->first_name .'";"' .$oCon->last_name .'";"' .$oCon->account_name .'";"' .$oCon->Contacts0emailAddress0.'";' ."\n";
}

But i only get FIRST NAME, LAST NAME column fill with information i attach a print screen

This for get email

$sea = new SugarEmailAddress(); 

foreach ($oContacts as $oCon) {

 $sea->getPrimaryAddress($oCon);  =>> CONTACT PRIMARY EMAIL ADRESS
 $user = BeanFactory::getBean('Users', $oCon->assigned_user_id);
 $user->user_name;  =>> USER_NAME

}

For Account name, you can try as user, but relationship Accounts - Contacts is Many@Many… then witch Account ???
I dont know how export script handle this.
Same for Email, it’s ONE2MANY relationship… but with sample, we get only primary adress.
As you can imagine, this is a BUGGY export with account name and email address.

For some reason, dont work the script now if i add the code above in custom/modules/Contacts/ExportContacts.php

<?php
include_once('include/SugarPHPMailer.php');

function ExportContacts(){
    
    $oContact = BeanFactory::newBean('Contacts');
    $oContacts = $oContact->get_full_list();
    $csv = '"FRIST NAME";"LAST NAME";"ACCOUNT";"EMAIL";;';
foreach ($oContacts as $oCon) {
$csv .= '"' .$oCon->first_name .'";"' .$oCon->last_name .'";"' .$oCon->Contacts0emailAddress0 .'";' ."\n";
}

$sea = new SugarEmailAddress();

foreach ($oContacts as $oCon) {

$sea->getPrimaryAddress($oCon); =>> CONTACT PRIMARY EMAIL ADRESS
$user = BeanFactory::getBean('Users', $oCon->assigned_user_id);
$user->user_name; =>> USER_NAME

}

    $temp_file = tempnam(sys_get_temp_dir(), 'tmp.csv');
    file_put_contents($temp_file, $csv); 
    sleep(1);  
	$mail = new SugarPHPMailer();
	$mail->From = "hello@domain.com";
	$mail->FromName = "Example Company";
	$mail->ClearAllRecipients();
	$mail->ClearReplyTos();
	$mail->AddAddress('user@domain.com', 'John Doe');
	$mail->Subject = "Welcome";
	$mail->Body_html = "<b>html body</b>";
	$mail->Body = "text body";
	$mail->isHTML(true);
	$mail->AddAttachment($temp_file, 'contacts.csv', 'base64', 'text/csv');
	$mail->prepForOutbound();
	$mail->setMailerForSystem();

    //readfile($temp_file); 

    if (!$mail->Send()) {
    	$GLOBALS['log']->fatal("ERROR: Contacts Export Mail sending failed!");
	}
	unlink($temp_file);
}

My code work, you have 0 skill in php…

foreach ($oContacts as $oCon) {

$sea->getPrimaryAddress($oCon);
$user = BeanFactory::getBean(‘Users’, $oCon->assigned_user_id);

$csv .= ‘"’ .$oCon->first_name .’";"’ .$oCon->last_name .’";"’ .$user->user_name .’";"’ .$sea->getPrimaryAddress($oCon).’";’ ."\n";

}


I know I don’t know to code sorry mate…
I till can’t put this to work…

im not a coder or something like that im just a marketing and sales guy @item