Tag Archives: SMTP

How to Send email direct from linux terminal

To send an email from console you need to use mail command, which is an intelligent mail processing system which has a command syntax reminiscent of ed with lines replaced by messages.

To send an email to [email protected] you need to type following command:

$ mail [email protected]
Output:

Subject: Hello
Hi,
Kamusta kana? sana ok ka/// :)
Ingats
xMoDx
. <Type  DOT (.) followed by ENTER KEY>
Cc: <Press ENTER KEY>

You need to type . (dot) to send an email.

To send contains of file (such as /var/log/messages) as mail body then use following command:

$ mail -s 'Subject here' [email protected] < /var/log/messages

Please note that above command will NOT route an email if you do not have properly configured MTA/mail server. and check your spam folder for paid services like gmail for the mail might end up there

Test if local SMTP server is running

To test if a local SMTP server is running, you can try to ping the port 25

$ echo -e “quit” | nc localhost 25

220 ubuntu ESMTP Postfix (Ubuntu)
221 2.0.0 Bye
If no SMTP server is listening

$ echo -e “quit” | nc localhost 25

localhost.localdomain [127.0.0.1] 25 (?) : Connection refused

How do I test PHP SMTP functionality?

You can test PHP SMTP functions with the following two examples. The first one is standard SMTP while the second one is SMTP with SSL.

Sending with PHP SMTP

You will only need to change the following variables:

  • $from
  • $to
  • $subject
  • $body
  • $username
  • $password
<?php
require_once "Mail.php";

$from = "Web Master <[email protected]>";
$to = "Nobody <[email protected]>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";

$host = "mail.emailsrvr.com";
$username = "[email protected]";
$password = "yourPassword";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    '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>");
}
?>

Sending with PHP SMTP with SSL

You will only need to change the following variables:

  • $from
  • $to
  • $subject
  • $body
  • $username
  • $password
<?php
require_once "Mail.php";

$from = "Web Master <[email protected]>";
$to = "Nobody <[email protected]>";
$subject = "Test email using PHP SMTP with SSL\r\n\r\n";
$body = "This is a test email message";

$host = "ssl://secure.emailsrvr.com";
$port = "465";
$username = "[email protected]";
$password = "yourPassword";

$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>");
}
?>