Home > OS >  How to set up a newsletter sent via php code on your centos 8 server and an external service
How to set up a newsletter sent via php code on your centos 8 server and an external service

Time:01-02

I have a problem and I would like a couple of tips from you. I have a corporate email @ example.info relating to my example.info website hosted on godaddy. I have searched on google but I have not found any guides on the matter, how can I create a php code on my centos 8 server that sends a preset newsletter when I decide it? 1- I just need to know if it is possible to link my php code to an @ example.info externa email and how to do it. 2- Furthermore, I would need to know how to send a newsletter to more than 8 thousand people without being entered as spam by google or other such problems. I opted for this solution because I found on the internet that gmail doesn't allow you to send more than 100 emails automatically or something like that, so I created my own email to do the same thing but bypass the limitation. Anyone who can explain to me how to do or link me a guide, is welcome. Thank you

CodePudding user response:

You may use phpmailer library to easily send mail thru an external SMTP server, but if you do not want to use phpmailer, use the following PHP function:

<?php  

function authMail($from, $namefrom, $to, $nameto, $subject, $message){
/*  your configuration here  */

$smtpServer = "XX.XX.XX.XXXX"; //ip accepted as well
$username = "xxxxxxx"; //the login for your smtp
$password = "xxxxxxx"; //the pass for your smtp


$port = "25"; // should be 25 by default
$timeout = "60"; //typical timeout. try 45 for slow servers

$localhost = "127.0.0.1"; //this seems to work always
$newLine = "\r\n"; //var just for nelines in MS
$secure = 0; //change to 1 if you need a secure connect
  
/*  you shouldn't need to mod anything else */

//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect))
{
   $output = "Failed to connect: $smtpResponse";
   return $output;
}
else
{
   $logArray['connection'] = "Connected to: $smtpResponse";
}

//say HELO to our little friend
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse'] = "$smtpResponse";

//start a tls session if needed 
if($secure)
{
   fputs($smtpConnect, "STARTTLS". $newLine);
   $smtpResponse = fgets($smtpConnect, 4096);
   $logArray['tlsresponse'] = "$smtpResponse";

   //you have to say HELO again after TLS is started
   fputs($smtpConnect, "HELO $localhost". $newLine);
   $smtpResponse = fgets($smtpConnect, 4096);
   $logArray['heloresponse2'] = "$smtpResponse";
}

//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";

//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";

//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";

//email from
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";


//email to
fputs($smtpConnect, "RCPT TO: $to" . $newLine);

$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";

//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";

//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=utf-8" . $newLine;
//$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;


//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: $to\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success 
return($logArray);
}


?>
  • Related