Skip to content

Instantly share code, notes, and snippets.

@jonathanccalixto
Created October 30, 2014 00:04
Show Gist options
  • Save jonathanccalixto/8d596bd451c98966c989 to your computer and use it in GitHub Desktop.
Save jonathanccalixto/8d596bd451c98966c989 to your computer and use it in GitHub Desktop.
Configuração de email na kinghost
<?
class Email{
private $serverConfig;
private $errors;
private $log;
public function __construct ($serverConfig = array()) {
$this->serverConfig = array_merge_recursive(array(
"host" => "smtp.seuapp.com.br",
"port" => "25",
"timeout" => "10",
"authenticated" => true,
"user" => "[email protected]",
"password" => "suasenha"
), $serverConfig);
$this->errors = array();
$this->log = "";
}
public function send($mailConfig = array()){
$mailConfig = $this->mailConfig($mailConfig);
$connection = &$this->openConnection();
/*echo "<pre>";
var_dump($this->serverConfig);
var_dump($this->errors);exit("</pre>");*/
if(is_null($connection)){
return false;
}
# provides a return e-mail to receive error messages
fputs($connection, "MAIL FROM:<".$mailConfig["reply-to"]["email"].">\r\n", 512);
$this->log .= "\n".fgets($connection, 512);
# establishes the sending address
$address = (isset($mailConfig["to"]["email"]) ? $mailConfig["to"]["email"] : $mailConfig["to"][0]["email"]);
fputs($connection, "RCPT TO:<".$address.">\r\n", 512);
$this->log .= "\n".fgets($connection, 512);
# command the beginning of the email to be sent
fputs($connection, "DATA\r\n", 512);
$this->log .= "\n".fgets($connection, 512);
# email information to be sent
fputs($connection, "MIME-Version: 1.0\r\n");
fputs($connection, "Content-Type: text/html; charset=iso-8859-1\r\n");
fputs($connection, "Date: ".$mailConfig["date"]."\r\n");
$this->log .= "\nDate: ".$mailConfig["date"]."\r\n";
fputs($connection, "From: ".$this->mailFormat($mailConfig["from"])."\r\n");
$this->log .= "\nFrom: ".$this->mailFormat($mailConfig["from"])."\r\n";
fputs($connection, "Reply-to: ".$this->mailFormat($mailConfig["reply-to"])."\r\n");
$this->log .= "\nReply-to: ".$this->mailFormat($mailConfig["reply-to"])."\r\n";
fputs($connection, "To: ".$this->mailFormat($mailConfig["to"])."\r\n");
$this->log .= "\nTo: ".$this->mailFormat($mailConfig["to"])."\r\n";
$cc = $this->mailFormat($mailConfig["cc"]);
if(!empty( $cc ) ){
fputs($connection, "Cc: ".$cc."\r\n");
$this->log .= "\nCc: ".$cc."\r\n";
}
$bcc = $this->mailFormat($mailConfig["bcc"]);
if(!empty( $cc ) ){
fputs($connection, "Bcc: ".$bcc."\r\n");
$this->log .= "\nBcc: ".$bcc."\r\n";
}
fputs($connection, "Subject: ".$mailConfig["subject"]."\r\n");
fputs($connection, "\r\n");
fputs($connection, $mailConfig["body"]."\r\n.\r\n");
$this->log .= "\n".fgets($connection, 512);
# closes the connection to the server
fputs($connection, "QUIT\r\n", 512);
$end_connection = fgets($connection, 512);
$this->log .= "\n".$end_connection.
"\n----------------------------------------------------------------------";
fclose($connection);
//var_dump(preg_match('/^250 2\.0\.0 OK/', $end_connection)); exit();
return preg_match('/^250 2\.0\.0 ([oO][kK])/', $end_connection);
}
public function getLogs(){
return $this->log;
}
private function array_merge($array1, $array2){
if(!is_array($array1)){
$array1 = array();
}
foreach ($array2 as $key => $value) {
if (is_array($value)) {
if(!isset($array1["$key"])){ $array1["$key"] = array(); }
$array1["$key"] = $this->array_merge($array1["$key"], $value);
} else {
$array1["$key"] = $value;
}
}
return $array1;
}
private function mailConfig($config = array()) {
return $this->array_merge(array(
"from" => array("name" => "Nao responda", "email" => "[email protected]"),
"reply-to" => array("name" => "Nao responda", "email" => "[email protected]"),
"to" => array(),
"cc" => array("name" => "", "email" => ""),
"bcc" => array("name" => "", "email" => ""),
"date" => date('r',time()),
"subject" => "",
"body" => ""
), $config);
}
private function mailFormat($mails) {
$address = "";
if(isset($mails["email"]) && !empty($mails["email"])){
$address = "\"".$mails["name"]."\" <".mb_strtolower($mails["email"]).">";
} elseif(is_array($mails)){
$address = array();
foreach($mails as $mail){
if(isset($mail["email"]) && !empty($mail["email"])){
array_push($address, "\"".$mail["name"]."\" <".mb_strtolower($mails["email"]).">");
}
}
$address = join(', ', $address);
}
return $address;
}
# Opens a socket connection to the SMTP server using the fsockopen function;
# The @ in the beginning is to hide the error messages automatically,
# because we handle errors through the variables $errno and $errstr
private function openConnection(){
$connection = @fsockopen($this->serverConfig["host"], $this->serverConfig["port"], $errno, $errstr, $this->serverConfig["timeout"]);
if($errno){
echo "Errono ".$errno."<br />";
echo "Errstr ".$errstr;
exit();
$this->errors[$errno] = "Error connecting to \"".$this->serverConfig["host"].":".$this->serverConfig["port"]."\" ".$errstr;
return null;
}
$this->log .= "\n".fgets($connection, 1024);
fputs($connection,"HELO ".$this->serverConfig["host"]."\r\n", 512);
if($this->serverConfig["authenticated"]){
fputs($connection,"AUTH LOGIN\r\n", 512)."<br>"; # sends the request
$this->log .= "\n".fgets($connection, 512); # receives the response and prints
fputs($connection,base64_encode($this->serverConfig["user"])."\r\n", 512)."<br>"; # sends the request
$this->log .= "\n".fgets($connection, 512); # receives the response and prints
fputs($connection,base64_encode($this->serverConfig["password"])."\r\n", 512)."<br>"; # sends the request
$this->log .= "\n".fgets($connection, 512); # receives the response and prints
}
return $connection;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment