Sending mails with PHP with tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsalgado
    New Member
    • Jul 2005
    • 2

    Sending mails with PHP with tables

    Hello,
    I´m trying to build a php program to send emails with tables with variables into thems. I explain: the body of emails contens into a table 2x2 and the first line have titles: (Name, Lastname) and the second line, variables from a Mysql like $name and $lastname.
    Some one can help me to acomplish that! I´m lost.
    Thanks
    guillermo
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2433

    #2
    Here is a rough example of some code:
    Code:
     
    // asks database for all members and their information example e-mail, first name, last name
    $result = mysql_query("SELECT * FROM members");
     
    //run a loop to go through all members
    while ($row = mysql_fetch_array ($result)){
    	$first = $row[firstname];
    	$last = $row[lastname];
    	$email = $row[email];
    	
    	// this is where you would have a template for your table/html
    	$html = "$first, $last, $email";   // example, i'm sure ur e-mail would be different
     
    	// use mail function to mail out
       mail();  // use php.net to see what the mail function required inputs are
    }
    There are other things u need to take in consideration, like subscriber list. If the list is long then you need to run this script in CLI mode. Also you need to to pay attention to how html e-mails are sent. HTML e-mails need different mime types.

    Quick search on "MIME e-mail PHP mail" should get you what you are looking for.

    Hope that helps.
    niheel @ bytes

    Comment

    • gsalgado
      New Member
      • Jul 2005
      • 2

      #3
      Sending mails with PHP with tables- explain

      Hi,
      Thanks for your fast response.
      All you code it´s very usefull for me, but i want to explain better my situation.
      I include some code:
      $mensaje = '
      <html>
      <head>
      <title>Recordat orio de Cumplea&ntilde; os para Agosto</title>
      </head>
      <body>
      <p>&iexcl;Aqu&i acute; est&aacute;n los cumplea&ntilde; os que llegan en Agosto!</p>
      <table>
      <tr>
      <th>Persona</th><th>D&iacute ;a</th><th>Mes</th><th>A&ntilde ;o</th>
      </tr>
      <tr>
      <td>Juan</td><td>3</td><td>August</td><td>1970</td>
      </tr>
      <tr>
      <td>Sandra</td><td>17</td><td>August</td><td>1973</td>
      </tr>
      </table>
      </body>
      </html>
      ';
      This inserts a 4 colums with 3 lines (Titles ans Data) like this
      Persona Día Mes Año
      Juan 3 August 1970
      Sandra 17 August 1973
      I´m looking to put all data into a table with borders, colors, etc and i´m trying to replace the names and dates to variables lik $name, $date, etc.
      It´s possible to do that? I´m checked some many documentation (PHP site, web forums but I cannot find and example o any tools to do that.
      Thanks a lot .
      guillermo

      Comment

      • ArtDerailed
        New Member
        • Sep 2006
        • 2

        #4
        Hello there,
        I too am having a somewhat similar problem and would very much appreciate some assistance. Through PHP, I have already set up my Flash actionscript to pass info and variables onto a MySQL database that is functionable in retrieving user info (name, email, subject, and comments). My only problem is that I need to have the information that the user sends when the click submit to also send an email to the owner with the same info. He wants to get an individual email for each individual who inputs their info and hits submit.

        I was trying to derive something from this code to get it to work, but nothing seemed to work:

        <?php
        $my_result = $_POST['myData'];

        $my_result = mail( "sales@longbeac hclothing.com", "LBC Site Inquiry: $clientSubject" ,
        "From: ".$clientNa me . "
        Email: ".$clientEm ail. "
        Subject: ".$clientSubjec t. "
        Message: ".$clientMessag e, "From: $clientEmail" );
        ?>

        Here is my current code. There are two files.
        The first is called insert.php
        <?php
        ini_set('displa y_error',1);
        ?>
        <?php
        $connection = mysql_pconnect( "localhost","lo ngbeach562","40 403141") or die('Could not connect: ' . mysql_error());
        mysql_select_db ("longbeach562" , $connection) or die('Could not select database' . mysql_error());
        ?>
        <?php
        $_name = $_REQUEST['name'];
        $_email = $_REQUEST['email'];
        $_subject = $_REQUEST['subject'];
        $_comments = $_REQUEST['comments'];

        if ($_name != '' && $_subject != '') {
        //if ($_name != '') {
        $SQL = "INSERT INTO `userInfo` (`name`, `email`, `subject`, `comments`) VALUES ('$_name', '$_email', '$_subject','$_ comments');";
        mysql_query($SQ L) or die('Query failed: ' . mysql_error());
        echo("&success= Thank you, your info has been received.");
        } else {
        echo("&success= Some of your info is missing!");
        }
        ?>

        This second on is called select.php:
        <?php
        ini_set('displa y_error',1);
        ?>
        <?php
        $connection = mysql_pconnect( "localhost","lo ngbeach562","40 403141") or die('Could not connect: ' . mysql_error());
        mysql_select_db ("longbeach562" , $connection) or die('Could not select database' . mysql_error());

        // no problems so query the db
        $SQL = "SELECT * FROM userInfo;";
        $result = mysql_query($SQ L) or die('Query failed: ' . mysql_error());
        ?>
        <?php
        $output = "<table border=\"1\">\n " .
        "<tr>\n" .
        "<td>Id</td>\n" .
        "<td>Name(s )</td>\n" .
        "<td>Email( s)</td>\n" .
        "<td>Subjec t?</td>\n" .
        "<td>Commen ts</td>\n" .
        "<td>Date & Time</td>\n" .
        "</tr>\n";

        while ($row = mysql_fetch_arr ay($result, MYSQL_NUM)) {
        $id = $row[0];
        $name = $row[1];
        $email = $row[2];
        $subject = $row[3];
        $comments = $row[4];
        $date = $row[5];
        $output .= "\t<tr>\n" .
        "\t\t<td>$i d</td>\n" .
        "\t\t<td>$n ame</td>\n" .
        "\t\t<td>$email </td>\n" .
        "\t\t<td>$subje ct</td>\n" .
        "\t\t<td>$comme nts</td>\n" .
        "\t\t<td>$d ate</td>\n" .
        "\t</tr>\n";
        }

        $output .= "</table>\n";
        ?>
        <?php echo($output) ?>
        <?php
        mysql_free_resu lt($result);
        ?>
        </body>
        </html>

        Comment

        • rkagrawal
          New Member
          • Aug 2006
          • 13

          #5
          Originally posted by gsalgado
          Hi,
          Thanks for your fast response.
          All you code it´s very usefull for me, but i want to explain better my situation.
          I include some code:
          $mensaje = '
          <html>
          <head>
          <title>Recordat orio de Cumplea&ntilde; os para Agosto</title>
          </head>
          <body>
          <p>&iexcl;Aqu&i acute; est&aacute;n los cumplea&ntilde; os que llegan en Agosto!</p>
          <table>
          <tr>
          <th>Persona</th><th>D&iacute ;a</th><th>Mes</th><th>A&ntilde ;o</th>
          </tr>
          <tr>
          <td>Juan</td><td>3</td><td>August</td><td>1970</td>
          </tr>
          <tr>
          <td>Sandra</td><td>17</td><td>August</td><td>1973</td>
          </tr>
          </table>
          </body>
          </html>
          ';
          This inserts a 4 colums with 3 lines (Titles ans Data) like this
          Persona Día Mes Año
          Juan 3 August 1970
          Sandra 17 August 1973
          I´m looking to put all data into a table with borders, colors, etc and i´m trying to replace the names and dates to variables lik $name, $date, etc.
          It´s possible to do that? I´m checked some many documentation (PHP site, web forums but I cannot find and example o any tools to do that.
          Thanks a lot .
          guillermo
          As far as i feel , rather than sending the mail , your actual problem is composing the body variable which has to be emailed.
          Suppose $body is the variable storing text to be emailed.

          $body = '<table border = 1><tr><td>Sno</td><td>Field 1</td><td>......</td></tr>';
          //now for keeping the variables just do this
          $body.= '<tr><td><?=$va r1?></td><td><?=$var2 ?></td><tr>';
          // Incase this is to be taken from database , just run a while($row=.... ) and put the above line in between the loops ensuring that var1 and var2 are provided the value you want to send.

          After the $body is complete , simply use
          mail() with other headers to send it.

          If you need more help with mail() function and sending HTML email or MIME-TYPE content , just drop a reply here.

          Kind Regards,
          Rahul Agrawal
          Developer
          Web2003 Corporation
          www.web2003corp .com

          Comment

          Working...