Jump to content

Recommended Posts

Having given up on mail() I'm trying to work out PEAR mail sending through SMTP with outgoing authentication.  Unfortunately I have no experiance with using PEAR for anything, and the error messages are far from helpfull to me.  Here's what I get after what I assume to be a timeout:

starting build of mail message...
Message body built ok...
Attachment built ok...
Headers built ok...
SMTP Paramaters built ok...
Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 416

Strict Standards: Non-static method PEAR::raiseError() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 465

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 1024

Strict Standards: Non-static method Mail_mimePart::encodeHeader() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 1322

Strict Standards: Non-static method Mail_mimePart::encodeHeader() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 1322

Strict Standards: Non-static method Mail_mimePart::encodeHeader() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 1322

Strict Standards: Non-static method Mail_mimePart::encodeHeader() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 1322

Strict Standards: Non-static method Mail_mimePart::_explodeQuotedString() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mimePart.php on line 812

Strict Standards: Non-static method Mail_mimePart::encodeHeader() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 1322

Strict Standards: Non-static method Mail_mimePart::_explodeQuotedString() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mimePart.php on line 812

Strict Standards: Non-static method Mail_mimePart::encodeHeader() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mime.php on line 1322

Strict Standards: Non-static method Mail_mimePart::_explodeQuotedString() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\mimePart.php on line 812

Strict Standards: Non-static method Mail::factory() should not be called statically in C:\xampp\htdocs\fos\index3.php on line 13

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Mail\smtp.php on line 365

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Net\SMTP.php on line 450

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in C:\xampp\php\PEAR\Net\SMTP.php on line 467

 

and this is the test page I am trying to send from:

<?php
require_once 'Mail.php';
require_once 'mail/mime.php';

function sendMime($body, $attach, $head, $params ){
$eol = '\n';
$mime = new Mail_mime(array('eol' => $eol));
$mime -> setHTMLBody ($body);
$att = implode(',', $attach);
$mime -> addAttachment ($att);
$body = $mime -> get();
$headers = $mime->headers($head);
$mail =& Mail::factory('SMTP', $params);
$mail ->send('muddyfunster.webdev@gmail.com', $headers, $body);

return $mail;
}

function buildHeaders(){
$pHead = array();
$pHead['To'] = 'muddyfunster.webdev@gmail.com';
$pHead['From'] = 'validEmailAddress@domain.ext';
$pHead['Reply-To'] = 'ReplyAddress@domain.ext';

return $pHead;
}

function buildParams(){
$para = array();
$para['Host'] = 'mail.domain.ext';
$para['port'] = '25';
$para['auth'] = 'true';
$para['username'] = 'ValidMailAddress@domain.ext';
$para['password'] = 'ValidPassword';

return $para;
}


function buildBody(){

$mailBody = "<html><body><br><center><h3>SAMPLE SHEET</h3></center><br>";
$mailBody .="<table border='1'><tr><th>Service Provider Name :</th><td>Test Sender</td><th>Job Number:</th><td>1</td></tr>";
$mailBody .="<tr><th>Contact Name :</th><td>Muddy_Funster</td><th>Customer :</th><td>Sample Company</td></tr>";
$mailBody .="<tr><th>Contact No :</th><td>11111111111</td><th>ID :</th><td>ABCDE</td></tr>";
$mailBody .= "<tr><th>Query Date :</th><td>Today</td><th>Job Date :</th><td>Today</td></tr>";
$mailBody .= "<tr><td colspan='4'>QUERY TYPE :</td></tr>";
$mailBody .= "<tr><th>QUERY INFO :</th><td colspan='3'>Other:Test</td></tr>";
$mailBody .= "<tr><td colspan='4'>DETAILS OF THE ISSUE :</td></tr>";
$mailBody .= "<tr><td colspan='4'>Test Data</td></tr>";
$mailBody .= "</table><p>Please do not reply to this email unless there is a technical issue.</p></body></html>"."\r\n"."\r\n" ;

return $mailBody;
}                                                                                               

