|
|
|||||||
| Linux Web Hosting Discussion Forum and Support Discussion forum on Linux Web Hosting issues related to cpanel, whm, ensim, plesk, emails, ftp, frontpage, php, perl, cgi or permission. |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
Hello Everyone,
We have disabled php mail() function on the server due to several reasons. To begin with, it is a major cause of spamming. Next, since it uses the 'nobody' user to send mails, it creates problems of email spoofing. Thus, scripts utilizing the 'nobody' user to not be able to send mails now. The best alternative to this is to use SMTP authentication. If you are one of those users who need this code then kindly find it as below: Code:
<?php
//new function
$to = "post@example.com";
$nameto = "Who To";
$from = "post@example.com";
$namefrom = "Who From";
$subject = "Hello World Again!";
$message = "World, Hello!";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
?>
<?php
/* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */
//Authenticate Send - 21st March 2005
//This will send an email using auth smtp and output a log array
//logArray - connection,
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
Â* Â*//SMTP + SERVER DETAILS
Â* Â*/* * * * CONFIGURATION START * * * */
Â* Â*$smtpServer = "mail.server.com";
Â* Â*$port = "25";
Â* Â*$timeout = "30";
Â* Â*$username = "smtpusername";
Â* Â*$password = "smtppassword";
Â* Â*$localhost = "localhost";
Â* Â*$newLine = "\r\n";
Â* Â*/* * * * CONFIGURATION END * * * * */
Â* Â*
Â* Â*//Connect to the host on the specified port
Â* Â*$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*if(empty($smtpConnect))
Â* Â*{
Â* Â* Â* Â*$output = "Failed to connect: $smtpResponse";
Â* Â* Â* Â*return $output;
Â* Â*}
Â* Â*else
Â* Â*{
Â* Â* Â* Â*$logArray['connection'] = "Connected: $smtpResponse";
Â* Â*}
Â* Â*//Request Auth Login
Â* Â*fputs($smtpConnect,"AUTH LOGIN" . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['authrequest'] = "$smtpResponse";
Â* Â*
Â* Â*//Send username
Â* Â*fputs($smtpConnect, base64_encode($username) . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['authusername'] = "$smtpResponse";
Â* Â*
Â* Â*//Send password
Â* Â*fputs($smtpConnect, base64_encode($password) . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['authpassword'] = "$smtpResponse";
Â* Â*//Say Hello to SMTP
Â* Â*fputs($smtpConnect, "HELO $localhost" . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['heloresponse'] = "$smtpResponse";
Â* Â*
Â* Â*//Email From
Â* Â*fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['mailfromresponse'] = "$smtpResponse";
Â* Â* Â* Â*
Â* Â*//Email To
Â* Â*fputs($smtpConnect, "RCPT TO: $to" . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['mailtoresponse'] = "$smtpResponse";
Â* Â*
Â* Â*//The Email
Â* Â*fputs($smtpConnect, "DATA" . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['data1response'] = "$smtpResponse";
Â* Â*
Â* Â*//Construct Headers
Â* Â*$headers Â*= "MIME-Version: 1.0" . $newLine;
Â* Â*$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
Â* Â*$headers .= "To: $nameto <$to>" . $newLine;
Â* Â*$headers .= "From: $namefrom <$from>" . $newLine;
Â* Â*
Â* Â*fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['data2response'] = "$smtpResponse";
Â* Â*
Â* Â*// Say Bye to SMTP
Â* Â*fputs($smtpConnect,"QUIT" . $newLine);
Â* Â*$smtpResponse = fgets($smtpConnect, 515);
Â* Â*$logArray['quitresponse'] = "$smtpResponse"; Â* Â*
}
?>
__________________
Vaughn Team Leader // Tech. Support AccuWebHosting.Com email:support@accuwebhosting.com Blog: blogsandbloggers.com/Vaughn http://www.accuwebhosting.biz/Vaughn...aughn_Sign.jpg |
|
|||
|
You were getting this error as you had an error on line 9, you were missing the semi-colon at the end. I've added it and the error is now fixed, please check.
Thanks,
__________________
Vaughn Team Leader // Tech. Support AccuWebHosting.Com email:support@accuwebhosting.com Blog: blogsandbloggers.com/Vaughn http://www.accuwebhosting.biz/Vaughn...aughn_Sign.jpg |
|
|||
|
I found a lot of errors with that authenticated SMTP send function.
For one, it doesn't seem to handle multi-line SMTP server responses, and the mail server certainly seems to be giving them. So I wrote a small function to handle those: Code:
function getSmtpResponse($smtpConnection)
{
Â*Â*Â*Â*$smtpResponse = "";
Â*Â*Â*Â*$stmpLine = "";
Â*Â*Â*Â*$getNextLine = true;
Â*Â*Â*Â*while ( $getNextLine )
Â*Â*Â*Â*{
Â*Â*Â*Â*Â*Â*Â*Â*$smtpLine = fgets($smtpConnection);
Â*Â*Â*Â*Â*Â*Â*Â*// If the line has a dash after the response number,
Â*Â*Â*Â*Â*Â*Â*Â*//Â*Â* it's a multiline response
Â*Â*Â*Â*Â*Â*Â*Â*$getNextLine = strpos( $smtpLine, "-" ) == 3;
Â*Â*Â*Â*Â*Â*Â*Â*$smtpResponse .= $smtpLine . "\r\n";
Â*Â*Â*Â*}
Â*Â*Â*Â*return $smtpResponse;
}
Code:
$smtpResponse = fgets($smtpConnect, 515); Code:
$smtpResponse = getSmtpResponse($smtpConnect); How was I finding all this out? I had it split out the contents of the $logArray (in b2evolution, using the debug_die command and a short routine to split apart the array) I haven't spent the time to figure out how to make it figure out sending errors, but I figure it's probably best to replace this function with something more permanent and less hacky in future. Hope that helps ![]() -- Ritchie |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Compile php | Manuel_ | Linux Reseller Accounts Pre-Sales Questions | 0 | 07-10-2006 01:53 AM |
| How to :: add php extension in windows server | Manuel_ | Windows Dedicated Servers | 0 | 06-23-2006 07:53 AM |
| PHP Mail | Vaughn | Windows Web Hosting Discussion Forum | 1 | 03-27-2006 12:54 AM |
| Sendmail form for php | donjory | Windows Web Hosting Discussion Forum | 1 | 02-05-2006 01:48 AM |
| PHP email facility on site not working anymore | Adriaan | Linux Web Hosting Discussion Forum and Support | 2 | 08-30-2005 02:02 AM |