Scenario:
How do I set up outbound SMTP using PEAR::Mail?
Solution:
The following example of code shows you how to send an email using "MX Outbound" as the SMTP SmartHost / mail relay using the PEAR::Mail module for php.
Note "MX Outbound" is an authenticated SMTP service so the example below shows authentication in php as well. The code below uses the default SMTP port 25 but it can easily be changed to an alternative SMTP port by editing the line code $mail_params['port']
.
Example code smtp-test.php
<html>
<head>
<title>SMTP Test using PEAR::Mail in PHP</title>
</head>
<body>
<?php
require_once('Mail.php');
// set the recipients (to) address [REQUIRED]
$mail_to = This email address is being protected from spambots. You need JavaScript enabled to view it.';
// set the mail headers which include sender address, subject, etc
$mail_headers['From'] = This email address is being protected from spambots. You need JavaScript enabled to view it.';
$mail_headers['To'] = $mail_to;
$mail_headers['Subject'] = 'Test message';
// set the mail content
$mail_body = "The test messsage is having this body line inside.";
// set the smtp mail server [REQUIRED]
$mail_params['host'] = 'mxoutbound.mxtools.co.uk';
$mail_params['port'] = 25; // Can use alternative ports if needed (See below)
$mail_params['auth'] = 'PLAIN';
$mail_params['username'] = 'mxoutbound-username';
$mail_params['password'] = 'mxoutbound-password';
// optional SMTP debug options
// $mail_params['debug'] = true;
// Create the mail object using Mail::factory
$mail_object =& Mail::factory('smtp', $mail_params);
// Send the message
$mail_object->send($mail_to, $mail_headers, $mail_body);
?>
</body>
</html>
Summary of server details
Outgoing server |
As provided. |
Outgoing server protocol |
SMTP |
Outgoing server port |
25, 465, 587, 2525, 8025 or 10025 |
Authentication Type |
Basic Authentication |
Username |
As provided |
Password |
As provided |
Disclaimer
The Origin of this information may be internal or external to MX Tools. MX Tools makes all reasonable efforts to verify this information. However, the information provided in this document is for your information only. MX Tools makes no explicit or implied claims to the validity of this information. Any trademarks referenced in this document are the property of their respective owners.