php code to send email using smtp server code example

Example 1: send mail config using gmail php

Before sending emails using the Gmails SMTP Server, you to make some of the
  security and permission level settings under your Google Account Security
  Settings.

1. Make sure that 2-Step-Verification is disabled.

2. Turn ON the "Less Secure App" access or click here.

3. If 2-step-verification is enabled, then you will have to create app
  password for your application or device.

4. For security measures, Google may require you to complete this additional
    step while signing-in. Click here to allow access to your Google account
      using the new device/app.

Note*: It may take an hour or more to reflect any security changes
===================================================================
PHP CODE CONFIG :=
  
$mail->SMTPDebug  = 1;  
$mail->SMTPAuth   = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port       = 587;
$mail->Host       = "smtp.gmail.com";
$mail->Username   = "[email protected]";
$mail->Password   = "your-gmail-password";

=================================================================

For More Info check this link : https://pepipost.com/tutorials/send-an-email-via-gmail-smtp-server-using-php/

Example 2: send email php smtp

I migrated an application to a platform without a local transport agent (MTA). I did not want to configure an MTA, so I wrote this xxmail function to replace mail() with calls to a remote SMTP server. Hopefully it is of some use.

function xxmail($to, $subject, $body, $headers)
{
 $smtp = stream_socket_client('tcp://smtp.yourmail.com:25', $eno, $estr, 30);

 $B = 8192;
 $c = "\r\n";
 $s = '[email protected]';

 fwrite($smtp, 'helo ' . $_ENV['HOSTNAME'] . $c);
  $junk = fgets($smtp, $B);

 // Envelope
 fwrite($smtp, 'mail from: ' . $s . $c);
  $junk = fgets($smtp, $B);
 fwrite($smtp, 'rcpt to: ' . $to . $c);
  $junk = fgets($smtp, $B);
 fwrite($smtp, 'data' . $c);
  $junk = fgets($smtp, $B);

 // Header 
 fwrite($smtp, 'To: ' . $to . $c); 
 if(strlen($subject)) fwrite($smtp, 'Subject: ' . $subject . $c); 
 if(strlen($headers)) fwrite($smtp, $headers); // Must be \r\n (delimited)
 fwrite($smtp, $headers . $c); 

 // Body
 if(strlen($body)) fwrite($smtp, $body . $c); 
 fwrite($smtp, $c . '.' . $c);
  $junk = fgets($smtp, $B);

 // Close
 fwrite($smtp, 'quit' . $c);
  $junk = fgets($smtp, $B);
 fclose($smtp);
}

Tags:

Php Example