Sample Code on how to send email using PHP

Part of what makes the PHP mail() function is so simple is its lack of flexibility. Most importantly and frustratingly, the stock mail() does not usually allow you to use the SMTP server of your choice, and it does not support SMTP authentication, required by many a mail server today, at all.

Fortunately, overcoming PHP's built-in shortcomings need not be difficult, complicated or painful either. For most email uses, the free PEAR Mail package offers all the power and flexibility needed, and it authenticates with your desired outgoing mail server, too

 

                                                        
1 php
2 require_once "Mail.php";
3
4 $from = "Sandra Sender ";
5 $to = "Ramona Recipient ";
6 $subject = "Hi!";
7 $body = "Hi,\n\nHow are you?";
8
9 $host = "mail.yourdomain.com";
10 $username = "emailusername@domainname.com";
11 $password = "email password";
12
13 $headers = array ('From' => $from,
14 'To' => $to,
15 'Subject' => $subject);
16 $smtp = Mail::factory('smtp',
17 array ('host' => $host,
18 'auth' => true,
19 'username' => $username,
20 'password' => $password));
21
22 $mail = $smtp->send($to, $headers, $body);
23
24 if (PEAR::isError($mail)) {
25 echo("

 

" . $mail->getMessage() . "

");
26 } else {
27 echo("

Message successfully sent!

");
28 }
29 ?>
30
31

Note :
Change the host with your mail.yourdomain.com
Username --> Change with your email account in this format (email@domain.com).
Password-> Your email password.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Query an MS SQL Database with PHP using DSNLess Connection

The information in this article applies to: PHP MS SQL 2000 MS SQL 2005...

Query MySQL Database in PHP

1 <HTML> 2 <HEAD> 3 4...