PHP Send Email Using Authenticated SMTP Mail Server In Real Time

Posted on in Categories Apache, Howto, lighttpd, Mail server, php, Postfix, Security, Troubleshooting last updated November 30, 2006

PHP has mail() function to send an email to users. However this mail() will not work:

=> If sendmail (or compatible binary) is not installed

=> If Apache Web server / Lighttpd running in chrooted jail

=> And your smtp server needs an authentication before sending an email

=> Or you just need to send email using PHP PEAR

In all these cases you need to use PHP PEAR’s Mail:: interface. It defines the interface for implementing mailers under the PEAR hierarchy, and provides supporting functions which are useful in multiple mailer backends. In this tip you will learn about how to send an e-mail directly to client smtp server in real time.

PHP Pear’s Mail.php is located in /usr/share/pear/ directory. Following is sample code to send an email via authenticated smtp server.

PHP send email using PHP SMTP mail Pear functions – Sample source code

Following code is well commented, you need to make necessary changes as per your setup.

<?php
include("Mail.php");
/* mail setup recipients, subject etc */
$recipients = "[email protected]";
$headers["From"] = "[email protected]";
$headers["To"] = "[email protected]";
$headers["Subject"] = "User feedback";
$mailmsg = "Hello, This is a test.";
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "smtp.mycorp.com";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "smtpusername";
$smtpinfo["password"] = "smtpPassword";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
?>

Sending smtp email from chrooted Apache or Lighttpd webserver

Read following section, if you are running a secure chrooted Apache or Lighttpd web server. I have already written about setting php mail() function in chrooted jail. If you are using chrooted jail server setup, copy all files from /usr/share/pear directory to /chroot-directory/usr/share/pear directory. For example if lighttpd chrooted jail located in /webroot directory, you need to type following commands to install PHP pear support:
# mkdir -p /webroot/usr/share/pear
# cd /webroot/usr/share/pear
# cp -avr /usr/share/pear .

If PHP SAFE MODE is on, you must set /webroot/usr/share/pear directory permission to webserver username to allow access. Otherwise you will see error as follows:

1-Nov-2006 09:43:19] PHP Warning:  main(): SAFE MODE Restriction in effect.  The script whose uid is 506 is not allowed to access /usr/share/pear/PEAR.php owned by uid 0 in /usr/share/pear/Mail.php on line 636.

So if webserver username is lighttpd or apache use following command to setup correct ownership:
# chown lighttpd:lighttpd /webroot/usr/share/pear -ROR# chown apache:apache /webroot/usr/share/pear -R

You may also find modified wordpress WP-ContactForm plugin useful. It is a drop in form for users to contact you. It can be implemented on a page or a post. Original authored by Ryan Duff, which use php mail() function to send email. I have modified the same to send email via my ISP authenticated gateway using PHP PEAR’s Mail:: interface 😀

Improve Apache (LAMP) security with mod_proxy

Posted on in Categories Apache, Howto, Security, Tuning last updated November 30, 2006

Virtual hosting with multiple user IDs is possible to increase Apache security with mod_proxy.

In this article, Nick Maynard outlines a method for you to improve the security of a LAMP setup by using Apache’s mod_proxy module. This article is specific to Linux; however, you can also apply some of the principles to other operating systems.

One challenge that faces LAMP administrators is that of providing all the features of a full PHP installation while ensuring a secure environment for all users of the system. Use of PHP’s safe mode is one technique for meeting this goal, but it can also unduly limit users, and some PHP applications simply don’t function with this facility enabled.

In this article, you’ll explore a method that improves the security of your LAMP installation by using the mod_proxy module and multiple back-end servers. I’ll discuss the merits and drawbacks of such an approach, and you’ll see an example configuration of a working setup.

Humor: The Book of Mozilla – Mozilla firefox Easter egg

Posted on in Categories Humor, Mozilla last updated November 30, 2006

Just open firefox and type about:mozilla followed by [enter] key…

You will see message:
And so at last the beast fell and the unbelievers rejoiced.But all was not lost, for from the ash rose a great bird. The bird gazed down upon the unbelievers and cast fire and thunder upon them. For the beast had been reborn with its strength renewed, and the followers of Mammon cowered in horror.

from The Book of Mozilla, 7:15


(click to enlarge image)

Heh

