enviarEmailAniversario.php
<?php
// ------------------------------------------------ //
// Desenvolvido por Sílvio Garbes Lara - 23-04-2016 //
// silviogarbes@gmail.com //
// ------------------------------------------------ //
/*
No cron
Com root execute "crontab -e"
#[minutos] [horas] [dias do mês] [mês] [dias da semana] [usuário] [comando]
30 5 * * * php /var/www/html/intranet/aniversario_email/email.php > /dev/null #Email Feliz Aniversario
*/
class mailSocket {
public $server;
public $port = "25";
public $authenticate = FALSE;
public $authenticateUser;
public $authenticatePassword;
public $mailFrom;
public $mailTo;
public $subject;
public $header;
public $body;
function __construct() {
//Aumenta o tempo de espera maximo
set_time_limit(60);
}
function send(){
// Tenta conectar
$socket=fsockopen($this->server, $this->port, $errno, $errstr, 20);
echo fgets($socket, 256); // Verifica se conectou - code 220
if($this->authenticate){
echo $msg = "EHLO ".$this->server."\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 250
echo $msg = "AUTH LOGIN\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 334
echo $msg = base64_encode($this->$authenticateUser) . "\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 334
echo $msg = base64_encode($this->$authenticatePassword) . "\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 335
}else{
echo $msg = "HELO ".$this->server."\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 250
}
// E-mail Remetente
echo $msg = "MAIL FROM: ".$this->mailFrom."\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 250
// E-mail Destinatario
echo $msg = "RCPT TO: ".$this->mailTo."\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 250
// Pronto para envio
echo $msg = "DATA\r\n";
fputs($socket, $msg);
echo fgets($socket, 256); // code 354
// Cabecalho
echo $msg = "From: ".$this->mailFrom."\r\n";
fputs($socket, $msg);
echo $msg = "To: ".$this->mailTo."\r\n";
fputs($socket, $msg);
echo $msg = "Subject: ".$this->subject."\r\n";
fputs($socket, $msg);
// Corpo
fputs($socket, $this->header);
fputs($socket, $this->body);
// Finalizando envio
fputs($socket, "\r\n.\r\n");
echo fgets($socket, 256); // code 250
fputs($socket, "QUIT\r\n");
fclose($socket);
}
}
function enviarEmailAniversario($destinatario){
$mail = new mailSocket;
$mail->server = "smtp.silviogarbes.com.br";
$mail->mailFrom = "contato@silviogarbes.com.br";
$mail->mailTo = $destinatario;
$mail->subject = "Feliz aniversário";
//$mail->header = "";
//$mail->body = "";
$limiter = "_=======". date('YmdHms'). time() . "=======_";
$mail->header = "MIME-version: 1.0\r\n";
$mail->header .= "Content-type: multipart/related; boundary=\"".$limiter."\"\r\n";
// Codificar imagem para base64
$fileName = "/var/www/html/intranet/aniversario_email/felizaniversario.jpg";
$handle = fopen($fileName,'r');
$contents = fread($handle, filesize($fileName));
$encodedFile = chunk_split(base64_encode($contents));
fclose($handle);
$cid = date('YmdHms').'.'.time();
$mail->body = "--".$limiter."\r\n";
$mail->body .= "Content-type: text/html; charset=iso-8859-1\r\n";
// Tem que ficar sem identacao e nao pode ter espaco no inicio de cada linha
$html = "
<html>
<body>
<center>
<h2 style='color:blue;'>Silvio Garbes lhe deseja neste dia tão especial um</h2>
<img src=\"cid:".$cid."\">
</center>
</body>
</html>
";
$mail->body .= $html;
$mail->body .= "--".$limiter."\r\n";
$mail->body .= "Content-type: image/jpeg; name=\"".$fileName."\"\r\n";
$mail->body .= "Content-Transfer-Encoding: base64\r\n";
$mail->body .= "Content-ID: <".$cid.">\r\n";
$mail->body .= "\n".$encodedFile."\r\n";
$mail->body .= "--".$limiter."--\r\n";
$mail->send();
}
define("HOSTDB", "mysql01.silviogarbes.com.br");
define("USERDB", "intranet");
define("PASSDB", "senhadomysql");
define("NAMEDB", "aniversario");
$dbhandle = new mysqli(HOSTDB, USERDB, PASSDB, NAMEDB);
if ($dbhandle->connect_error) {
exit("Sem conexão: ".$dbhandle->connect_error);
}
$strQuery = "SELECT email FROM colaborador WHERE data = curdate() AND email <> ''";
$result = $dbhandle->query($strQuery) or exit("Erro codigo ({$dbhandle->errno}): {$dbhandle->error}");
if ($result) {
while($row = mysqli_fetch_array($result)) {
$email = $row['email'];
//$email = "silviogarbes@gmail.com.br";
enviarEmailAniversario($email);
}
}
$dbhandle->close();
?>