Help with mail() please.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fcarpio
Forum Newbie
Posts: 4
Joined: Thu Mar 03, 2011 10:41 am

Help with mail() please.

Post by fcarpio »

Hello,
I am new to this and I need some help. I have a class that when I call one of its objects I can just echo the object that I return and it will print it on the screen, no problem. But when I want to call the same object to be used as the body of an email I run into trouble. Here is the code:

The object I am calling:

Code: Select all

    public function detailedReport($code)
    {
            $this->code = $code;
            $this->query = "SELECT
                            *
                            FROM
                            tbl_users, tbl_po_returns, tbl_po_trans
                            WHERE
                            tbl_users.piEmail = tbl_po_returns.piEmail
                            AND
                            tbl_po_returns.transNum = '$this->code'
                            AND
                            tbl_po_returns.transNum = tbl_po_trans.transNum
							LIMIT 0, 1";

            $this->result   =   mysql_query($this->query) OR die(mysql_error());
            $this->count    =   mysql_num_rows($this->result);

            if($this->count > 0)
            {
                    while($this->row = mysql_fetch_array($this->result))
                    {
                            $this->myReport="
                            <table width='575' border='1' cellspacing='3' cellpadding='3'>
                                    <th colspan='2' scope='col'><p>FROM</p></th>
                                    <th colspan='2' scope='col'><p>TO</p></th>
                              </tr>
                              <tr>
                                    <td align='left' style='width:20%'><p><strong>Name:</strong></p></td>
                                    <td align='left' style='width:30%'><p>".$this->row['piFirstName']." ".$this->row['piLastName']."</p></td>
                                    <td align='left' style='width:20%'><p><strong>Company Name:</strong></p></td>
                                    <td align='left' style='width:30%'><p>".$this->row['coName']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Bldng/Room:</strong></p></td>
                                    <td align='left'><p>".$this->row['roomNum']."</p></td>
                                    <td align='left'><p><strong>Attn:</strong></p></td>
                                    <td align='left'><p>".$this->row['repName']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Telephone:</strong></p></td>
                                    <td align='left'><p>".$this->row['piTelNum']."</p></td>
                                    <td align='left'><p><strong>Address:</strong></p></td>
                                    <td align='left'><p>".$this->row['coAdd1']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Email:</strong></p></td>
                                    <td align='left'><p><a href='mailto:".$this->row['piEmail']."@scripps.edu'>".$this->row['piEmail']."@scripps.edu</a></p></td>
                                    <td align='left'><p><strong>Address Cont'd:</strong></p></td>
                                    <td align='left'><p>".$this->row['coAdd2']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Date/Time:</strong></p></td>
                                    <td align='left'><p>".$this->row['transTime']."</p></td>
                                    <td align='left'><p><strong>Address Cont'd:</strong></p></td>
                                    <td align='left'><p>".$this->row['coAdd3']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Notes:</strong></p></td>
                                    <td align='left'><p></p></td>
                                    <td align='left'><p><strong>City, State, Zip:</strong></p></td>
                                    <td align='left'><p>".$this->row['coCity'].", ".$this->row['coState'].", ".$this->row['coZip']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left' colspan='2' rowspan='3'><p><font color='red'>".$this->row['notes']."</font></p></td>
                                    <td align='left'><p><strong>Country:</strong></p></td>
                                    <td align='left'><p>".$this->row['country']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Telephone:</strong></p></td>
                                    <td align='left'><p>".$this->row['coTel']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Email:</strong></p></td>
                                    <td align='left'><p><a href='mailto:".$this->row['repEmail']."'>".$this->row['repEmail']."</a></p></td>
                              </tr>
                              </table>
                              <br>
                              <table width='575' border='1' cellspacing='3' cellpadding='3'>
                              <tr>
                                    <th colspan='4' align='left' style='width:25%'><p>Shipping Information</p></th>
                              </tr>
                              <tr>
                                    <td align='left' style='width:20%'><p><strong>Carrier:</strong></p></td>
                                    <td align='left' style='width:30%'><p>".$this->row['carrier']."</p></td>
                                    <td align='left' style='width:20%'><p><strong>Billing:</strong></p></td>
                                    <td align='left' style='width:30%'><p>".$this->row['billing']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Tracking No.:</strong></p></td>
                                    <td align='left'><p>".$this->row['trackNum']."</p></td>
                                    <td align='left'><p><strong>Delivery Type:</strong></p></td>
                                    <td align='left'><p>".$this->row['shipType']."</p></td>
                              </tr>
                              <tr>
                                    <td align='left'><p><strong>Ice:</strong></p></td>
                                    <td align='left'><p>".$this->row['ice']."</p></td>
                                    <td align='left'><p><strong>Shipment Type:</strong></p></td>
                                    <td align='left'><p>TBD</p></td>
                              </tr>
                            </table><br>";

                            //echo $this->myReport;
                    }//while 1

                    $this->query="	SELECT
                                    *
                                    FROM
                                    tbl_po_trans
                                    WHERE
                                    tbl_po_trans.transNum = '$this->code'";

                    $this->result   =   mysql_query($this->query) OR die(mysql_error());

                            $this->myReport = $this->myReport."
                            <table width='575' border='1' cellspacing='3' cellpadding='3'>
                              <tr>
                                    <th style='width:10%' scope='col'><p>QTY</p></th>
                                    <th style='width:10%' scope='col'><p>Unit</p></th>
                                    <th style='width:10%' scope='col'><p>SKU.</p></th>
                                    <th style='width:50%' scope='col'><p>Description</p></th>
                                    <th style='width:10%' scope='col'><p>Price</p></th>
                                    <th style='width:10%' scope='col'><p>Total</p></th>
                              </tr>";

                            //echo $this->myReport;
                            
                    While($this->row = mysql_fetch_array($this->result))
                    {
                        $this->myReport = $this->myReport."
                              <tr>
                                    <td align='center'><p>".$this->row['qty']."</p></td>
                                    <td align='center'><p>".$this->row['unit']."</p></td>
                                    <td align='center'><p>".$this->row['catNum']."</p></td>
                                    <td align='left'><p>".$this->row['prodDesc']."</p></td>
                                    <td align='right'><p>".number_format($this->row['price'], 2, '.', '')."</p></td>
                                    <td align='right'><p>";
                                    //echo $this->myReport;

                                    //echo number_format($this->row['qty']*$this->row['price'], 2, '.', '');
									$this->myReport = $this->myReport.number_format($this->row['qty']*$this->row['price'], 2, '.', '');

                                    $this->myReport = $this->myReport."
                                    </p></td>
                               </tr>";
                            //echo $this->myReport;
                    }//while 2

                    //echo "</table>";
					$this->myReport = $this->myReport."</table>";
					return $this->myReport;
					

            }//if
            else
            {
                    echo "<p>Invalid Transaction Number</P>";
            }//else
	}
