This is the context:
I had a shared hosting plan at Hostgator (firstly) and WebHostPython (lately) (both cPanel) PHP Mailer 5.2 worked just perfect
I migrated from shared hosting to VPS From cPanel to HestiaCP
PHPMailer is unable to send emails
I made some variable declaration on the script, but it is still not working, even after trying to move from PHP-Mailer 5.2 to 6.8
It seems that "clasically" the script took the smtp configuration directly from the cPanel config, or something like that, as an example
$mail->Host were not defined on the script that allways worked fine.
now I define all the smtp settings, and it still not working
I'd LOVE if you can take 1 or 2 minutres to review this script and post your suggestion:
At line 226 the definition of HOST begins...
<?php session_start();
if($_SESSION['user'] && $_SESSION['role'] == 2){}
else{
header("location:index.php");
}
$user = $_SESSION['user'];
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/general.css"> </link>
<link rel="stylesheet" type="text/css" href="css/table.css"> </link>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Envío Mail</title>
</head>
<body>
<div class="menu">
<?php include 'menu.php';?>
</div>
<?php
function armarAsunto($nombreCarpeta) {
$partes = explode("-", $nombreCarpeta);
if($partes[1] == '01') { $MES = 'ENE'; }
elseif ($partes[1] == '02') { $MES = 'FEB'; }
elseif ($partes[1] == '03') { $MES = 'MAR'; }
elseif ($partes[1] == '04') { $MES = 'ABR'; }
elseif ($partes[1] == '05') { $MES = 'MAY'; }
elseif ($partes[1] == '06') { $MES = 'JUN'; }
elseif ($partes[1] == '07') { $MES = 'JUL'; }
elseif ($partes[1] == '08') { $MES = 'AGO'; }
elseif ($partes[1] == '09') { $MES = 'SEP'; }
elseif ($partes[1] == '10') { $MES = 'OCT'; }
elseif ($partes[1] == '11') { $MES = 'NOV'; }
elseif ($partes[1] == '12') { $MES = 'DIC'; }
else {$MES = 'MES';};
if($partes[2] == '01') { $textoNumeroActualizacion = '1ra. Actualización de precios.'; }
elseif ($partes[2] == '02') { $textoNumeroActualizacion = '2da. Actualización de precios.'; }
elseif ($partes[2] == '03') { $textoNumeroActualizacion = '3ra. Actualización de precios.'; }
elseif ($partes[2] == '04') { $textoNumeroActualizacion = '4ta. Actualización de precios.'; }
elseif ($partes[2] == '05') { $textoNumeroActualizacion = '5ta. Actualización de precios.'; }
elseif ($partes[2] == '06') { $textoNumeroActualizacion = '6ta. Actualización de precios.'; }
elseif ($partes[2] == '07') { $textoNumeroActualizacion = '7ma. Actualización de precios.'; }
elseif ($partes[2] == '08') { $textoNumeroActualizacion = '8va. Actualización de precios.'; }
elseif ($partes[2] == '09') { $textoNumeroActualizacion = '9na. Actualización de precios.'; }
elseif ($partes[2] == '10') { $textoNumeroActualizacion = '10ma. Actualización de precios.'; }
elseif ($partes[2] == '11') { $textoNumeroActualizacion = '11va. Actualización de precios.'; }
elseif ($partes[2] == '12') { $textoNumeroActualizacion = '12va. Actualización de precios.'; }
elseif ($partes[2] == '13') { $textoNumeroActualizacion = '13va. Actualización de precios.'; }
elseif ($partes[2] == '14') { $textoNumeroActualizacion = '14va. Actualización de precios.'; }
elseif ($partes[2] == '15') { $textoNumeroActualizacion = '15va. Actualización de precios.'; }
elseif ($partes[2] == '16') { $textoNumeroActualizacion = '16va. Actualización de precios.'; }
elseif ($partes[2] == '17') { $textoNumeroActualizacion = '17va. Actualización de precios.'; }
elseif ($partes[2] == '18') { $textoNumeroActualizacion = '18va. Actualización de precios.'; }
elseif ($partes[2] == '19') { $textoNumeroActualizacion = '19va. Actualización de precios.'; }
elseif ($partes[2] == '20') { $textoNumeroActualizacion = '20va. Actualización de precios.'; }
else {$textoNumeroActualizacion = 'Actualización de precios.';};
$asunto = $MES . "/" . $partes[0] . " - " . $textoNumeroActualizacion;
return $asunto;
}
/*Funcion para validar checkbox*/
function IsChecked($chkname,$value)
{
if(!empty($_POST[$chkname]))
{
foreach($_POST[$chkname] as $chkval)
{
if($chkval == $value)
{
return true;
}
}
}
return false;
}
;?>
</br></br></br></br></br></br></br></br>
<form id="envioMail" name="envioMail" action="#" method="post">
<p>Adjuntar: <select class="Select-Actualizacion" id="archivosAdjuntos" name="archivosAdjuntos">
<?php
$user = $_SESSION['user'];
$ini_array = parse_ini_file("config.ini");
//Cargo valores de configuración
$host = $ini_array['host'];
$dbuser = $ini_array['dbusercsv'];
$dbpass = $ini_array['dbpasscsv'];
$schema = $ini_array['schema'];
$ruta_envio_mail = $ini_array['carpeta_ENVIO_MAIL'];
$directorio = $ruta_envio_mail;
$carpetas = scandir($directorio,SCANDIR_SORT_DESCENDING);
print '<option value="Seleccionar">Seleccionar</option>';
foreach ($carpetas as &$nombreCarpeta) {
if($nombreCarpeta != "." && $nombreCarpeta != ".."){
print '<option value="'.$nombreCarpeta.'">'.$nombreCarpeta.'</option>';
}
}
//JavaScript para cargar el Asunto
print '<script type="text/javascript">';
print ' var myData = new Array();';
//Cargo los valores del asunto
foreach ($carpetas as &$nombreCarpeta) {
if($nombreCarpeta != "." && $nombreCarpeta != ".."){
$asuntoFinal = armarAsunto($nombreCarpeta);
print 'myData["'.$nombreCarpeta.'"] = "'. $asuntoFinal .'";';
}
}
print '</script>';
?>
</select>
<input type="submit" name="submit" id="enviomail" value="ENVIAR" />
</p>
<textarea id="asunto" name="asunto" rows="1" cols="80">Actualización de precios</textarea>
</br>
<textarea name="cuerpoMail" rows="12" cols="80">Cuerpo del mail</textarea>
</br>
<?php
$con = mysqli_connect($host, $dbuser, $dbpass, $schema);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET CHARACTER SET 'utf8'");
$sql = "select CONCAT(apellido,', ',nombre) NOMBRE, Nombre_libreria LIBRERIA, mail MAIL from users where role in (2,3)";
$query = mysqli_query($con, $sql);
$row_cnt = mysqli_num_rows($query);
if($row_cnt > 0){
Print "<table class='rwd-table'>";
Print "<tr>";
Print "<th>Nombre</th>";
Print "<th>Libreria</th>";
Print "<th>Casilla de Mail</th>";
Print "<th><input type=\"checkbox\" onclick=\"checkAll('listaCasillasEnvioMail[]');\" checked> Enviar</th>";
Print "<th><input type=\"checkbox\" onclick=\"checkAll('adjuntoArchivos[]');\" checked> Adjuntar</th>";
Print "</tr>";
}
while ($row = mysqli_fetch_array($query)) {
Print "<tr>";
Print '<td>' . $row['NOMBRE'] . "</td>";
Print '<td>' . $row['LIBRERIA'] . "</td>";
Print '<td>' . $row['MAIL'] . "</td>";
Print '<td> <input type="checkbox" name="listaCasillasEnvioMail[]" value="' . $row['MAIL'] . '" checked> </td>';
Print '<td> <input type="checkbox" name="adjuntoArchivos[]" value="' . $row['MAIL'] . '" checked> </td>';
Print "</tr>";
}
?>
</form>
<?php
//Incluyo PHPMailer Project
// php Mailer Master (version 5.2) require_once('./PHPMailer-master/class.phpmailer.php');
// require_once('./PHPMailer6/src/PHPMailer.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require './PHPMailer6/src/PHPMailer.php';
if(isset($_POST['submit']) && !empty($_POST['listaCasillasEnvioMail'])){
$carpetaArchivos = $_POST['archivosAdjuntos'];
$con = mysqli_connect($host, $dbuser, $dbpass, $schema);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
foreach($_POST['listaCasillasEnvioMail'] as $casillaEnvioMail) {
$email = new PHPMailer();
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET CHARACTER SET 'utf8'");
$sql = "select tab.casilla from (";
$sql = $sql. " select mail_aux_1 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_2 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_3 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_4 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_5 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_6 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_7 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_8 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_9 casilla from users where mail = '" . $casillaEnvioMail . "' union all";
$sql = $sql. " select mail_aux_10 casilla from users where mail = '" . $casillaEnvioMail . "'";
$sql = $sql. " ) tab where tab.casilla is not null and tab.casilla != '' ";
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($query)) {
$email->AddCC($row['casilla']);
}
// bloque agregado por la falla de HestiaCP que no reconoce la configuracion de email, o no se la traslada a/de PHP
$email->isSMTP();
$email->SMTPDebug = 2;
$email->Debugoutput = 'html';
$email->SMTPSecure = false;
$email->SMTPAutoTLS = false;
$email->Host = 'my vps smtp' ;
$email->Port = 25; //use port 465 when using SMPTSecure = 'ssl'
$email->SMTPAuth = true;
$email->Username = 'user@example.com';
$email->Password = 'xxx';
$email->From = 'user@example.com';
$email->ConfirmReadingTo = 'user@example.com';
$email->FromName = 'Precios de Libros';
$email->Subject = $_POST['asunto'];
$email->Body = $_POST['cuerpoMail'];
//$email->IsHTML(true);
$email->CharSet = 'UTF-8';
$email->AddAddress( $casillaEnvioMail );
if(IsChecked('adjuntoArchivos',$casillaEnvioMail))
{
//Valido que tipo de archivos tengo que enviar
$sql = "select upper(ae.nombre_archivo) nombre_archivo from users user, tipo_archivos_envio tae, archivo_envio ae";
$sql = $sql. " where user.tipo_archivos_envio_id = tae.id";
$sql = $sql. " and tae.codigo_envio = ae.codigo_tipo_archivos_archivo";
$sql = $sql. " and user.mail = '" . $casillaEnvioMail . "'";
$query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($query)) {
//Cargo los archivos adjuntos
$archivos = scandir($ruta_envio_mail."/".$carpetaArchivos);
foreach ($archivos as &$nombreArchivo) {
$existe = stripos($nombreArchivo, $row['nombre_archivo']);
if($nombreArchivo != "." && $nombreArchivo != ".." && $existe !== false){
$email->AddAttachment( $ruta_envio_mail. "/" .$carpetaArchivos . "/" . $nombreArchivo );
print "<p>SE ADJUNTARA EL ARCHIVO: " . $nombreArchivo . "</p>";
}
}
}
}
else{
print '<p>NO adjunto los archivos para ' . $casillaEnvioMail . '</p>';
}
//Envìo el mail
if(!$email->Send()) {
print '<p>No se pudo enviar el mail a la casilla de "' . $casillaEnvioMail . '"';
print " --> <b>Detalle del error:</b> " . $email->ErrorInfo . "</p>";
} else {
print "<p>Se envió el mail a la casilla de: " . $casillaEnvioMail . "</p>";
}
}
}
?>
<script type="text/javascript">
document.envioMail.archivosAdjuntos.onchange = updateText;
function updateText() {
var obj_sel = document.envioMail.archivosAdjuntos;
document.envioMail.asunto.value = myData[obj_sel.value];
}
</script>
<script type="text/javascript">
function checkAll(checkName){
var inputs = document.getElementsByName(checkName);
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == "checkbox" ) {
if(inputs[i].checked == true) {
inputs[i].checked = false ;
} else if (inputs[i].checked == false ) {
inputs[i].checked = true ;
}
}
}
}
</script>
</body>
</html>