Scripting‎ > ‎

PHP sockets send auth email (gmail)

posted Sep 10, 2015, 7:16 AM by Chris Franklin


Code

function SendMail($ServerName, $Port, $Username, $Password, $ToEmail, $FromEmail, $Subject, $Body, $Attachments='' ) {
 /* Attachments work like this array('Attachment Name'=>'ATTACHMENT AS A STRING','Next Attachment Name'=>'ATTACHMENT 2 AS A STRING') */

 $smtp = fsockopen($ServerName, $Port);
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 220;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed Connect";

 fputs($smtp, "HELO $ServerName\r\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 250;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed Helo";

 fputs($smtp, "AUTH LOGIN\r\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 334;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed AUTH";

 fputs($smtp, base64_encode($Username)."\r\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 334;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed Username";

 fputs($smtp, base64_encode($Password)."\r\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 235;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed Password";

 fputs($smtp, "MAIL From:<$FromEmail>\r\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 250;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed MAIL";

 fputs($smtp, "RCPT To:<$ToEmail>\r\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 250;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed RCPT";

 fputs($smtp, "DATA\r\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 354;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed DATA";

 fputs($smtp, "From: $FromEmail\r\n");
 fputs($smtp, "To: $ToEmail\r\n");
 if ($Attachments != '') {
  $ContentBoundry = '----=_NextPart_'.md5(date('U').uniqid('NCF',TRUE));
  fputs($smtp, "MIME-Version: 1.0\r\n");
  fputs($smtp, 'Content-Type: multipart/mixed; boundary="'.$ContentBoundry.'"'."\r\n");
 }
 fputs($smtp, "Subject: $Subject\r\n\r\n");

 if ($Attachments == '') {
  fputs($smtp, "$Body\r\n.\r\n");
 } else {
  $NewBody  = "\r\n";
  $NewBody .= '--'.$ContentBoundry."\r\n";
  $NewBody .= 'Content-Type: text/plain; charset="UTF-8"'."\r\n";
  $NewBody .= 'Content-Transfer-Encoding: 8bit'."\r\n\r\n";
  $NewBody .= $Body."\r\n";
  foreach ($Attachments as $Filename => $Attachment) {
   $NewBody .= '--'.$ContentBoundry."\r\n";
   $NewBody .= 'Content-Type: application/octet-stream; name="'.$Filename.'"'."\r\n";
   $NewBody .= 'Content-Transfer-Encoding: base64'."\r\n";
   $NewBody .= 'Content-Disposition: attachment; filename="'.$Filename.'"'."\r\n\r\n";
   $NewBody .= chunk_split(base64_encode($Attachment));
   $NewBody .= "\r\n";
  }
  $NewBody .= $ContentBoundry."--\r\n\r\n";
  fputs($smtp, $NewBody."\r\n.\r\n");
 }

 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 250;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed BODY";

 fputs($smtp, "QUIT\n");
 $InputBuffer = fgets($smtp, 1024);
 $ErrorCode = 221;
 if(substr($InputBuffer,0,3) != "$ErrorCode") return "Failed QUIT";

 fclose($smtp);

 return TRUE;
}


How to use it

$TheAttachments = array('CurriculumReviewCycle.pdf'=>file_get_contents('/var/www/html/CurriculumReviewCycle.pdf'));

//  SendMail($ServerName, $Port, $Username, $Password, $ToEmail, $FromEmail, $Subject, $Body, $Attachments='' )

SendMail('smtp.gmail.com', 25, 'franklinc@fromdomain', 'Password', 'cfranklin@todomain', 'franklinc@fromdomain', 'Attach - Test - subject', 'What up dog! - the body', $TheAttachments);


Comments