How to connect to an outgoing SMTP server from a PHP script
Sending an email from a PHP script is easy, rapid, and clean… If it works!
Part of what makes the PHP mail() function so easy is its loss of flexibility, however one trouble with that is that the inventory PHP mail() would not usually let you use the SMTP server of your choice, and it does not aid SMTP authentication.
Fortunately, overcoming PHP’s integrated shortcomings is not hard. For maximum e-mail users, the free PEAR Mail bundle gives all the strength and flexibility needed, and it authenticates with your desired outgoing mail server. For more desirable safety, encrypted SSL connections are supported for sending mail using PEAR Mail as nicely.
How to Send Email From a PHP Script With SMTP Authentication
To begin, install the PEAR Mail package. Typically, this could have already been accomplished for you with PHP 4 and later, however if you’re no longer certain if you already have it, pass in advance and deploy it.
Copy this code:
<?php
require_once "Mail.php";
$from = "Sandra Sender <[email protected]>";
$to = “
Ramona Recipient <[email protected]>";
$subject = “Hi!”; $body = “Hi,\n\nHow are you?”; $host = “
mail.example.com";
$username = “
smtp_username";
$password = “
smtp_password";
$headers = array (‘From’ => $from, ‘To’ => $to, ‘Subject’ => $situation); $smtp = Mail::factory(‘smtp’, array (‘host’ => $host, ‘auth’ => true, ‘username’ => $username, ‘password’ => $password)); $mail = $smtp->ship($to, $headers, $frame); if (PEAR::isError($mail)) echo(“
” . $mail->getMessage() . “”); else echo(“
Message efficaciously despatched!”);
?> Locate all the ambitious textual content in our example and alternate those regions of the script to whatever is applicable to you. Those are the best areas which you must change in order for the PHP script to work, however additionally make certain to alter the problem and body text, too.
- From: The email cope with from that you need the message to be sent
- to: The recipient’s e mail address and call
- host: Your outgoing SMTP server call
- username: The SMTP username (normally the same as the username used to retrieve mail)
- password: The password for SMTP authentication
Note:The above instance is of a PHP script that sends an electronic mail with SMTP authentication however with out SSL encryption. If you need encryption as properly, use this script instead, again, swapping the formidable text along with your facts.
<?php
require_once "Mail.php";
$from = "Sandra Sender <[email protected]>";
$to = “
Ramona Recipient <[email protected]>";
$subject = “Hi!”; $body = “Hi,\n\nHow are you?”; $host = “
ssl://mail.example.com";
$port = “
465";
$username = “
smtp_username";
$password = “
smtp_password";
$headers = array (‘From’ => $from, ‘To’ => $to, ‘Subject’ => $subject); $smtp = Mail::factory(‘smtp’, array (‘host’ => $host, ‘port’ => $port, ‘auth’ => true, ‘username’ => $username, ‘password’ => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo(“<p>” . $mail->getMessage() . “</p>”); } else { echo(“<p>Message successfully sent!</p>”); } ?>