nixCraft Poll

Topics

PHP Send Email Using Authenticated SMTP Mail Server In Real Time

Posted by Vivek Gite [Last updated: April 18, 2008]

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

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Loren Dorez Says:

    Im currently working on a PHP Mail File.. Essentiall it needs to log into a webbased mail account like ‘Outlook Web Access’. All tutorials i see you a submit button to the caling and send of the php file. Can i use the onclick evern like such

    onClick=”send_Email.php3″

    or must i use a sbumit button for the form. A submit buttons doesnt work with the current setup but i could model it to if it is neccessary..

  2. subhendu das Says:

    i need help for windows environment.
    i use php triad 4.0.1

  3. subhendu das Says:

    How i configure smtp server.
    i need help for windows environment.
    i use php triad 4.0.1

  4. nixcraft Says:

    subhendu,

    I don’t use Windows so I have no idea about Windows SMTP server configuration.

  5. gurjeet singh Says:

    hi i got this error

    Warning: main(Mail.php): failed to open stream: No such file or directory in /home/meresaja/public_html/mailtest.php on line 2

    Fatal error: main(): Failed opening required ‘Mail.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/meresaja/public_html/mailtest.php on line 2

    any body help me

  6. niteen Says:

    you need to install pear in your server

    http://pear.php.net/package/Mail

  7. Vincent Says:

    Ok I would like to hide the SMTP authentication from the web directory i.e. put it out of the path of public html, how would I call the functions for authentication in this case, also I want to e.mail the details of a form before it is posted or as it is being posted…..

  8. nixcraft Says:

    PHP is server side technology; as long as it is configured properly no one is going to see your SMTP authentication information.

  9. Sia.G Says:

    Ok so my mailtest.php seems to have worked as I get my confirmation message. I just never receive the e-mail. ANyone know what’s going on?

  10. Jackie Says:

    Hello,
    I’ve been trying to send emails using the PHP mail() function but it looks like the mails are not being sent. What do I need to configure on my windows server to get it to work?

  11. Mateus Pezzin Says:

    Thank’s a lot!

    This sample is awesome!

    I used with Mime to send attachments in the mail.

  12. Kiran S Says:

    Hi,
    I am not that good with php..its working when done stand alone.. i customized the code given by u to fit my text fields.. but didn’t work.. cud you give me some tips so that i can make it fit for my site

    Kiran S

  13. MANOJ Says:

    Hello,
    I’ve been trying to send emails using the given php code in file named contact.php
    Its showing error

    Parse error: syntax error, unexpected ‘@’ in /home/paragpap/public_html/contact.php on line 5

    What do I need to configure on my linux server to get it to work?

    Please help…

    Thanks

  14. mazen Says:

    Hi
    any download zip file ?
    Thanks

  15. ehmeidan Says:

    thanks for this thing

  16. Uma Mahesh Says:

    i downloaded Mail package that contain a folder with Mail.php, Mail/*.php.
    the code testmail.php was in the folder of Mail.php and when i execute it is asking for PEAR.php
    i could not finid it.
    Plz tell me how to install Pear on the server to
    echo_mahesh@yahoo.co.in

  17. Jake Says:

    can i check with you guys,
    i have a sever currently installed with Apache,

    i written a script in php to send emails out.
    what do i need to install other than:
    PHP5 and SMTP mail server?

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , , , , ,

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.