Foros del Web » Programando para Internet » PHP »

Envio de Mail HTML con consultas a una BD con PHP

Estas en el tema de Envio de Mail HTML con consultas a una BD con PHP en el foro de PHP en Foros del Web. Hola Amigos del foro. Tengo un problema al enviar un mail desde php. Estoy haciendo uso de mail(), y todo funciona bien cuando envio un ...
  #1 (permalink)  
Antiguo 07/07/2010, 09:38
 
Fecha de Ingreso: mayo-2008
Ubicación: Lima
Mensajes: 11
Antigüedad: 15 años, 10 meses
Puntos: 0
Envio de Mail HTML con consultas a una BD con PHP

Hola Amigos del foro.
Tengo un problema al enviar un mail desde php. Estoy haciendo uso de mail(), y todo funciona bien cuando envio un html normal o solo texto; pero no se envia nada cuando en el contenido le agrego datos obtenidos desde una bd mysql. No se si el problema está en la programacion o en mi hosting, agradezco mucho su ayuda. Este es el codigo.

// email_stock.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--

p {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
p em {
color: #FF6600;
}
a {
color: #FF6600;
text-decoration: none;
padding-bottom: 2px;
border-bottom-width: 1px;
border-bottom-style: dotted;
border-bottom-color: #8A5300;
}
strong {
color: #663300;
font-weight: bold;
}.Estilo13 {
color: #006699;
font-weight: bold;
}
.Estilo14 {color: #FFFFFF}
.Estilo16 {font-size: 12px}
.Estilo17 {font-size: 11px}
.Estilo18 {
font-family: Tahoma;
font-size: 13px;
}
-->
</style>
</head>

<body>

<p>[*MENSAJE*]</p>
<br />
<br />
<table border="0" cellpadding="0" cellspacing="1">

<tr height="33">

<td style="background-color: #996633; width: 78px; heigh: 20px; text-align: center; font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14 Estilo16" >
<div align="left">Country</div>
</div></td>

<td style="background-color: #996633; width: 130px; heigh: 20px; text-align: center;font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14">
<p align="left" class="Estilo16">Grade</p>
</div></td>

<td style="background-color: #996633; width: 91px; height: 30; text-align: center; font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14 Estilo16">
<div align="left">Quantity</div>
</div></td>

<td style="background-color: #996633; width: 137px; height: 30; text-align: center; font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14 Estilo16">
<div align="left">Price</div>
</div></td>

<td style="background-color: #996633; width: 110px; height: 30; text-align: center; font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14 Estilo16">
<div align="left">Shippig Date <br />
</div>
</div></td>

<td style="background-color: #996633; width: 40px; height: 30; text-align: center; font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14 Estilo16">
<div align="left">Quality<br />
</div>
</div></td>

<td style="background-color: #996633; width: 95px; height: 30; text-align: center; font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14 Estilo16">Altitude</div></td>

<td style="background-color: #996633; width: 150px; height: 30; text-align: center; font-family: Arial,sans-serif; font-size: 11px; color: #DEBE9E;"><div align="center" class="Estilo13 Estilo14 Estilo16">Specifications</div></td>
</tr>

[*BODY*]

</table>


<p><a href="http://www.tropicalmountains.com"><img src="http://img11.imageshack.us/img11/8412/tropizx5.jpg" border="0" alt="Image Hosted by ImageShack.us"/></a><br/></p>
<table width="100%" border="0">
<tr>
<td><span class="Estilo18">Celular: +51-1-992924894<br />
Fijo: +41-78878-3222<br />
[email protected]<br />
Skype: schweglerthomas<br />
MSN: [email protected]<br />
<br />
You can also see this offer list online at: <br />
<a href="http://www.tropicalmountains.com/stockprices.php">www.tropicalmountains.com/stockprices.php</a><br />
<br />
Note: All offers and prices are subject to reconfirmation</span></td>
</tr>
</table>
</body>
</html>

En el siguiente mensaje pongo la prte 2.
  #2 (permalink)  
Antiguo 07/07/2010, 09:39
 
Fecha de Ingreso: mayo-2008
Ubicación: Lima
Mensajes: 11
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: Envio de Mail HTML con consultas a una BD con PHP

Este es en otro archivo.

// email-form.php
<?php
include_once("inc/session.php"); // aca esta la conexion.


$ob = "";
if (isset($_GET['ob']))
{
$ob = $_GET['ob'];
}else{
$ob = "Pais";
}

if (isset($_GET['n']) && isset($_POST['Pais']) == false )
{
if ($n == 1)
{
$inicio = 0;
$fin = 25;
}else{
$inicio = $_GET['n'];
$fin = $inicio*25;
$inicio = ($inicio-1)*25;
}
}elseif(isset($_POST['Pais']) == false){
$inicio = 0;
$fin = 25;
}else{
$inicio = 0;
$fin = 1000;
}


$msg = "";
$pais = array();
$table = "";
$pais[] = "Brazil";
if (isset($_POST['button']))// validamos si se hizo click
{
/*if ($_POST['mensaje'] != "" )
{ */

if ($_POST['compraventa'] == "2")
{
// tomamos el contenido de email_stock.php
$contenido = file_get_contents("email_stock.php");
// reemplazamos la palabra [*MENSAJE*] por lo que contenga $_POST['mensaje'].
$contenido = str_replace("[*MENSAJE*]",$_POST['mensaje'],$contenido);
// consulta que si esta bien
$result = $_SESSION['db']->getadminOfertasPublish($ob,$inicio,$fin);
/* function getadminOfertasPublish($ob,$i,$f)
{
$this->execute();
if ($ob == "Embarque")
{
$sql = "select * from oferta o,mes_embarq m where m.estado='1' and m.estado=o.estado and o.IdOferta=m.IdOferta and o.Embarque=m.Embarque and o.Embarque1=m.Embarque1 and o.Publicar = 1 order by m.id";
}elseif($ob == "Pais"){
$sql = "select * from oferta o,mes_embarq m where m.estado='1' and m.estado=o.estado and o.IdOferta=m.IdOferta and o.Embarque=m.Embarque and o.Embarque1=m.Embarque1 and o.Publicar = 1 order by o.paiscafe";
}else{
$sql = "SELECT * FROM oferta where m.estado='1' and Publicar = 1 ORDER BY $ob ";
}
return mysql_query($sql,$this->cn);
}

*/
$style = "class='lista'";
$table = ""; // variable $table q almacenará todo el contenido

$bgr = " style='background-color: #DEBE9E;border-top-width: 2px;border-top-style: solid;border-top-color: #703E18; text-align: right; color: #C7A861;' ";
$bgc = " style='text-align: center;' ";
$bgl = " style='text-align: left;' ";

$coun="Bolivia";

$style = " style='background-color: #DBCCC8;border-top-width: 2px;border-top-style: solid;border-top-color: #703E18; border-bottom-width: 2px;border-bottom-style: solid;border-bottom-color: #703E18; text-align: left; color: #552211; font-family: Arial, Helvetica, sans-serif; font-size: 12px;' ";

while( $row = mysql_fetch_array($result) )
{

if($coun==$row['paiscafe']) {




} else{

$style3 = " style='background-color: #F0EAE8;'";

$table .= "<tr $style3 height=15>";
$table .= "<td>&nbsp;</td>";
$table .= "<td>&nbsp;</td>";
$table .= "<td>&nbsp;</td>";
$table .= "<td>&nbsp;</td>";
$table .= "<td>&nbsp;</td>";
$table .= "<td>&nbsp;</td>";
$table .= "<td>&nbsp;</td>";
$table .= "<td>&nbsp;</td>";
$table .= "</tr>";

}



$table .= "<tr $style height=22>";



$table .= "<td>".$row['paiscafe']."</td>";

if ($row['Grade'] == "Other")
{
$table .= "<td>".$row['certi']."</td>";
}else{
$table .= "<td>".$row['Grade']."</td>";
}

if ($row['Sacos1'] != "")
{
$table .= "<td $bgl>".$row['Sacos1']." bags</td>";
}else{
$table .= "<td $bgl>".$row['Sacos']." bags</td>";
}




if ($row['Precio'] == "1") {

if ($row['Lb1'] != "" ) {

$table .= "<td $bgl>US$ ".$row['Lb1']."</td>";

}else {

if ($row['Lb'] != "") {

$table .= "<td $bgl>US$ ".$row['Lb']."</td>";

}else {

$table .= "<td $bgl>Negotiable</td>";

}

}

}else {

if ($row['Ny1'] != "" ) {

if ($row['PrecioMinimo1'] != "" && $row['PrecioMinimo1'] != "opcional" && $row['PrecioMinimo1'] != "-" && $row['PrecioMinimo1'] != "--") {

$table .= "<td $bgl>NY+".$row['Ny1'].", min US$ ".$row['PrecioMinimo1']."</td>";

}else {

$table .= "<td $bgl>NY+".$row['Ny1']."</td>";

}

}else if($row['Ny'] != "" ){

if ($row['PrecioMinimo'] != "" && $row['PrecioMinimo'] != "opcional" && $row['PrecioMinimo'] != "-" && $row['PrecioMinimo'] != "--") {

$table .= "<td $bgl>NY+".$row['Ny'].", min US$ ".$row['PrecioMinimo']."</td>";

}else {

$table .= "<td $bgl>NY+".$row['Ny']."</td>";

}

} else {

$table .="<td $bgl>Negotiable</td>";

}
}

if ($row['Embarque'] != "" and $row['Embarque1'] != "")
{
$table .= "<td $bgl>".$row['Embarque']." - ".$row['Embarque1']."</td>";
}else{
$table .= "<td $bgl>".$row['Embarque']."</td>";
}



if ($row['Calidad'] == "Otros" )
{
$table .= "<td $bgl>".$row['cali']."</td>";
}else{
$table .= "<td $bgl>".$row['Calidad']."</td>";
}


if ($row['Altura']=="" && $row['Altura1']=="") {

$table .= "<td $bgc>&nbsp;</td>";

}else{

$table .= "<td $bgc>".$row['Altura']." - ".$row['Altura1']." </td>";

}

$table .= "<td>".$row['Observaciones']."</td>";


$table .= "</tr>";

$coun=$row['paiscafe'];





} // reemplazamos [*BODY*] por el contenido de $table
$contenido = str_replace("[*BODY*]",$table,$contenido);



}


// inicio del envio del mail.
$to = $_POST['para'];
$subject = $_POST['asunto'];
$message = $contenido;
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

/* Additional headers
$headers .= 'To: ' .$_POST['para']. "\r\n";
$headers .= 'From: <[email protected]>' . "\r\n";*/

mail($to, $subject, $message, $headers);

// echo $message; esto era para ver si habia valores.

echo "<script type = 'text/javascript'>";
echo ' document.location.href = "contact-form-tk.html"; ';
echo "</script>";
//}
}


$result = $_SESSION['db']->getOferta($_GET['Id']);
$row = mysql_fetch_array($result);
$idusuario = $row['IdUsuario'];
$correo =$_SESSION['db']->getCorreo($idusuario);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="cssFiles/styles.css" rel="stylesheet" type="text/css" />
<script type = "text/javascript" src = "Js/jquery.js" ></script>
<script type = "text/javascript" src = "Js/appQuery.js" ></script>
<script type = "text/javascript" src = "Js/facebox/facebox.js" ></script>
</head>

<body>
<div id="contactform">
<form id="form1" name="form1" method="post" action="">
<input type = "hidden" name = "compraventa" value = "2"/>
<p>Para
<input type = "text" name = "para" id = "para" value = "[email protected]"/>

<!--<select name="compraventa" id = "compraventa" onchange = "SetThis()" id="compraventa">
<option value = "0">seleccione</option>
<option value = "1">vendedor</option>
<option value = "2">comprador</option>
</select>-->
</p>
<!-- <p id = "FIELD">&nbsp;</p>
<p>Para
<input name="para" type="text" id="para" />
</p>-->
<p>
Asunto
<input type="text" name="asunto" id="asunto" />
</p>

<p>
Ordernar por :
<br />
Pais :
<input name="ordenar" type="radio" style = "width:10px" value = "fecha" checked/>
Fecha :
<input name="ordenar" type="radio" style = "width:10px" value = "pais"/>
</p>

<p>Comentarios
<textarea name="mensaje" id="mensaje" cols="45" rows="5"></textarea>
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</div>
</body>
</html>
  #3 (permalink)  
Antiguo 07/07/2010, 09:44
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Tema movido desde PHP orientado a objetos a PHP
  #4 (permalink)  
Antiguo 07/07/2010, 10:29
Avatar de shakaran  
Fecha de Ingreso: agosto-2005
Ubicación: España - Ciudad Real
Mensajes: 374
Antigüedad: 18 años, 8 meses
Puntos: 7
Respuesta: Envio de Mail HTML con consultas a una BD con PHP

Fíjate que lo que estas mandando por email es el contenido del archivo emall_stock.php, pero no estas metiendo dentro nada del resultado de una base de datos.

Código PHP:
Ver original
  1. $contenido = file_get_contents("email_stock.php");

Puedes estar tranquilo con tu hosting, el problema esta en el código. No esta programado lo que dices o quieres hacer.

PD: Por favor resalta el coloreado de sintaxis la próxima vez que postees código y edita el primer post si tienes que añadir más texto en lugar de publicar otro post.
__________________
Quijost Backend Engineer - www.quijost.com - Hosting rápido, eficiente y profesional
Blog: www.shakaran.net
  #5 (permalink)  
Antiguo 07/07/2010, 11:30
 
Fecha de Ingreso: mayo-2008
Ubicación: Lima
Mensajes: 11
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: Envio de Mail HTML con consultas a una BD con PHP

Muchas gracias shakaran, como verás soy relativamente nuevo en este foro asi que tomaré tus recomendaciones para la proxima vez.

Está bien lo que dices...
$contenido = file_get_contents("email_stock.php");
con esta linea tome el contenido de email_stock.php; pero con esta:
$contenido = str_replace("[*BODY*]",$table,$contenido); reemplazo la parte [*BODY*] de email_stock.php por lo que acumulé en la variable $table, que es donde tengo los resultados para mostrar del query a la bd. No se si esto está bien de hacerlo o hay otra forma de hacerlo.

Muchas gracias por tu ayuda.
  #6 (permalink)  
Antiguo 07/07/2010, 11:45
Avatar de shakaran  
Fecha de Ingreso: agosto-2005
Ubicación: España - Ciudad Real
Mensajes: 374
Antigüedad: 18 años, 8 meses
Puntos: 7
Respuesta: Envio de Mail HTML con consultas a una BD con PHP

Y porque por ejemplo no te creas una funcion llamada send_email() o similar donde le pases un parámetro llamado $contenido que tenga los datos que necesites.

Tampoco veo muy necesario usar otro archivo para traerte los datos a menos que sea un email de páginas y páginas.
__________________
Quijost Backend Engineer - www.quijost.com - Hosting rápido, eficiente y profesional
Blog: www.shakaran.net
  #7 (permalink)  
Antiguo 07/07/2010, 11:50
 
Fecha de Ingreso: mayo-2008
Ubicación: Lima
Mensajes: 11
Antigüedad: 15 años, 10 meses
Puntos: 0
Respuesta: Envio de Mail HTML con consultas a una BD con PHP

voy a intentarlo como me dices shakaran... creando la funcion send-mail($contenido)... cualquier cosa les digo como me fué.

Pero si por ahi se les osurre algo o tienes alguna otra idea de solucion, agradecería mucho me la hagan saber.

Etiquetas: bd, envio, html, mail
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:54.