[HOWTO] Send emails(using mail function) from localhost in PHP through msmtp (using gmail account) on Linux

There’ve been lots of times when I wanted to use the mail() function on my local server. I am sure lots of you would’ve been wanting it too but most of you would’ve settled for PHPMailer or just used a web host to test the code instead. I myself had been doing the same until recently when I finally decided to do some research and get it to work.

Here are the steps in short for the geeks who like to do things on their own:
All I did was used smtp client called msmtp, configured it to work with my gmail account and configured PHP to use msmtp to send emails.

This tutorial is only applicable for linux users. I’ll write another article for windows users soon when I get my hands on a windows box.
All the commands used in the instructions are for Ubuntu, however you may use corresponding commands for your distro (for eg; you can use yum install instead of apt-get install on fedora, redhat, centos.

Step by step instructions:

  1. First of all, follow this tutorial:
    Install msmtp on your linux box and configure it to work with Gmail.Msmtp is highly configurable and you can easily configure it to work with any smtp server. If you wish to use any other service with msmtp, you may read its manual and configure it.
    Proceed to next step only if you have successfully configured and are able to send a test message.
  2. Open php.ini in your text editor.
    sudo gedit /etc/php5/apache2/php.ini

    Search for ‘sendmail_path’ and change it to look like

    sendmail_path = '/usr/bin/msmtp -t'

    Save the file and exit the text editor.
    Please note that your msmtp path may vary if you are not using Ubuntu. You may find the path to executable by:

    which msmtp
  3. Restart apache:
    sudo /etc/init.d/apache2 restart

    OR

    sudo /opt/lampp/lampp restart
  4. Everything’s done. Lets test if the mail() function is working now:
    if ( mail ( '[email protected]', 'Test mail from localhost', 'Working Fine.' ) );
    echo 'Mail sent';
    else
    echo 'Error. Please check error log.';

    Replace the [email protected] with your own username, save it into a php file along with php delimeters in your virtual host root folder and execute it through the browser. You should receive a mail.

Give yourself a pat on the back.

Some points to note:

  • If you weren’t able to send yourself a test mail in Step 1 ie. setting up msmtp, you should look at the debug information and search for solutions accordingly on the internet. Here’s the manpage of msmtp to help you with configuration
  • If you didn’t receive a mail in your inbox on the last step, check in your Spam folder. If its not there your should check the php error log for hint about what went wrong and act accordingly.
  • For any other questions, suggestions or appreciation feel free to use the comment box.

Cheers!

[HowTo] Configure msmtp to work with gmail on linux

Msmtp is a small but powerful and highly customizable smtp client. You can access gmail smtp using msmtp, which is exactly what I’ll teach in this tutorial.

Step by step instructions:

  1. Install msmtp and ca-certificates for use with SSL:
    sudo apt-get install msmtp ca-certificates
  2. We’ll create and edit the configuration file using Gedit:
    sudo gedit /etc/msmtprc

    Now copy and paste the following code in the text editor:

    defaults
    tls on
    tls_starttls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
     
    account default
    host smtp.gmail.com
    port 587
    auth on
    user [email protected]
    password mypass
    from [email protected]
    logfile /var/log/msmtp.log

    Needless to say, you have to replace [email protected] with your email and mypass with your password

  3. It should be readable by anyone. However, we’ll still execute the following command to be sure.
    sudo chmod 0644 /etc/msmtprc
  4. Your msmtp configuration is ready. Lets send a test email to yourself to see if everything’s working properly:
    echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" |msmtp --debug --from=default -t [email protected]

    Again replace [email protected] with your own email address and execute it. You should receive an email.

These are the exact commands to be used with Ubuntu.
You can replicate the steps for another distibution by using equivalent commands (For eg; yum install in place of apt-get install for centos, redhat, fedora.)
Cheers!

[HOW-TO] Back up all MySQL databases

To backup all or some of your MySQL databases, you’ll need mysqldump which comes bundled with mysql. If you have MySQL installed, you probably have mysqldump installed already.

To backup all databases use the following command:

In linux:

mysqldump -uroot -ppassword –all-databases | gzip > /media/disk-2/db.sql.gz

In Windows:

mysqldump -uroot -ppassword –all-databases > db.sql

This will backup all your databases from a single command.

To backup just a selected few use the following command:

In linux:

mysqldump -uroot -ppassword –databases db1 db2 | gzip > db1db2.sql.gz

In windows:

mysqldump -uroot -ppassword –databases db1 db2 > db1db2.sql

Replace db1 and db2 with your database names. You can add more databases separated by a space.

Replace ‘root’ and ‘password’ with your own mysql username and password.

Note that there is no space between ‘u’ and ‘root’ in -uroot, and between ‘p’ and ‘password’ in -ppassword

For more options please check out its man page if you are on linux or mysqldump –help which is available both in linux and windows.

It can’t get any simpler than that.

Cheers!

[How-To] Use facebook chat with pidgin

Now that facebook supports XMPP protocol for its chat, any messenger which supports XMPP would be able to use facebook chat without much effort.

Follow these steps to add your facebook chat to pidgin:

  • Start the pidgin and press Ctrl+A to bring the Accounts dialog box.
  • Click on Add.
  • Select XMPP as protocol from the drop-down menu.
  • Type in your username or your alias (the name you chose to to keep in url for your profile).
  • In ‘Domain’, type chat.facebook.com
  • In ‘Password’, type your facebook password.
  • Click on Advanced Tab, and uncheck ‘Require SSL/TLS’.
  • In ‘Connect Port’ type 5222.
  • In ‘Connect server’, type chat.facebook.com
  • Finally, click on Add.

This will add your facebook chat to pidgin.
Cheers!

[Solved] “Error saving media attachment.” error in WordPress

WordPress might give this error on uploading an image from media library. It means that wordpress isn’t able to save your file.

It is really easy to fix. However, this may not work for all:

Go to Settings->Media. Change the path in ‘Store uploads in this folder’ option to ‘wp-content/uploads’ (without the quotes).

If this doesn’t work you may have to change the permission of your wp-content/uploads directory to 0755. Read this to learn how to change directory permissions.

Cheers!

[How-To] Changing directory permissions from cPanel

This is not a difficult task but people often don’t know what directory permissions are. While we need not go into depth, just know 0755 is the recommended setting for all directories.

For changing a directories permission from cPanel, follow the following steps:

  • Login to your cPanel.
  • Find File Manager and click on it.
  • Select web root and click on Go.
  • Now place a check in the checkbox next to the directory you want to change the permission of.
  • Click on Change Permissions.
  • And select 755 in the box.
  • Finally, click on Change Permissions.

You’re done.

This would change the permission of selected directory to 755.

Cheers!

[HOW-TO] Restore recovery partition on Asus 1005HA

A friend of mine who has Asus 1005HA deleted his recovery partition to free up disk-space. He didn’t knew what the consequences could be. He felt the need of recovery just few days after it and he realised that he couldn’t use the recovery mode anymore. He gave me his netbook. It took me a while to figure out how to restore the partition.

What you require:

Another computer with DVD drive
USB flash drive (minimum 1GB) and another USB flash/hard drive(minimum 4.5GB)
PEtoUSB
ISO extraction software (winrar/7zip)
Any linux distro

Once you are setup with the requirements, follow these steps to restore:

    • Extract WINPE.iso to any folder on harddrive on second computer. Suppose this folder is D:\WINPE
    • Edit D:\WINPE\I386\SYSTEM32\WINPESHL.INI:

Change:
AppPath=x:\EPCRecover.exe
To:
AppPath=x:\GHOST32.EXE

    • Insert pendrive in the computer, and use PEtoUSB to make pendrive bootable. In Vista and Win7 please use admin mode.

Check ‘USB Removable’, and select the pen-drive.
Check ‘Enable Disk Format’.
Source Path To Built BartPE/WinPE Files: D:\WINPE
Check ‘Enable File Copy’ and select ‘Overwrite Always

    • Copy \Recovery\envir.GHO and \RECOVERY\envir.cmd from DVD to the root of your pen drive.
    • Convert your Pen-drive filesystem from FAT32 to NTFS by executing the following command in command-line:

convert x: /fs:ntfs
//change x to your pen-drive letter.

    • Disable the boot-booster on your 1005HA by pressing F2 two times as soon as you switch it on and disable it in BIOS.
    • Insert the pen-drive on 1005HA and press Esc repeatedly on bootup and select the flash drive to boot from. You’ll get into WINPE environment and Norton Ghost will start. Select to disk, from image and select envir.gho. This will repartition your complete hard-drive as it was when you purchased. Please note that this will destroy any data on your hard-drive. Make sure you back-up your data before doing this.
    • Download a linux distribution which recognises NTFS partitions. Use unetbootin to make the distribution USB bootable. Boot into live mode. I recommend Uubntu NBR for the purpose. Mount the partition labeled ‘PE’, this is your recovery partition.
    • On second computer copy \Recovery from the DVD to a larger pen-drive/hard-drive. Connect this drive to 1005HA and copy all the contents in \Recovery from your pen-drive to root of PE.
    • You can also edit the WINPESHL.INI to look like this:

AppPath = x:\minint\system32\recover.exe

This is it. Restart your computer.

Cheers!!