At the beginning of the year, there were several significant changes in email sending practices. Most email providers like Seznam, Gmail, Yahoo, and many others have tightened their anti-spam policies, making it quite complex to deliver bulk and regular emails. Even emails sent via the PHP mail()
function are affected by these restrictions. The following code will help you modify your contact forms to send emails via an SMTP server.
<?php
$to = "RECIPIENT EMAIL";
$nameto = "RECIPIENT NAME";
$from = "SENDER EMAIL";
$namefrom = "SENDER NAME";
$subject = "SUBJECT";
$message = "EMAIL MESSAGE";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
// SMTP SERVER CONFIGURATION
$smtpServer = "email.mydreams.cz";
$port = "25";
$timeout = "30";
$username = "SMTP SERVER LOGIN";
$password = "SMTP SERVER PASSWORD";
$localhost = "email.mydreams.cz";
$newLine = "\r\n";
// CONNECT TO SMTP SERVER VIA 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";
}
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
// CREATE EMAIL HEADER
$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";
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>