Re: Help Sending Mail
| From: | Jim Lucas | Date: | Tue, 11 Dec 2007 05:04:05 +0000 |
| Subject: | Re: Help Sending Mail | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-265661@lists.php.net to get a copy of this message | ||
Me2resh Lists wrote:
Dear All, i wrote this class below to send Mail. but when i use it. i get the sender No Body. and from the address of the server, not the From address that i specify. and the MIME Headers as text in the message itself. and the attachment is pasted as binary text also. can anyone help please ?? here is the code : -------------------------------------------------------------------- <?php $Mail = new MainClass(); $Mail->Construct("Ahmed", "me2resh.lists@gmail.com", "Test Mail", "Hello, this is my first message"); $Mail->AddTo("ahmed@cha-international.com", "Ahmed"); $Mail->AddCc("me2resh@mac.com", "Me2resh"); $Mail->AddBcc("ahmed@menatravelawards.com", "hola");Your calling this
$Mail->AddAttach("favicon.ico"); Before this... ( see below )
$Mail->SendMail(); echo "<pre>"; print_r($Mail); /** * */ class MainClass{Because, right here, you are adding a blank line to the email body. This tells the system that the rest is the message. Take off the leading \n and move it to the end of the statement and it might work for you./** * */var $To = ""; var $Cc = ""; var $Bcc = ""; var $From = ""; var $Name = ""; var $Subject = ""; var $Message = ""; var $Headers = "";function Construct($name, $from, $subject, $message){ //construct message if ($name == ""){ $name = $from; } $this->Name = $name; $this->From = $from; $this->Subject = $subject; $this->Message = $message; }//end of Constructfunction AddTo($email, $name){ // add reciepnt to To array if ($name == ""){ $name = $email; } if ($this->To == ""){ $this->To .= $name.' <'.$email.'>'; }else{ $this->To .= ",".$name.' <'.$email.'>'; } }// end of AddTofunction AddCc($email, $name){ // add reciepnt to Cc array if ($name == ""){ $name = $email; } if ($this->Cc == ""){ $this->Cc .= $name.' <'.$email.'>'; }else{ $this->Cc .= ",".$name.' <'.$email.'>'; } }function AddBcc($email, $name){ // add reciepnt to Bcc array if ($name == ""){ $name = $email; } if ($this->Bcc == ""){ $this->Bcc .= $name.' <'.$email.'>'; }else{ $this->Bcc .= ",".$name.' <'.$email.'>'; } }function AddAttach($attach){ //add attach to array //if (is_uploaded_file($attach)) { // Read the file to be attached ('rb' = read binary) $file = fopen($attach,'rb'); $data = fread($file,filesize($attach)); fclose($file);// Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
Are these really suppose to be \r\n??? Or should you use the PHP_EOL constant?// Add the headers for a file attachment $this->Headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";// Add a multipart boundary above the plain message $this->Message = "This is a multi-part message in MIMEformat.\n\n" ."--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n"."Content-Transfer-Encoding: 7bit\n\n" . $this->Message . "\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $this->Message .= "--{$mime_boundary}\n" . "Content-Type: {filetype($attach)};\n" . " name=\"{$attach}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$attach}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; // }// end of if}// end of add attachfunction SendMail(){
$this->Headers .= "\r\n";
$this->Headers .= 'To: '.$this->To."\r\n";
$this->Headers .= 'From: '.$this->Name.'<'.$this->From.'>'."\r\n";
$this->Headers .= 'Cc: '.$this->Cc."\r\n";
$this->Headers .= 'Bcc: '.$this->Bcc."\r\n";
mail($this->From, $this->Subject, $this->Message, $this->Headers);
}
}// end of class
?>
Here is the text in the email :
----------------------------------------------------------------
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_xd609c629ba34ecb645a7572abb61eb27x"
To: Ahmed <ahmed@cha-international.com>
From: Ahmed<me2resh.lists@gmail.com>
Cc: Me2resh <me2resh@mac.com>
Bcc: hola <ahmed@menatravelawards.com>
This is a multi-part message in MIME format.
--==Multipart_Boundary_xd609c629ba34ecb645a7572abb61eb27x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello, this is my first message
--==Multipart_Boundary_xd609c629ba34ecb645a7572abb61eb27x
Content-Type: {filetype(favicon.ico)};
name="favicon.ico"
Content-Disposition: attachment;
filename="favicon.ico"
Content-Transfer-Encoding: base64
AAABAAEAEBAAAAEAGABoAwAAFgAAACgAAAAQAAAAIAAAAAEAGAAAAAAAAAMAAAAAAAAAAAAAAAAA
AAAAAAASEhIHBwcHBwcJCQkoKChLS0tZWVlOTk4MDAwHBwcHBwcHBwcHBwcHBwcHBwcmJiYKCgok
JCSPj4/h4eH+/v7+/v7+/v7+/v4pKSkAAAAAAAAAAAAAAAAAAAAAAAAgICB4eHj5+fn+/v7/////
///////+/v7+/v52dnYAAAAAAAAAAAAAAAAAAAAAAAAgICBGRkb9/f3////////+/v709PTPz8/B
wcG0tLTQ0NDf39+BgYEAAAAAAAAAAAAgICAKCgq2trb+/v7+/v64uLi9vb39/f3+/v7+/v7+/v7+
/v739/cpKSkAAAAAAAAgICAKCgpRUVH+/v7e3t6VlZX+/v7+/v7////////09PR/f39/f3+RkZEA
AAAAAAAgICAAAAARERH7+/uurq7n5+f+/v7////////////+/v7+/v61tbXZ2dk8PDwAAAAgICAA
AAAAAACbm5uFhYX19fX+/v7+/v7U1NS1tbWysrLHx8fBwcGpqal0dHQeHh4iIiIAAAADAwPr6+vU
1NRKSkqMjIzs7OyYmJj9/f3+/v7X19csLCxra2vg4ODKyspQUFAAAAACAgLn5+f8/PwPDw83Nze9
vb2wsLC6urq5ubnFxcXu7u7+/v77+/tYWFggICAAAAAAAAA0NDRhYWEEBAQEBATg4OD+/v7+/v7+
/v7+/v7+/v7+/v7+/v6Dg4MgICAAAAAAAAAAAAAAAAAAAAAAAACfn5/////////+/v729vaurq5z
c3N2dnZPT08gICAGBgYAAAAAAAAAAAAAAAAAAAA1NTW8vLypqalra2sWFhYAAAAAAAAAAAAAAAAg
ICANDQ0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgICAZGRkAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgICA7OzsXFxcXFxcXFxcXFxcX
FxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxcXFxc1NTUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
--==Multipart_Boundary_xd609c629ba34ecb645a7572abb61eb27x--
please help, any help would be appreciated.
Regards,
Me2resh