cannot mail

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

victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

cannot mail

Post by victor »

The following class is adapted from the php developer's cookbook used for sending HTML or attachments, I used it for my application to send out a mail but to no avail, can somebody pls advice.

Code: Select all

<?php

//'email_fns.php'

<?php

mt_srand((double)microtime()*1000000);

class MIME{

var $attachments = array();
var $to = "";
var $from = "";
var $subject = "";
var $body = "";


	function MIME($to, $from='', $subject='', $body='')
	{
	 $this->to = $to;
	 $this->from = $from;
	 $this->subject = $subject;
	 $this->body = $body;
	 
	 echo 'message assign, constructor used<br><br>';
	 echo 'to - '.$this->to.'<br><br>';
	 echo 'from - '.$this->from.'<br><br>';
	 echo 'subject - '.$this->subject.'<br><br>';
	 echo 'body - '.$this->body.'<br><br>';
	 	 
	}

	function attachment($name="", $contents="", $type="application/octet-stream", 
	                    $encoding = "base64")
	{
	 $this->attachments[] = array("filename"=>$name,
	 			      "type"    =>$type,
	 			      "encoding"=>$encoding,
	 			      "data"    =>$contents); 
	}


	function build()
	{
	 $boundary = 'b'.md5(uniquid(mt_rand()))+getmypid();
 
	 $ret.= 'Content-type:multipart/mixed; ';
	 $ret.= "boundary = $boundary\r\n\r\n";
	 $ret.= "This is a MIME encoded message.\r\n\r\n";
	 $ret.= "--$$boundary";
 
	 $ret.= "Content-type: text/plain\r\n";
	 $ret.= "Content-Transfer-Encoding: 8bit\r\n\r\n";
	 $ret.= $this->body."\r\n--$boundary";
 
 
 
	 foreach($this->attachments as $attachment)
	 {
	  $attachment[data]= base64_encode($attachment[data]);
	  $attachment[data]= chunk_split($attachment[data]);
  
	  $data = "Content-type: $attachment[type]".$attachments[filename]?
	  "; name = "$attachments[filename]"":""."\r\n"."Content-Transfer-Encoding:$attachments[encoding]".
	  "\r\n\r\n$attachment[data]\r\n";
	  
	  $ret.="\r\n--$data--$boundary";
	  
	  $ret.="--\r\n";
  
	  return($ret);
	 }
	}

	function send()
	{
	 return mail($this->to,$this->subject,'',$this->build());
	}

}


class HTML_Email extends MIME{

	function HTML_Email($to, $from, $subject)
	{
	  $this->MIME($to, $from, $subject);		  
	}

	function html_data($html)
	{
	  $this->attachment("",$html,"text/html");
	  print_r($this->attachments);
	}
	
	
}


//In my application 

[b]include_once('email_fns.php');

$mm = new HTML_Email('thessa29@yahoo.com','xyz','Class Update for 15 Oct to 30 Oct');
$mm->html_data($html);    
$mm->send();[/b]

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

does the mail function return false? If not, it may be that yahoo is filtering out your email. It may also be your use of nonquoted literal strings.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Can you send a simple e-mail?

Code: Select all

mail('thessa29@yahoo.com','test','test');
Mac
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

basic email works with the following

Code: Select all

<?php
function send_html($to, $subject, $message)
{

/* To send HTML mail, set the Content-type header. */
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

if(mail($to, $subject, $message, $headers))
return true;
else 
echo false;
}
?>

however, i cannot send any mails based on the if i instantiate the the object mm, I've trouble shoot the code by echoing the attributes of the class there's output, however the mail function inside the class just don't seem to be activated..
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

which line of the quote uses nonquoted literal strings? pls advice cos I 'm nubie..
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you added any debugging into the send() function in the MIME class? Maybe having a simple e-mail sent at the same time? Checking the output of $this->to,$this->subject and $this->build()?

Mac
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

yeah, there's nothing when I tried to echo build(), seems like the mail function in the MIME class has been crippled because of that. Any clues why is this so?
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

