20150123 testing smtp with php - plembo/onemoretech GitHub Wiki

title: Testing SMTP with PHP link: https://onemoretech.wordpress.com/2015/01/23/testing-smtp-with-php/ author: phil2nc description: post_id: 9207 created: 2015/01/23 12:01:14 created_gmt: 2015/01/23 17:01:14 comment_status: closed post_name: testing-smtp-with-php status: publish post_type: post

Testing SMTP with PHP

A simple recipe to test SMTP mailing using php. There a a number of mail-related modules available for php. Some are more full-featured than others. The language by default lets you send messages through a host's local MTA like sendmail or postfix running on the host, but doing so is not a best practice and not available in most environments because of that. Sending mail through a remote relay usually requires the installation of additional modules, like PHP:Mail:Net::SMTP. On Red Hat derived systems the packages needed to support this are: php-pear-Mail php-pear-Mail-Net-SMTP If not already installed these will also load some other packages as dependencies, such as php-pear-Net-Socket, php-pear-Auth-SASL to support SSL/TLS encrypted connections and authentication. Here's a sample script adapted from one provided by Rackspace to test this functionality: [code lang="php" gutter="false"] "; $to = "Inside Devs "; $subject = "Test email using SMTP\r\n\r\n"; $body = "This is a test email using PHP Pear's Mail:Net:SMTP."; $host = "smtp.example.com"; // $username = "[email protected]"; // $password = "yourPassword"; $headers = array ( 'From' => $from, 'To' => $to, 'Subject' => $subject); $smtp = Mail::factory('smtp', array ( 'host' => $host, 'port' => 25, 'auth' => false, // 'username' => $username, // 'password' => $password)); )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo("

" . $mail->getMessage() . "

"); } else { echo("

Message successfully sent!

"); } ?> [/code] In the above authentication is not being used, and the well-known port for SMTP messaging is being addressed (TLS encrypted SMTP usually uses port 587, although some systems use port 465 for compatibility with legacy SMTPS servers). NOTE: The following code can be used to test the basic mail via local MTA functionality that php comes with out of the box: [code lang="php" gutter="false"] [/code] This basically corresponds to the tried-and-true command line test of your local MTA: [code lang="bash" gutter="false"] echo "This is a test." | mail -s "Testing local mail" [email protected] [/code]

Copyright 2004-2019 Phil Lembo

⚠️ **GitHub.com Fallback** ⚠️