The Book of Mozilla is a well-known computer Easter egg found in the Netscape and Mozilla series of web browsers. The Easter Egg is viewed by having the browser go to the page about:mozilla.

Bjarne Stroustrup explains why software sucks (bad)

Posted on in Categories Beyond nixCraft, Links, News last updated November 29, 2006

In the 1980s and 90s, Bjarne Stroustrup designed and implemented the C++ programming language, which popularized object-oriented programming and influenced numerous other programming languages, including Java.

Technology Review has published an interview with Bjarne Stroustrup the inventor of the C++ programming language, defends his legacy and examines what’s wrong with most software code.

nixCraft FAQ roundup Nov / 29 / 2006

Posted on in Categories FAQ, Linux, UNIX last updated November 29, 2006

Recently updated/posted Linux and UNIX FAQ:

=> Why Linux usernames with capital letters not working or accepted by adduser command?

=> Linux Duplicate packages when querying the rpm databases

=> FreeBSD add a user to group

=> Monitor or sniff Apache http packets

=> Access Windows Shares from Linux

=> If sshd is restart will any connections be disconnected

=> How to detect ARP Spoofing under UNIX or Linux?

Linux desktop auto start or launch programs

Posted on in Categories Howto, Linux desktop, Tips last updated November 29, 2006

So how do you automatically start or launch program when you login into your KDE desktop system? For example if you would like to start program called xawtv (to watch tv) and mozilla-thunderbird mail client program.

This tip explains how to run programs to launch when you login into KDE or Gnome desktop systems.

KDE

If you are using KDE under any Linux distribution drop/copy program file to ~/.kde/Autostart directory. If your user name is tom create a soft link or copy binary in /home/tom/.kde/Autostart directory. Please note that the symbol ~ indicate the users home directory.

First change directory to ~/.kde/Autostart
$ cd ~/.kde/Autostart

Now create a symbolic link for xawtv program:
Type the following command to make a link (soft) to xawtv program:
$ ln -s $(which xawtv) .
Now create a link (soft) for mozilla-thunderbird mail client:
$ ln -s $(which mozilla-thunderbird) .

General syntax to create softlink to autostart program under KDE is as follows:
ln -s $(which linux-command-name) .

GNOME

Under GNOME Desktop system:
Open control center > Sessions > Startup Programs.

Now add your programs. Another option is to add programs in ~/.gnome2/session-manual file.

Finally you can also add program names to ~/.xinitrc file. This is useful for lightweight Window Manger such as Fluxbox or X session started by startx.
Open .xinitrc file:
$ vi ~/.xinitrc
Append program name:
/usr/bin/xawtv &

Save and close file.

Howto Setup Apache 2, MySQL 5, and PHP 5 (SAMP) for SUN Solaris 10 UNIX

Posted on in Categories Apache, Howto, MySQL, php, Solaris, UNIX last updated November 29, 2006

Many documents and blogs can be found on the Internet explaining how to use the Solaris 10 OS for a SAMP server (Solaris, Apache 2, MySQL, PHP). However, many of these articles are for older versions of the software packages or do not include the popular PHP language. Some of them lack any detail or examples to help you understand the process.

This article shows the technical user how to build their own up-to-date copy of PHP5 and integrate it with MySQL5 from Blastwave and the version of Apache2 included with the Solaris OS.

For this exercise, the installation will use the following software:

* Apache as delivered with the Solaris installation
* MySQL 5, from Blastwave.org, using pkg-get to install it
* The latest PHP 5 from php.net, downloaded and compiled

SAMP (Solaris, Apache 2, MySQL 5, and PHP 5) Setup for Solaris 10 OS and Solaris Express

BASH tips and tricks – power shell usage

Posted on in Categories Howto, Links, Shell scripting last updated November 28, 2006

If you really want to become # 1 at UNIX admin job 😉 or power user, shell is the key. Mastering shell, shortcuts and various commands result into good speed and improvements in productivity. If you type lots of things everyday this is a must read for you.

There is a nice presentation (slides) and PDF document available at GBdirect website.

Topic included

=> Tips for driving Bash better in everyday Linux use
=> Command history
=> Searching history
=> Keyboard shortcuts
=> Calling back keywords/commands and command arguments on fly
=> And much more

Download

=> Read online: Power Shell Usage – Bash Tips & Tricks
=> Download PDF version