I need to do some hacks on sending email using SMTP Authentication. So, I am looking for a good solution. Finally, I got very nice PHP component in PEAR. Let me say something about PEAR Package. PEAR stands for “The PHP Extension and Application Repository” and it is basically a framework and distribution system for reusable PHP components. The most important benefit is reusing the component in many applications. Installing PEAR is very easy in Ubuntu as well as Linux. Just follow me. .
In Ubuntu, you can install it from repositories. Type thin in terminal
sudo apt-get install php-pear
In other versions of Linux
wget http://pear.php.net/go-pear.phar php go-pear.phar
After installing it properly, you can check that weather it works or not. Just type
pear version
I should show you PEAR, PHP, Zend Engine versions and so on.
Now make test.php file with following PHP code in server
<?php require_once 'System.php'; var_dump(class_exists('System', false)); ?>
Then, save the file as test.php and run it. If it shows you: bool(true). Then, it’s okay. Your PEAR installing is done.
Now install Pear mail package. Type
sudo pear install mail sudo pear install Net_SMTP
Now write a PHP script for sending email. . .
<?php require_once "Mail.php"; $to = "Testo1 <[email protected]>"; $body = "Hi,\n\n this is just test email ? "; $headers = array( 'From' => 'Test <[email protected]>', 'To' => $to, 'Subject' => 'test email' ); $smtp = Mail::factory('smtp',array ( 'host' => 'ssl://smtp.googlemail.com', 'port' => '465', 'auth' => true, 'username' => 'test', //gmail user name 'password' => 'type your password' // gmail password )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo(" < p>" . $mail->getMessage() . " </p > "); } else { echo("<p > Message successfully sent!</p >"); } ?>
Done. Check you email account. There should be an email waiting for you.
Wow………….man that was quick! and worked like a charm………….thanks
Thanks Purohit. Your feedback will inspire me to give more technical blog posts.
Do you have a trick to avoid gmail to think that our sent email was forged?
The test mail of your example was systematically classed as SPAM.
Hi Louis,
I think, Gmail has some policies on this issue. These links might help you: http://stackoverflow.com/questions/370620/how-to-stop-gmail-from-marking-mails-sent-by-my-web-app-as-spam
and
http://support.google.com/mail/bin/answer.py?hl=en&ctx=mail&answer=1366858&expand=5
Think you very much, but there are possibility using pear whitout smtp?
missing a quote before “subject” in the headers.
Thank you. Fixed.