Skip to main content

PHP Send Email Using Authenticated SMTP Mail Server In Real Time


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 = "feedback@yourdot.com";
$headers["From"] = "user@somewhere.com";
$headers["To"] = "feedback@yourdot.com";
$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 :D

Comments

Popular posts from this blog

Sending Email in Android using JavaMail API without using the default android app(Builtin Email application)

I find a way sending a mail in android using the JavaMail API using Gmail authentication Steps to create a simple Project: MailSenderActivity.java YOUR PACKAGE ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . Button ; public class MailSenderActivity extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate ( Bundle savedInstanceState ) {         super . onCreate ( savedInstanceState );         setContentView ( R . layout . main );         final Button send = ( Button ) this . findViewById ( R . id . send );         send . setOnClickListener ( new View . OnClickListener () {             public void onClick ( View v ) {       ...

Getting Lat and Lon from Address in Google Maps Using PHP

Recently, I am working on an application where I have to convert addresses to their corresponding Lat and Lon. So, I decided to write on how to get Latitude and Longitude in Google Maps using only PHP. To use the technique explained below, you need to have a  Google Maps Key . The idea is simple. Make a request to Google Maps server, and it will return an XML (or JSON). Then, parse the XML to get the Lat and Lon. I use DomDocument and DomXPath for processing it. Below I have used the address:  100 City Centre Dr., Mississauga, Ontario . 1. $key    =  "....." ;  // your Google Maps key 2.   3. $address   = urlencode( '100 City Centre Dr., Mississauga, ON' ); 4.   5. $url   =  ' http://maps.google.com/maps/geo?q= ' . $address . '&output=xml&oe=utf8&key=' . $key ; 6. $dom   =  new   DomDocument(); 7. $dom ->load( $url ); At this point, the XML is loaded into the $dom. Now, it...

Reversing Digits of a 32-Bit Integer in Java

  Have you ever wondered how to reverse the digits of a signed 32-bit integer in Java? Reversing the digits of an integer is a common programming task, and it's essential to handle it correctly, especially when dealing with constraints like the 32-bit integer range. In this blog post, we'll walk you through how to achieve this task in Java, ensuring that you handle potential integer overflow. The Challenge The challenge is to reverse the digits of a given 32-bit integer x while maintaining the constraints of the problem. In Java, an int can hold 32 bits, with values ranging from -2^31 to 2^31 - 1. Reversing the digits should be done while considering both positive and negative integers and avoiding integer overflow. The Java Solution To reverse the digits of a 32-bit integer in Java, you can follow these steps: java Copy code public int reverse ( int x) { // Initialize variables to store the result and check for the sign of x int result = 0 ; int sign = ...