function buildXML(){
$xmlBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Document title=\"Sample SHEET\"><InternalID>99999</InternalID><JobDate>2012-01-16</JobDate>";
$xmlBody .= "<QueryDate>2012-01-16 13:21:24</QueryDate><ServiceProvider name=\"Test Sender\">";
$xmlBody .= "<Name>Muddy_Funster</Name><TelNo>1111111111</TelNo><FaxNo>22222222</FaxNo></ServiceProvider>";
$xmlBody .= "<Customer name=\"Sample Company\"><ID>ABCDE</ID></Customer>";
$xmlBody .= "<Query ID=\"1\"><Type>Other:Test</Type><Details>Testing the Mail System</Details></Query></Document>";

$at = array(); // build attachment
$at['file'] = $xmlBody;             //content
$at['c_type'] = 'text/xml';           //Content-Type:
$at['name'] = '300000001.xml';      //Suggested File Name
$at['isfile'] = false;                //is attachment a file?
$at['encoding'] = 'base64';             //encoding type
$at['disposition'] = 'attachment';         //Content-Disposition (choose either attachment or inline)

return $at;
}
echo "starting build of mail message...";
$body = buildBody();
echo "<br>Message body built ok...";
$attachment = buildXML();
echo "<br>Attachment built ok...";
$headers = buildHeaders();
echo "<br>Headers built ok...";
$params = buildParams();
echo "<br>SMTP Paramaters built ok...";

$newMailMessage = sendMime($body, $attachment, $headers, $params);

print_r($newMailMessage);

?>

 

I would really appreciate if someone could point out where this is going wrong.  I have run the PEAR upgrade-all as there was previously depreciated messages showing and it's now up to date (nothing to upgrade message) and those messages have gone (and at this rate my mind is soon to follow  :-\).

Link to comment
https://forums.phpfreaks.com/topic/255144-pear-mail-problems/
Share on other sites

mail() doesn't support SMTP authentication on win32 servers (tried using ini_set and editing the php.ini file to no avail, constant error 530 returned from server).  as well as that I just couldnt get it to send both parts of a multipart MIME message (1 attachment and 1 HTMLbody) in the same message.  I could send the attachment through fine (to an internal address) and I could send the body through IF I left the attachment off (again to internal addresses).  I'm not too fussed about getting the external addresses working, but this whole "mail with attachment" idea is starting to annoy the hell out of me.

Link to comment
https://forums.phpfreaks.com/topic/255144-pear-mail-problems/#findComment-1308217
Share on other sites

Think i've got a work arround using the PEAR Net_SMTP Class.  Having played about with it: it's looking like a pretty well rounded class (function wise) although it is rather undocumented in it's usage.  Anyone any experience of this? Is there a season it's practicly unused?

Link to comment
https://forums.phpfreaks.com/topic/255144-pear-mail-problems/#findComment-1308488
Share on other sites

Damn! Is there any way to specify an EOF equivelent termination at the end of a sring variable?  The attachment doesn't close off, it's as though it doesn't know where to terminate the file contents and where to start looking for header information again.  I am assuming this is delt with in file format by recognising the EOF, so can I hard code an EOF into a string variable?

Link to comment
https://forums.phpfreaks.com/topic/255144-pear-mail-problems/#findComment-1308540
Share on other sites

OK, all sorted. Here's the functional code, commented and generified (is that even a word...? :shrug:) I did want to put this in the snippits section as there is very little on this class that is easily accessable.  anyway, hope this helps save someone the three days it wound up taking me.

<?php
require_once 'Net/SMTP.php'; //stick this line somewhere at the top of your page


