PHP Form Mail For Linux Server using SMTP authentication

Send Email from a PHP Script Using SMTP Authentication

To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email:
 
Adapt the example below for your needs. Make sure you change the following variables at least:

from: the email address from which you want the message to be sent.
to: the recipient's email address and name.
host: your outgoing SMTP server name.
username: the SMTP user name (typically the same as the user name used to retrieve mail).
password: the password for SMTP authentication.

This is PHP form mail script for Linux server where mail ids are configured with own hosting server

<?php
require_once "Mail.php";
$from = "Sandra Sender ";
$to = "Ramona Recipient ";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.yourdomain.com";
$username = "test@yourdomain.com";
$password = "1234";
$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("
" . $mail->getMessage() . "

");
 } else {
  echo("
Message successfully sent!

");
 }
?>

 

This is PHP form mail script for Linux server where mail ids are configured with Google Apps or Gmail.


<?php
require_once "Mail.php";
$from = "Sandra Sender <test@yourdomain.com>";
$to = "Ramona Recipient <test2@yourdomain.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$port = "587";
$username = "test@yourdomain.com";
$password = "1234";
$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
 'port' => $port,
    'username' => $username,
    'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
  echo("
" . $mail->getMessage() . "

");
 } else {
  echo("
Message successfully sent!

");
 }
?>
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

Form Mail .net

In web.config File   <system.net>    <mailSettings>      <smtp>...

Form Mail php for Windows Server

This is php form mail script for Windows server which mail ids configure with your own Hosting...

ZOHO Form Mail

This is PHP form mail script for Linux server where mail ids are configured with ZOHO Mail....