lam3 problem/question about formatting text

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
n0ob
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2003 6:54 am

lam3 problem/question about formatting text

Post by n0ob »

hi,

i have preety lam3 question about formatting text in my php script. point of the code below is to get ip of the submitter and sent his ip to his email (it's stupid cause i modified script to be as small as possible)

Code: Select all

<?
function getIP() &#123;
$ip;
if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
else $ip = "UNKNOWN";
return $ip;
&#125;

$message .= "\nYour IP is: ".getIP();
$message .= "\n\nThanks";


require("class.smtp.php"); 
$smtp=new smtp(); 
$smtp->host_name = "localhost"; 
$smtp->localhost = "localhost.domain"; 

$frm = "me@mysite.com";
$to = $Mail; 
$subject = "some subject"; 


$smtp->SendMessage( $frm, array( $to ), array( "From: $frm", "To: $to", "Subject: $subject" ), $message );

header ("Location: http://www.mysite.com/submitted.html");
?>
Problem is in the word "Thanks". I want it to be bold style but i have problems doing that. I tried all of this things below, but they didnt return bold text at all, they just send email with all those caracters (<strong>, </strong>, <b>, </b> etc.)

Code: Select all

$message .= "<b style="color:red;">" Thanks "</b>";

Code: Select all

$message .= "<strong>Thanks</strong>"

Code: Select all

$message .= "<b>Thanks</b>"

Code: Select all

$message .= "\bThanks\b0"
So my question is how should that line of code look so the text be really bold? And, if its possible, can I set it to be Verdana font, size 10?

Thanks in advance,
John
Flood
Forum Newbie
Posts: 11
Joined: Wed Jun 25, 2003 4:52 pm

Post by Flood »

Hi!

Try to add a heading like "Content-type: text/html;" to your e-mail... It should work!

/Flood
n0ob
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2003 6:54 am

Post by n0ob »

u mean that i should add

Code: Select all

$message = "Content-type: text/html;"
or how?
Flood wrote:Hi!

Try to add a heading like "Content-type: text/html;" to your e-mail... It should work!

/Flood
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

you will probably need to add it to this line somehow

Code: Select all

$smtp->SendMessage( $frm, array( $to ), array( "From: $frm", "To: $to", "Subject: $subject" ), $message );
but you may need to change your SendMessage() function, but we will have to see the function to let you know for sure.
n0ob
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2003 6:54 am

Post by n0ob »

here is class.smtp.php file

Code: Select all

<?
/*********************************************************************
*       class SMTP
*
*********************************************************************/
if ( !defined('_SMTP_CLASS_') ) &#123;
        define('_SMTP_CLASS_', TRUE);

class smtp &#123;
 var $host_name="";
 var $host_port=25;
 var $localhost="";
 var $timeout=0;
 var $error="";
 var $debug=0;
 var $esmtp=1;
 var $esmtp_host="";
 var $esmtp_extensions=array();
 var $maximum_piped_recipients=100;

 /* private variables - DO NOT ACCESS */

 var $state="Disconnected";
 var $connection=0;
 var $pending_recipients=0;

 /* Private methods - DO NOT CALL */

 Function OutputDebug($message)
 &#123;
  echo $message,"\n";
 &#125;

 Function GetLine()
 &#123;
  for($line="";;)
  &#123;
   if(feof($this->connection))
   &#123;
    $this->error="reached the end of stream while reading from socket";
    return(0);
   &#125;
   if(($data=fgets($this->connection,100))==false)
   &#123;
    $this->error="it was not possible to read line from socket";
    return(0);
   &#125;
   $line.=$data;
   $length=strlen($line);
   if($length>=2
   && substr($line,$length-2,2)=="\r\n")
   &#123;
    $line=substr($line,0,$length-2);
    if($this->debug)
     $this->OutputDebug("< $line");
    return($line);
   &#125;
  &#125;
 &#125;

 Function PutLine($line)
 &#123;
  if($this->debug)
   $this->OutputDebug("> $line");
  if(!fputs($this->connection,"$line\r\n"))
  &#123;
   $this->error="it was not possible to write line to socket";
   return(0);
  &#125;
  return(1);
 &#125;

 Function PutData($data)
 &#123;
  if(strlen($data))
  &#123;
   if($this->debug)
    $this->OutputDebug("> $data");
   if(!fputs($this->connection,$data))
   &#123;
    $this->error="it was not possible to write data to socket";
    return(0);
   &#125;
  &#125;
  return(1);
 &#125;

 Function VerifyResultLines($code,$responses="")
 &#123;
  if(GetType($responses)!="array")
   $responses=array();
  Unset($match_code);
  while(($line=$this->GetLine($this->connection)))
  &#123;
   if(IsSet($match_code))
   &#123;
    if(strcmp(strtok($line," -"),$match_code))
    &#123;
     $this->error=$line;
     return(0);
    &#125;
   &#125;
   else
   &#123;
    $match_code=strtok($line," -");
    if(GetType($code)=="array")
    &#123;
     for($codes=0;$codes<count($code) && strcmp($match_code,$code&#1111;$codes]);$codes++);
     if($codes>=count($code))
     &#123;
      $this->error=$line;
      return(0);
     &#125;
    &#125;
    else
    &#123;
     if(strcmp($match_code,$code))
     &#123;
      $this->error=$line;
      return(0);
     &#125;
    &#125;
   &#125;
   $responses&#1111;]=strtok("");
   if(!strcmp($match_code,strtok($line," ")))
    return(1);
  &#125;
  return(-1);
 &#125;

 Function FlushRecipients()
 &#123;
  if($this->pending_sender)
  &#123;
   if($this->VerifyResultLines("250")<=0)
    return(0);
   $this->pending_sender=0;
  &#125;
  for(;$this->pending_recipients;$this->pending_recipients--)
  &#123;
   if($this->VerifyResultLines(array("250","251"))<=0)
    return(0);
  &#125;
  return(1);
 &#125;

 /* Public methods */

 Function Connect()
 &#123;
  $this->error=$error="";
   $this->esmtp_host="";
   $this->esmtp_extensions=array();
  if(!($this->connection=($this->timeout ? fsockopen($this->host_name,$this->host_port,&$errno,&$error,$this->timeout) : fsockopen($this->host_name,$this->host_port))))
  &#123;
   switch($error)
   &#123;
    case -3:
     $this->error="-3 socket could not be created";
     return(0);
    case -4:
     $this->error="-4 dns lookup on hostname "".$host_name."" failed";
     return(0);
    case -5:
     $this->error="-5 connection refused or timed out";
     return(0);
    case -6:
     $this->error="-6 fdopen() call failed";
     return(0);
    case -7:
     $this->error="-7 setvbuf() call failed";
     return(0);
    default:
     $this->error=$error." could not connect to the host "".$this->host_name.""";
     return(0);
   &#125;
  &#125;
  else
  &#123;
   if(!strcmp($localhost=$this->localhost,"")
   && !strcmp($localhost=getenv("SERVER_NAME"),"")
   && !strcmp($localhost=getenv("HOST"),""))
     $localhost="localhost";
    $success=0;
    if($this->VerifyResultLines("220")>0)
    &#123;
     if($this->esmtp)
     &#123;
      $responses=array();
      if($this->PutLine("EHLO $localhost")
      && $this->VerifyResultLines("250",&$responses)>0)
      &#123;
       $this->esmtp_host=strtok($responses&#1111;0]," ");
       for($response=1;$response<count($responses);$response++)
       &#123;
        $extension=strtoupper(strtok($responses&#1111;$response]," "));
        $this->esmtp_extensions&#1111;$extension]=strtok("");
       &#125;
       $success=1;
      &#125;
     &#125;
     if(!$success
     && $this->PutLine("HELO $localhost")
     && $this->VerifyResultLines("250")>0)
      $success=1;
   &#125;
   if($success)
   &#123;
    $this->state="Connected";
    return(1);
   &#125;
   else
   &#123;
    fclose($this->connection);
    $this->connection=0;
    $this->state="Disconnected";
    return(0);
   &#125;
  &#125;
 &#125;

 Function MailFrom($sender)
 &#123;
  if(strcmp($this->state,"Connected"))
  &#123;
   $this->error="connection is not in the initial state";
   return(0);
  &#125;
  $this->error="";
  if(!$this->PutLine("MAIL FROM:<$sender>"))
   return(0);
  if(!IsSet($this->esmtp_extensions&#1111;"PIPELINING"])
  && $this->VerifyResultLines("250")<=0)
   return(0);
  $this->state="SenderSet";
  if(IsSet($this->esmtp_extensions&#1111;"PIPELINING"]))
   $this->pending_sender=1;
  $this->pending_recipients=0;
  return(1);
 &#125;

 Function SetRecipient($recipient)
 &#123;
  switch($this->state)
  &#123;
   case "SenderSet":
   case "RecipientSet":
    break;
   default:
    $this->error="connection is not in the recipient setting state";
    return(0);
  &#125;
  $this->error="";
  if(!$this->PutLine("RCPT TO:<$recipient>"))
   return(0);
  if(IsSet($this->esmtp_extensions&#1111;"PIPELINING"]))
  &#123;
   $this->pending_recipients++;
   if($this->pending_recipients>=$this->maximum_piped_recipients)
   &#123;
    if(!$this->FlushRecipients())
     return(0);
   &#125;
  &#125;
  else
  &#123;
   if($this->VerifyResultLines(array("250","251"))<=0)
    return(0);
  &#125;
  $this->state="RecipientSet";
  return(1);
 &#125;

 Function StartData()
 &#123;
  if(strcmp($this->state,"RecipientSet"))
  &#123;
   $this->error="connection is not in the start sending data state";
   return(0);
  &#125;
  $this->error="";
  if(!$this->PutLine("DATA"))
   return(0);
  if($this->pending_recipients)
  &#123;
   if(!$this->FlushRecipients())
    return(0);
  &#125;
  if($this->VerifyResultLines("354")<=0)
   return(0);
  $this->state="SendingData";
  return(1);
 &#125;

 Function PrepareData($data,&$output)
 &#123;
  $length=strlen(&$data);
  for($output="",$position=0;$position<$length;)
  &#123;
   $next_position=$length;
   for($current=$position;$current<$length;$current++)
   &#123;
    switch($data&#1111;$current])
    &#123;
     case "\n":
      $next_position=$current+1;
      break 2;
     case "\r":
      $next_position=$current+1;
      if($data&#1111;$next_position]=="\n")
       $next_position++;
      break 2;
    &#125;
   &#125;
   if($data&#1111;$position]==".")
    $output.=".";
   $output.=substr(&$data,$position,$current-$position)."\r\n";
   $position=$next_position;
  &#125;
 &#125;

 Function SendData($data)
 &#123;
  if(strcmp($this->state,"SendingData"))
  &#123;
   $this->error="connection is not in the sending data state";
   return(0);
  &#125;
  $this->error="";
  return($this->PutData(&$data));
 &#125;

 Function EndSendingData()
 &#123;
  if(strcmp($this->state,"SendingData"))
  &#123;
   $this->error="connection is not in the sending data state";
   return(0);
  &#125;
  $this->error="";
  if(!$this->PutLine("\r\n.")
  || $this->VerifyResultLines("250")<=0)
   return(0);
  $this->state="Connected";
  return(1);
 &#125;

 Function ResetConnection()
 &#123;
  switch($this->state)
  &#123;
   case "Connected":
    return(1);
   case "SendingData":
    $this->error="can not reset the connection while sending data";
    return(0);
   case "Disconnected":
    $this->error="can not reset the connection before it is established";
    return(0);
  &#125;
  $this->error="";
  if(!$this->PutLine("RSET")
  || $this->VerifyResultLines("250")<=0)
   return(0);
  $this->state="Connected";
  return(1);
 &#125;

 Function Disconnect($quit=1)
 &#123;
  if(!strcmp($this->state,"Disconnected"))
  &#123;
   $this->error="it was not previously established a SMTP connection";
   return(0);
  &#125;
  $this->error="";
  if(!strcmp($this->state,"Connected")
  && $quit
  && (!$this->PutLine("QUIT")
  || $this->VerifyResultLines("221")<=0))
   return(0);
  fclose($this->connection);
  $this->connection=0;
  $this->state="Disconnected";
  return(1);
 &#125;

 Function SendMessage($sender,$recipients,$headers,$body)
 &#123;
  if(($success=$this->Connect()))
  &#123;
   if(($success=$this->MailFrom($sender)))
   &#123;
    for($recipient=0;$recipient<count($recipients);$recipient++)
    &#123;
     if(!($success=$this->SetRecipient($recipients&#1111;$recipient])))
      break;
    &#125;
    if($success
    && ($success=$this->StartData()))
    &#123;
     for($header_data="",$header=0;$header<count($headers);$header++)
      $header_data.=$headers&#1111;$header]."\r\n";
     if(($success=$this->SendData($header_data."\r\n")))
     &#123;
      $this->PrepareData($body,&$body_data);
      $success=$this->SendData($body_data);
     &#125;
     if($success)
      $success=$this->EndSendingData();
    &#125;
   &#125;
   $error=$this->error;
   $disconnect_success=$this->Disconnect($success);
   if($success)
    $success=$disconnect_success;
   else
    $this->error=$error;
  &#125;
  return($success);
 &#125;

&#125;;

&#125; 
?>
n0ob
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2003 6:54 am

Post by n0ob »

maybe this part should be changed:

for($header_data="",$header=0;$header<count($headers);$header++)
$header_data.=$headers[$header]."\r\n";
if(($success=$this->SendData($header_data."\r\n")))

?
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

try

Code: Select all

$smtp->SendMessage( $frm, array( $to ), array( "From: $frm", "To: $to", "Subject: $subject","Content-type: text/html" ), $message );
the way the SendMessage function is written you should be able to get away with this.
Post Reply