/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
I wrote this function to use the PEAR Net_SMTP class as I needed something that 
would pass SMTP authentication information, and didn't want to install any new
librarys.  The most important parts of this were getting the right line terminator 
and then getting the right number of them for each line!  
Feel free to tweek this as much as you like, just don't change any of the line 
ends or your fixing it on your own 
--This is NOT configured to work with SSL or TSL (so no sending through gmail)
Any questions you can get in touch at: 
muddyfunster.webdev@gmail.com
*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/
function sendSMTP($subject, $body, $user='defaultUserName', $pass='defaultPass', $to='Friendly Name', $destination='default@recipient.ext', $from='Friendly Name', $sender='default@address.ext', $fName=null, $attachment=null){
//set defaults for user name, password, to, from, sender and recipient here ^^^

$e = "\n"; //end of line
$bound= "==MUDDY_FUNSTER_".md5(time()); //create a unique boundary for this mail to separate each different type in the message. 

@$smtp = new Net_SMTP("your.mailserver.ext", 25); // set to youe mail providers A record and port number (IP Addresses will also work for server)
@$smtp->connect(60); //create connection and set timeout duration
@$smtp->helo('your_mail_domain.ext'); //start comunication with server --change to your mail providers domain   
@$smtp->auth($user, $pass);  //authenticate account
@$smtp->mailFrom("$sender");  //email address of authenticated account, can set default in function declaration
@$smtp->rcptTo("$destination");   //recipient of mail, can set default in function declaration

$header = "";  //set header as empty 
$header .= "From: $to <$sender>".$e; // Set Friendly Name and address of sender
$header .= "To: $from <$destination>".$e; // Set Friendly name and address of recipient
$header .= "Subject: $subject".$e;  //Set mail Subject
$header .= "Reply-To: $sender".$e;  //Set Reply-to address
$header .= 'MIME-Version:1.0'.$e;   //use MIME transport
$header .= "Content-type:multipart/mixed;"; // Define multipart mail contnent
$header .= "boundary=\"".$bound."\"".$e.$e; //set unique boundary for each different type of content

$message = $header; //include headers into message information
$message .= "--".$bound.$e; //open boundary
$message .= "Content-type:text/html;";  //set contnent as html for mail body
$message .= "charset=\"iso-8859-1\";".$e; //set charactor set to be used in mail body - can be changed to suit
$message .= "Content-disposition:inline;"; //use inline to inferr displaying of contnet in the body without need for end user input
$message .= "Content-transfer-encoding:quoted-printable".$e.$e.$e; //set transfer encoding for html text content
$message .= $body.$e.$e; //add the contnent of $body which is passed into the function 
if($attachment != null && $fName != null){ //check if there is an attchment being passed into the function and that it has a name
$message .= "--".$bound.$e; //add boundary to change to next content type
$message .= "Content-type:application/octet-stream;"; //set content type to suitable type for attachment (octet-stream is for generic binary data other are available)
$message .= "name=\"$fName.xml\"".$e; //set the file name
$message .= "Content-disposition:attachment;"; //use attachment to infer data that is being passed as a file
$message .= "filename=\"$fName.xml\";".$e; //set the filename as it will be recieved
$message .= "Content-transfer-encoding:base64".$e.$e; // encode the file for transfer
$message .= $attachment.$e.$e; // add attachment contents
}
$message .= "--$bound--".$e; //closing boundary shows it's the last part of the MIME and the mail is now complete

@$smtp->data($message); //send message to the server
$msg = $smtp->getResponse(); // get server response array format -- $msg = Array([0]=>'server numeric code' [1]=>'Text message from server for code') -- hMailServer success response code is 250.  This can be called after any of the $smtp-> calls for error capture 
@$smtp->disconnect(); // close server connection
return $msg; //pass out server response array
}                       

//call function using $var = sendSMTP($subject, $body, $user, $pass, $to, $recipientAddress, $from, $senderAddress, $filenameOfAttachment, $attachmentContent); 
//$to and $from are the "Friendly Name" values for the message e.g. John Doe
//if you are passing in attachment content you MUST pass in a file name (prefferably with extension, but thats your targets problem) for it as well.
?>

Link to comment
https://forums.phpfreaks.com/topic/255144-pear-mail-problems/#findComment-1308701
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.