I spotted a typo error in the function build() (there are no source codes available, I've got to type line by line) the build function now becomes

Code: Select all

<?php
	function build()
	{
	 $boundary = 'b'.md5(uniquid(mt_rand()))+getmypid();
	 
	 if($this->from !="")
	 $ret = "From: ".$this->from ."\r\n";
 
	 $ret.= 'Content-type:multipart/mixed; ';
	 $ret.= "boundary = $boundary\r\n\r\n";
	 $ret.= "This is a MIME encoded message.\r\n\r\n";
	 $ret.= "--$$boundary";
 
	 $ret.= "Content-type: text/plain\r\n";
	 $ret.= "Content-Transfer-Encoding: 8bit\r\n\r\n";
	 $ret.= $this->body."\r\n--$boundary";
 
 
 
	 	foreach($this->attachments as $attachment)
	 	{
	 	 $attachment[data]= base64_encode($attachment[data]);
	 	 $attachment[data]= chunk_split($attachment[data]);
  	
	 	 $data = "Content-type: $attachment [type]".$attachments[filename]?"; name = "$attachments[filename]"":""."\r\n"."Content-Transfer-Encoding:$attachments[encoding]"."\r\n\r\n$attachment[data]\r\n";
	 	 
	 	 $ret.="\r\n--$data--$boundary";
	 	} 
	 	 $ret.="--\r\n";
  	 
	  return($ret);
	 
	}


?>
but the problem still persist.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Is your error reporting at it's highest level as if it's not it may be that something simple is being missed, add:

Code: Select all

ini_set('display_errors', 1);
error_reporting(E_ALL);
before this bit.

Code: Select all

include_once('email_fns.php');
Are you trying to send an HTML e-mail or an HTML page as an attachment?

Mac
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

I'm sending HTML as an attachment in this case, i've followed the highest level of error reporting and this is what i get:

Code: Select all

Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 51

Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 51

Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 52

Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 52

Notice: Use of undefined constant filename - assumed 'filename' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 54

Notice: Undefined variable: attachments in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 54

Notice: Undefined variable: attachments in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 54
From: xyz Content-type:multipart/mixed; boundary = b8c0aa9ff7af25e30bf72dae17f11f4d0 This is a MIME encoded message. --$b8c0aa9ff7af25e30bf72dae17f11f4d0Content-type: text/plain Content-Transfer-Encoding: 8bit --b8c0aa9ff7af25e30bf72dae17f11f4d0 --; name = ""--b8c0aa9ff7af25e30bf72dae17f11f4d0-- header - From: xyz Content-type:multipart/mixed; boundary = b8c0aa9ff7af25e30bf72dae17f11f4d0 This is a MIME encoded message. --$b8c0aa9ff7af25e30bf72dae17f11f4d0Content-type: text/plain Content-Transfer-Encoding: 8bit --b8c0aa9ff7af25e30bf72dae17f11f4d0 --; name = ""--b8c0aa9ff7af25e30bf72dae17f11f4d0-- 
Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 51

Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 51

Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 52

Notice: Use of undefined constant data - assumed 'data' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 52

Notice: Use of undefined constant filename - assumed 'filename' in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 54

Notice: Undefined variable: attachments in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 54

Notice: Undefined variable: attachments in C:\Program Files\Apache Group\Apache\htdocs\swimcentral\user\functions\email_fns.php on line 54
From: xyz Content-type:multipart/mixed; boundary = b0752fc3d2d6ba7b87c1e7053b8cb57f1 This is a MIME encoded message. --$b0752fc3d2d6ba7b87c1e7053b8cb57f1Content-type: text/plain Content-Transfer-Encoding: 8bit --b0752fc3d2d6ba7b87c1e7053b8cb57f1 --; name = ""--b0752fc3d2d6ba7b87c1e7053b8cb57f1--
by the way, the mail is working now, except with no content..
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

For the undefined constant notices check out the second link in my signature.

Mac
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

For the undefined variable error: you have called the variable $attachement in the foreach loop but try to access it as $attachments in the code.

Mac
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

made some changes to in the build() function by assign array values like:

Code: Select all

<?php
	 	 $TYPE = $attachment['type'];
	 	 $FILENAME = $attachment['filename'];
	 	 $ENCODING = $attachment['encoding'];
	 	 $DATA = $attachment['data']

?>
no more errors now.

except that the attachment does not appear in the received mail in yahoo..
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

What do you get if you echo out $this->build();?

Mac
victor
Forum Commoner
Posts: 65
Joined: Fri Feb 13, 2004 1:36 am

Post by victor »

I get this:

Code: Select all

<?php
From: xyz Content-type:multipart/mixed; boundary = bab345999c43fc80f755332a86328ddf6 This is a MIME encoded message. --$bab345999c43fc80f755332a86328ddf6Content-type: text/plain Content-Transfer-Encoding: 8bit --bab345999c43fc80f755332a86328ddf6 --; name = ""--bab345999c43fc80f755332a86328ddf6-- 

?>
Post Reply