Code Blog

Displaying 36-40 of 46 results.
2016/02/15 SugarCRM

My aim was to create an automatic weekly status update email for SugarCRM CE. The CE version doesn't come with this functionality built-in but it's still quite easy to create a script in PHP and add a task in Windows "task scheduler" to send out the email at a specific time and interval.

This is how I did it. First you need to create the following three files in your SugarCRM CE installation folder (relative file paths given)

  1. custom/scripts/Weeklyemail.php
  2. custom/extensions/modules/schedulers/ext/ScheduledTasks/WeeklyEmailJob.php
  3. custom/extensions/modules/schedulers/ext/ScheduledTasks/language/en_us.WeeklyEmailJob.php

The Weeklyemail.php is the part which will actually create the content for the email to be sent out. I did this by running some database queries to generate the content from the CRM database (here you may put in whatever is relevatant for your business i.e. latest Opportunities, Sales statistics etc).

SugarCRM uses the PHPMailer library for sending emails. It is an objectoriented PHP library with support for SMTP. Here's a code example in SugarCRM for creating the mail object: 

$emailobj = new Email();
$defaults = $emailobj->getSystemDefaultEmail(); 
$mail = new SugarPHPMailer();

In this setup the outgoing email is sent via the local Exchange mail server (ip address in example above, port 587 with TLS). For this to work I had to enable "extension=php_ldap.dll" in php.ini. The username and password didn't carry over correctly so I set it again explicitly in the code.

I then set up a "Scheduled task" in Windows Server 2008 r2, see screenshots below. I create a new task in "Control panel -> Administrative Tools -> Task Scheduler". The process is quite similar for any Windows system.


Need to specify the directory for php.exe process to run in, it should be the root of the SugarCRM installation, otherwise it cannot find the cron.php script file.

2016/02/15 MacOSX

I have been running into problems with my external HDD (Toshiba 1TB) when I've been in a hurry (read: too lazy) to properly eject the drive from my MacBook Air. The problem is when I plug the drive back in it just won't reappear in the finder window and even rebooting the Mac OS often won't solve the problem (it worked sometimes). This was getting rather annoying wasting a lot of time rebooting the system and still not being able to access the external HDD.
I noticed that the system has actually found the drive but did not re-attach it (mount it); I would guess this is because the OS detected a potential error and wants to protect the disk data. I found it easiest to mount the drive directly from the command line to solve this problem.

Open terminal window and run:

diskutil list

The diskutil will list all HDDs attached to your system. For my system /dev/disk0 is the internal physical drive, and /dev/disk1 is an internal virtual drive.

/dev/disk2 (external, physical):
   #:       TYPE NAME                      SIZE        IDENTIFIER
   0:       FDisk_partition_scheme         *1.0 TB     disk2
   1:       Windows_NTFS TOSHIBA EXT        1.0 TB     disk2s1

Here the diskutil listed the external HDD I want to mount with identifier /dev/disk2s1 (Windows_NTFS file partition type, filesystem is exFAT). The "s" stands for slice, which means the same thing as partition.

diskutil info /dev/disk2s1

Run the "info" option to list information about the device. It will show if it is mounted and the mount point. It also lists the filesystem used; I use exFAT for my Mac/Windows external drive.

diskutil unmount /dev/disk2s1

Here the output should be: "was already unmounted"

diskutil mount /dev/disk2s1

If output is "Volume on disk2s1 timed out waiting to mount" then try again using the "force" flag i.e. "diskutil mount force /dev/disk2s1". This used to work great for me in the past, but after recent Mac OS X updates this option seems to have been dropped from the diskutil command utility! Now I just have to wait ~5 min and after that I can mount the disk again.

On a side note "diskutil activity" may be useful to debug problems when mounting/unmounting partitions (in another terminal window). The man doc writes: "Continuously display system-wide disk manipulation activity ... This can be useful to watch system-wide activity of disks coming on-line or being ejected, volumes on disks being mounted or unmounted, volumes being renamed, etc.".

Another useful function of diskutil is the "diskutil secureErase level device" command which will zero out the entire HDD using a secure method (the "level" option selects which method to use). Save that for another time.

2016/02/15 Programming

I used to be an Emacs user, but switched to use Sublime Text the last few years. In any case I never got too familiar with the vim editor. However, vim editor is still quite useful as a terminal tool for editing as its pretty much always available. This time I needed to do some hex edit of a binary file but needed to look up some commands how to actually use it, see notes below.

Hex edit in Vi / Vim

Change into hex edit mode :%!xxd
Revert from hex mode :%!xxd -r
Global search and replace of string1 to string2 :g/string1/s//string2/g 

I noticed though that after turning into hex edit mode, the regexp will match all characters, so need to pay attention if one wants to match a sequence of 3 hex chars, as "1B04 05" will not match "1B 0405".

Btw, the difference between vi and vim is that vi is the standard pre-installed editor on Unix systems (first release dating back to 1976!), and vim (short for vi improved) is a cloned version with more functions than vi (originally written for the Amiga!).

Insert before the cursor (insert mode) i
Exit insert mode Esc
Delete from cursor to end of the line D
Write and quit :wq
Quit and don't save :q!

For more commands for example go to Vim Cheat Sheet (external link)

Run "vitutor" or "vimtutor" in the terminal will start vi/vim and load a beginners guide tutorial into the editor.

2016/02/15 Programming,Sublime-Editor

It's difficult to remember too many shortcuts, especially as i'm not using Sublime Text everyday, so I try to keep the list short. It's useful to lookup shortcuts via the command palette when I've forgotten something. A note on the documentation: the "super" key is the cmd key on the mac.

A Few Useful Shortcuts

cmd D mark word / multiple cursor
cmd shift P command palette
cmd P goto anything (add @ to search for symbols)
cmd R find symbol
cmd alt n advanced new file (need to first install the extension)
shift cmd [ or ] move between open files / tabs

Example of combined search:

cmd P “view blog form @functionname ”

This will search a file with the filepath matching for example, "view/blog/form.php", and it will open the first file that matches the search criteria and then also search for the "functionname" symbol inside the file!


Mac finder menu

On the mac its also nice to have a command in the left click menu in finder, to easily let Sublime Text read in a folder or file (the file structure will appear in the side bar of Sublime)

Go to Automator and create new command

Shell command

Create a "bin" directory for local user and create "subl" symbolic link to Sublime Text.

ln -s /Applications/Sublime\ .

in .bash_profile in home directory add the following line:

export PATH="~/bin:$PATH"

Show Hidden files in file dialog

Press ⌘ cmd + ⇧ shift + . to toggle show/hide hidden files.

2016/02/15 SugarCRM

In SugarCRM CE (the free Community Edition) there is no field for "Account name" for projects. However there already exists a relationship between Account and Project. But it is of type "Many-to-Many" and for my purpose "One-to-Many" Account/Project relationship would been more suitable.