5 Replies - 10365 Views - Last Post: 08 October 2007 - 08:25 AM Rate Topic: -----

#1 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

PHP mail from another server

Post icon  Posted 10 September 2007 - 07:44 PM

If this is posted in the wrong section, please forgive me. What I'm trying to do is sendmail from a webserver via an e-mail server that is running on the local network. From the Qmail logs, I can see that the log is successful, but the send log doesn't show anything going out, nor do I see any messages queue up.

Maybe someone here is familiar with using Qmail remotely?

One more thing, I can use both pop3(via outlook & thunderbird) & PHP SquirrelMail, so I know that the account works, both sending & receiving mail.

<?php

/*
  qmail setup
*/


function authgMail($from, $namefrom, $to, $nameto, $subject, $message) {

/*  your configuration here  */

$smtpServer = "192.168.xxx.xxx"; //ip accepted as well
$port = "25"; // should be 25 by default
$timeout = "45"; //typical timeout. try 45 for slow servers
$username = "sales@mydomain.com"; //the login for your smtp
$password = "myPA$$"; //the pass for your smtp
$localhost = "127.0.0.1";
$newLine = "\r\n"; //var just for nelines in MS
$secure = 0; //change to 1 if you need a secure connect

/*  you shouldn't need to mod anything else */

//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect)) {
   $output = "Failed to connect: $smtpResponse";
   echo $output;
   return $output;
}
else {
   $logArray['connection'] = "<p>Connected to: $smtpResponse";
   echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
}

//you have to say HELO again after TLS is started
   fputs($smtpConnect, "HELO $localhost". $newLine);
   $smtpResponse = fgets($smtpConnect, 4096);
   $logArray['heloresponse2'] = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";

//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";

//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";

//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";

//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";

//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$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;

//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";

// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}

$err=0;
$err_msg="This function is not yet implimented.";

if($_POST['name_']!="") { echo $_POST['name_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include your name";
}

if($_POST['day_phone_']!="") {echo $_POST['day_phone_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include a daytime phone number.";
}
if($_POST['add_']!="") { echo $_POST['add_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include your address.";
}
if($_POST['city_']!="") { echo $_POST['city_']."<br>"; }
else {
  $err=1;
  $err_msg="You must include the city the vehicle is located in.";
}
if($_POST['email']!="") { echo $_POST['email']."<br>"; }
else {
  $err=1;
  $err_msg="You must include the city the vehicle is located in.";
}
if($_POST['email']!="") { echo $_POST['email']."<br>"; }
else {
  $err=1;
  $err_msg="You must include your e-mail address.";
}

echo $err_msg;

if($err<=0) {
  $from="sales@mydomain.com";
  $namefrom="Akron Sales";
  $to = "no2pencil@mydomain.com";
  $nameto = "no2pencil";
  $message = "Test from My Domain";
  authgMail($from, $namefrom, $to, $nameto, $subject, $message);
}
else {
  echo "<p /> This form was not filled out correctly, please correct any mistakes.";
}

?>



Is This A Good Question/Topic? 0
  • +

Replies To: PHP mail from another server

#2 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 19-June 07

Re: PHP mail from another server

Posted 11 September 2007 - 11:11 AM

You can do that. Or you can just use mail() function?
EDIT: O sorry never saw the Qmail thing. But if the logs show that it received your request. Then most likely it is nothing to do with the PHP. Read the responses maybe it will give you some extra information.

This post has been edited by ap0c0lyps3: 11 September 2007 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: PHP mail from another server

Posted 11 September 2007 - 11:14 AM

View Postap0c0lyps3, on 11 Sep, 2007 - 11:11 AM, said:

You can do that. Or you can just use mail() function?

I wouldn't think that I can just use the mail function, since the e-mail server is remote. I have port 25 blocked on my web server. These are 2 different machines, but they both exist on my network.
Was This Post Helpful? 0
  • +
  • -

#4 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 19-June 07

Re: PHP mail from another server

Posted 12 September 2007 - 08:46 AM

The mail function sends the message on behalf of the server. So YOUR server sends the message unless set otherwise in the php.ini. Using the mail() function would use a SMTP server to send the message to whoever it is supposed to go to
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: PHP mail from another server

Posted 12 September 2007 - 09:51 AM

Again, I'm not using mail() due to the fact I have port 25 blocked on this server. I do however, have an entire server dedicated to email (it's an email server running qmail) & I am using php to remote connect to that server.

One of the errors that I have found is blank line feed code in qmail.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: PHP mail from another server

Posted 08 October 2007 - 08:25 AM

View Postap0c0lyps3, on 11 Sep, 2007 - 11:11 AM, said:

You can do that. Or you can just use mail() function?
EDIT: O sorry never saw the Qmail thing. But if the logs show that it received your request. Then most likely it is nothing to do with the PHP. Read the responses maybe it will give you some extra information.

This did turn out to be the issue. The webserver was not on qmails list of trusted domains. Added, updated, restarted qmail, worked as designed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1