SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help With Sending A HTML Email

    Can anyone help me with this code, I was just emailing text but it's becoming quit messy so thought I'd try and send it as a HTML, I have been able to format it and I can get a result from the top of the page, but how do I combine that with all my php & SQL Query?

    PHP Code:
    <?php 
    mail
    ('me@mydomain.com''Subject'
        
    '<html><body><p>Your <i>message</i> here.</p></body></html>'
        
    "To: The Receiver <me@mydomain.com>\n" 
        
    "From: The Sender <sender@some.net>\n" 
        
    "MIME-Version: 1.0\n" 
        
    "Content-type: text/html; charset=iso-8859-1");


    /* connect to the mysql database and use a query to get the members info */

    include 'library/config.php';
    include 
    'library/opendb.php';

    $p explode ' 'date('m Y') ); 

    if (
    $p[0] == 1) { 
        
    $Month 12
        
    $theYear $p[1]-1


    else { 
        
    $Month $p[0]-1
        
    $theYear $p[1];


    if (
    $Month 10) {
    $theMonth '0'.$Month;
    }

    else {
    $theMonth $Month;
    }

    //$results = mysql_query("SELECT * FROM `tblmembers` WHERE `memberApproved` = 'P'");
    $results mysql_query("SELECT tblmembers.*, tblrepresentatives.rep_Firstname,tblrepresentatives.rep_Lastname FROM tblmembers LEFT JOIN tblrepresentatives ON (tblmembers.rep_NBR = tblrepresentatives.rep_NBR) WHERE tblmembers.memberApproved = 'P'");


    if (
    mysql_num_rows($results) < 1) {
    die(
    'No members were approved last month');
    }

    else {

      while (
    $qry mysql_fetch_array($results)) {
          if (
    strtotime($qry['loginDateTime']) <= (time() + 86400*31)) {
              
    $login .= '<b> Name </b>' .$qry["FirstName"].' '.$qry["LastName"].
    Application Date:'
    .date("d/m/Y"strtotime($qry["JoinDate"])).' '.$qry["Email"].' '.$qry["rep_Firstname"].' '.$qry["rep_Lastname"].' ('.$qry["State"].')
    --------------------
    '
    ;
          }
      }

    $message = <<<HERE
    The login information is as follows:
    -----------------------------------------

    $login
    HERE;

    $from "<me@mydomain.com>";

    mail($to$subject$message'From: '.$from);
    }

    ?>

  2. #2
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tomyknoker View Post
    Can anyone help me with this code, I was just emailing text but it's becoming quit messy so thought I'd try and send it as a HTML, I have been able to format it and I can get a result from the top of the page, but how do I combine that with all my php & SQL Query?
    stick this right at the end after you've put your query results together -
    PHP Code:
    $message sprintf("<html><body>The login information is as follows:<br>-----------------------------------------<br>%s</body></html>",$login);
    $to "me@mydomain.com";
    $subject "My Test Email";
    $headers "FROM: <me@mydomain.com>\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    mail($to,$subject,$message,"$headers"); 

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha that worked a treat! But noticed that my Email of emmbers does not automatically get sent as a link, I obviously have to add an <a href> in there but where would I put it? Inside the Email $qry?

  4. #4
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tomyknoker View Post
    Ha that worked a treat! But noticed that my Email of emmbers does not automatically get sent as a link, I obviously have to add an <a href> in there but where would I put it? Inside the Email $qry?
    I would change the line -
    PHP Code:
    $login .= '<b> Name </b>' .$qry["FirstName"].' '.$qry["LastName"].
    Application Date:'
    .date("d/m/Y"strtotime($qry["JoinDate"])).' '.$qry["Email"].' '.$qry["rep_Firstname"].' '.$qry["rep_Lastname"].' ('.$qry["State"].') 
    to -
    PHP Code:
    $login .= sprintf("<b>Name</b>%s %s Application Date:%s <a href='mailto:%s'>%s</a> %s %s (%s)",
            
    $qry["FirstName"],
            
    $qry["LastName"],
            
    date("d/m/Y"strtotime($qry["JoinDate"])),
            
    $qry["Email"],
            
    $qry["Email"],
            
    $qry["rep_Firstname"],
            
    $qry["rep_Lastname"],
            
    $qry["State"]); 
    also.. what's up with all the date stuff? are those columns of DATETIME type in the database? You could really trim down much of that code by making better use of mysql functions.

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean with the time and date, oh I only want members in the past month...

    Not sure what I am doing wrong, but can't get it to display correct...

    PHP Code:
      while ($qry mysql_fetch_array($results)) {
          if (
    strtotime($qry['loginDateTime']) <= (time() + 86400*31)) {
            
    $login .= sprintf("<p><b>Name</b>%s %s <a href='mailto:%s'>(%s)</a><br /> %s %s (%s)</p>"
            
    $qry["FirstName"], 
            
    $qry["LastName"],  
            
    $qry["Email"],
            
    $qry["State"],
            
    date("d/m/Y"strtotime($qry["JoinDate"])),
            
    $qry["rep_Firstname"], 
            
    $qry["rep_Lastname"]);
          }
      } 
    Name: FirstName LastName, Email (State)
    Application Date: JoinDate
    Rep: rep_Firstname rep_Lastname

    Can't seem to get it like that?

    Also I am trying to put this in the rep_Firstname rep_Lastname part but can't get it to work... This code
    PHP Code:
    ($q['loginDateTime']?date('d/m/Y H:i:s'strtotime($q['loginDateTime'])):'unknown'
    Puts the word unknown, if there is no logindatetime in the datatbase column, I want to do this if there is no rep_Firstname rep_Lastname... Any ideaS?

  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also what's safe to delete out of the original file? I tried to delete the original mailer code but then it doesn't work... So now my page looks like this

    PHP Code:
    <?php 
    mail
    ('me@mydomain.com''Subject'
        
    '<html><body><p>Your <i>message</i> here.</p></body></html>'
        
    "To: The Receiver <me@mydomain.com>\n" 
        
    "From: The Sender <sender@some.net>\n" 
        
    "MIME-Version: 1.0\n" 
        
    "Content-type: text/html; charset=iso-8859-1");


    /* connect to the mysql database and use a query to get the members info */

    include 'library/config.php';
    include 
    'library/opendb.php';

    $p explode ' 'date('m Y') ); 

    if (
    $p[0] == 1) { 
        
    $Month 12
        
    $theYear $p[1]-1


    else { 
        
    $Month $p[0]-1
        
    $theYear $p[1];


    if (
    $Month 10) {
    $theMonth '0'.$Month;
    }

    else {
    $theMonth $Month;
    }

    //$results = mysql_query("SELECT * FROM `tblmembers` WHERE `memberApproved` = 'P'");
    $results mysql_query("SELECT tblmembers.*, tblrepresentatives.rep_Firstname,tblrepresentatives.rep_Lastname FROM tblmembers LEFT JOIN tblrepresentatives ON (tblmembers.rep_NBR = tblrepresentatives.rep_NBR) WHERE tblmembers.memberApproved = 'P'");


    if (
    mysql_num_rows($results) < 1) {
    die(
    'No members were approved last month');
    }

    else {

      while (
    $qry mysql_fetch_array($results)) {
          if (
    strtotime($qry['loginDateTime']) <= (time() + 86400*31)) {
              
    $login .= '<b> Name </b>' .$qry["FirstName"].' '.$qry["LastName"].
    Application Date:'
    .date("d/m/Y"strtotime($qry["JoinDate"])).' '.$qry["Email"].' '.$qry["rep_Firstname"].' '.$qry["rep_Lastname"].' ('.$qry["State"].')
    --------------------
    '
    ;
          }
      }

    $message = <<<HERE
    The login information is as follows:
    -----------------------------------------

    $login
    HERE;

    $from "<me@mydomain.com>";

    mail($to$subject$message'From: '.$from);
    }

    $message sprintf("<html><body>The login information is as follows:<br>-----------------------------------------<br>%s</body></html>",$login); 
    $to "me@mydomain.com"
    $subject "My Test Email"
    $headers "FROM: <me@mydomain.com>\r\n"
    $headers .= "MIME-Version: 1.0\r\n"
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"
    mail($to,$subject,$message,"$headers"); 

    ?>

  7. #7
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tomyknoker View Post
    You mean with the time and date, oh I only want members in the past month...
    Then you should only select those users from the database with a query like this -
    SELECT tblmembers.* FROM tblmembers WHERE JoinDate > DATE_SUB(CURRENT_DATE, INTERVAL 1 MONTH)

    But you can only use such a query if you've correctly set your database table up with appropriate DATETIME column types.

    Not sure what I am doing wrong, but can't get it to display correct...

    Name: FirstName LastName, Email (State)
    Application Date: JoinDate
    Rep: rep_Firstname rep_Lastname

    Can't seem to get it like that?
    You just need to put the linebreaks in -
    PHP Code:
    $login .= sprintf("<b>Name</b>%s %s, <a href='mailto:%s'>%s</a> (%s)<br>Application Date:%s<br>%s %s",
            
    $qry["FirstName"],
            
    $qry["LastName"],
            
    $qry["Email"],
            
    $qry["Email"],
            
    $qry["State"],
            
    date("d/m/Y"strtotime($qry["JoinDate"])),
            
    $qry["rep_Firstname"],
            
    $qry["rep_Lastname"]
            ); 
    Also I am trying to put this in the rep_Firstname rep_Lastname part but can't get it to work... This code ... Puts the word unknown, if there is no logindatetime in the datatbase column, I want to do this if there is no rep_Firstname rep_Lastname... Any ideaS?
    try this -
    PHP Code:
    if (empty ($qry["rep_Firstname"])) $repFirstname "uknown";
    else 
    $repFirstname $qry["rep_Firstname"];

    if (empty (
    $qry["rep_Lastname"])) $repLastname "unknown";
    else 
    $repLastname $qry["rep_Lastname"];

    $login .= sprintf("<b>Name</b>%s %s, <a href='mailto:%s'>%s</a> (%s)<br>Application Date:%s<br>%s %s",
            
    $qry["FirstName"],
            
    $qry["LastName"],
            
    $qry["Email"],
            
    $qry["Email"],
            
    $qry["State"],
            
    date("d/m/Y"strtotime($qry["JoinDate"])),
            
    $repFirstname,
            
    $repLastname
            
    ); 

  8. #8
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tomyknoker View Post
    Also what's safe to delete out of the original file? I tried to delete the original mailer code but then it doesn't work
    Here's the whole thing -
    PHP Code:
    <?php 
    /* connect to the mysql database and use a query to get the members info */

    include 'library/config.php';
    include 
    'library/opendb.php';

    /* Dunno what this stuff is.. doesn't seem to be used anywhere */
    $p explode ' 'date('m Y') ); 
    if (
    $p[0] == 1) { 
        
    $Month 12
        
    $theYear $p[1]-1
    } else { 
        
    $Month $p[0]-1
        
    $theYear $p[1];

    if (
    $Month 10) {
        
    $theMonth '0'.$Month;
    } else {
        
    $theMonth $Month;
    }

    $query"SELECT tblmembers.*, 
            tblrepresentatives.rep_Firstname,
            tblrepresentatives.rep_Lastname 
            FROM tblmembers 
            LEFT JOIN tblrepresentatives ON (tblmembers.rep_NBR = tblrepresentatives.rep_NBR) 
            WHERE tblmembers.memberApproved = 'P'"
    ;

    if (
    $results mysql_query($query)) {
        if (
    mysql_num_rows($results) < 1) {
            die(
    'No members were approved last month'); // This is a bit extreme don't you think?
                                                        // killing the app just because the query returned no results!
        
    } else {
            while (
    $qry mysql_fetch_array($results)) {
                if (
    strtotime($qry['loginDateTime']) <= (time() + 86400*31)) {
                    if (empty (
    $qry["rep_Firstname"])) $repFirstname "uknown"
                    else 
    $repFirstname $qry["rep_Firstname"]; 

                    if (empty (
    $qry["rep_Lastname"])) $repLastname "unknown"
                    else 
    $repLastname $qry["rep_Lastname"]; 

                    
    $login .= sprintf("<b>Name</b>%s %s, <a href='mailto:%s'>%s</a> (%s)<br>Application Date:%s<br>%s %s"
                    
    $qry["FirstName"], 
                    
    $qry["LastName"], 
                    
    $qry["Email"], 
                    
    $qry["Email"], 
                    
    $qry["State"], 
                    
    date("d/m/Y"strtotime($qry["JoinDate"])), 
                    
    $repFirstname
                    
    $repLastname 
                    
    );
                }
            }
        }
    }           
    $message sprintf("<html>
                        <body>
                        The login information is as follows:<br>
                        -----------------------------------------<br>
                        %s
                        </body>
                        </html>"
    ,$login);
                        
    $to "me@mydomain.com"//change this to your email address.
    $subject "My Test Email"
    $headers "FROM: <me@mydomain.com>\r\n"// change this to a valid email address.
    $headers .= "MIME-Version: 1.0\r\n"
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"
    mail($to,$subject,$message,"$headers"); 

    ?>

  9. #9
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heya catweasel... Ok I have been playing with another one, actually the date stuff all it does is just give me the info for the previous month, so last month. Is there a better way to do it?

    PHP Code:
    <?php

    /* connect to the mysql database and use a query to get the members info */

    include 'library/config.php';
    include 
    'library/opendb.php';

    $date date('d/m/Y');
    $p explode ' 'date('m Y') ); 

    if (
    $p[0] == 1) { 
        
    $Month 12
        
    $theYear $p[1]-1


    else { 
        
    $Month $p[0]-1
        
    $theYear $p[1];


    if (
    $Month 10) {
    $theMonth '0'.$Month;
    }

    else {
    $theMonth $Month;
    }

    //$results = mysql_query("SELECT * FROM `tblmembers` WHERE `loginDateTime` LIKE '$theYear-$theMonth%'");

    $query "SELECT tblmembers.*, 
            tblrepresentatives.rep_Firstname, 
            tblrepresentatives.rep_Lastname 
            FROM tblmembers 
            LEFT JOIN tblrepresentatives ON (tblmembers.rep_NBR = tblrepresentatives.rep_NBR)
            WHERE tblmembers.loginDateTime LIKE '
    $theYear-$theMonth%'"

    if (
    $results mysql_query($query)) { 
        if (
    mysql_num_rows($results) < 1) { 
            die(
    'No members logged in last month');

        } else { 
            while (
    $qry mysql_fetch_array($results)) {
                    if (
    strtotime($qry['loginDateTime']) <= (time() + 86400*31))
            
                    if (empty (
    $qry["rep_Firstname"])) $repFirstname "uknown";
                    else 
    $repFirstname $qry["rep_Firstname"];

                    if (empty (
    $qry["rep_Lastname"])) $repLastname "unknown";
                       else 
    $repLastname $qry["rep_Lastname"];
      
      
    $login .= sprintf("<p><strong>Name:</strong> %s %s, <a href='mailto:%s'>%s</a><br /><strong>DOB:</strong> %s<br /> <strong>State:</strong> %s<br /> <strong>Representative:</strong> %s %s<br /><strong>Last Login Date:</strong> %s<br /></p>",
                    
    $qry["FirstName"], 
                    
    $qry["LastName"], 
                    
    $qry["Email"], 
                    
    $qry["Email"],  
                    
    date("d/m/Y H:i:s"strtotime($qry["loginDateTime"])),
                    
    $qry["State"],
                    
    $repFirstname
                    
    $repLastname,
                    
    date("d/m/Y H:i:s"strtotime($qry["loginDateTime"]))
                    ); 
                } 
            } 
        }         
    $message sprintf("<html> 
                        <body> 
                        The following logged in during 
    $theMonth.<br />
                        Member information is as follows:<br />
                        -----------------------------------------<br />
                        %s 
                        </body> 
                        </html>"
    ,$login); 
                         
    $to "me@mydomain.com"
    $subject $subject .'Members Logged In During '$theMonth;
    $headers "FROM: me@mydomain.com\r\n"
    $headers .= "MIME-Version: 1.0\r\n"
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"
    mail($to,$subject,$message,"$headers");

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •