BASIC PHP Code to Send Authenticated Email
- 06/06/2020 8:01 PM
Original Contributor http://www.codewalkers.com/c/a/Email-Code/Smtp-Auth-Email-Script/
Thanks to "bjpalmer"
Here is a very simple script that allows you to send emails through smtp with auth. Bits were taken from all over the internet and I have tried to keep it simple.
By : bjpalmer
The OPENING php tag is omitted from Knowledgebase for security reasons simply add it to the top of the script.
//new function
//bjpalmer circa 2005
// additional comments added mjs in 2010
$to = "post@example.com"; //This is the email ADDRESS TO which you will be sending
$nameto = "Who To"; // This is the NAME of the person, i.e
$from = "post@example.com"; // This is the FROM Address
$namefrom = "Who From"; //This is WHO the email is from
$subject = "Hello World Again!"; //This is the SUBJECT
$message = "World, Hello!" //This is the BODY of the messsage
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
?>
/* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */
//Authenticate Send - 21st March 2005
//This will send an email using auth smtp and output a log array
//logArray - connection,
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = "mail.server.com"; //This is CRITICAL the SMTP Server -
$port = "25";
$timeout = "30";
$username = "smtpusername"; //This is the Username/Email that CAN authenticate with the SMTP Server
$password = "smtppassword";
$localhost = "localhost"; //This is the LOCAL Server from which you are sending - spoofing this will cause most mail servers to BLOCK this server's IP
$newLine = "\r\n"; //The ALL Important Line break after the SMTP connection has been established and each command is passed
/* * * * CONFIGURATION END * * * * */
//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine); //HELO is now required by about 95% of all Mail servers - EHLO would be better but this script is not written for that
$smtpResponse = fgets($smtpConnect, 515); //Again checking for the dreaded 515 response
$logArray['heloresponse'] = "$smtpResponse";
//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>