When I call it and use it like this it works well:

Code: Select all

$myReport   =   new myReport($username, $pageNum);
$printReport = $myReport->detailedReport($code);
echo $printReport;
But when I use it like this it fails:

Code: Select all

$transDetails = new myReport($_SESSION['piEmail'], $_SESSION['hour']);
$transDetails->detailedReport($transNum);

$to = $_SESSION['piEmail']."@scripps.edu";
$subject = "(".$_SESSION['shipType'].") - Shipping Transaction Confirmation";
$message = $transDetails;
$headers = "From: RTS Administrator <rtsadmin@scripps.edu> \r\n"."Content-type:text/html";
mail($to, $subject, $message, $headers);
The error I get is:
Warning: mail() expects parameter 3 to be string, object given in...

Needless to say the email does not get sent.

Any ideas? Thanks in advance.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help with mail() please.

Post by social_experiment »

Code: Select all

<?php
$transDetails = new myReport($_SESSION['piEmail'], $_SESSION['hour']);

$to = $_SESSION['piEmail']."@scripps.edu";
$subject = "(".$_SESSION['shipType'].") - Shipping Transaction Confirmation";
// modify your code like this
$message = $transDetails->detailedReport($transNum);
$headers = "From: RTS Administrator <rtsadmin@scripps.edu> \r\n"."Content-type:text/html";
mail($to, $subject, $message, $headers); 
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply