Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

I can't send E-mail in OScommerce


Guest

Recommended Posts

To setting the SMTP in php.ini

 

I can't just only input the SMTP address,

it must be input the Authenticate Account of SMTP server.

 

But what code do I input?!

 

Thanks~

Link to comment
Share on other sites

  • Replies 64
  • Created
  • Last Reply

Hi,

 

I had similar problems and am going to replace all the osCommerce email code (tep_mail() function plus a few others ??) with code using the mail::factory method.

 

<?php

include('Mail.php');

$recipients = '[email protected]';

$headers['From'] = '[email protected]';
$headers['To'] = '[email protected]';
$headers['Subject'] = 'Test message 1';

$body = 'Test message 2 with the body.';

$params['host'] = 'localhost';

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory('smtp', $params);

$mail_object->send($recipients, $headers, $body);
print "Sent Mail message to $recipients";
?>

 

Peter

Link to comment
Share on other sites

kind of new here - I have to use SMTP on my host also. Can you tell me where this snippet goes - and what it replaces?

 

This looks like a solution to the problem I'm having on a few different apps concerning emails - - thank you for posting it! :D

Link to comment
Share on other sites

Hi Mike,

 

Yes, my reason for needing to use the other email method was because, after many weeks of trying so many different things with emailing, i could not change the 'Return-Path:' of any emails. I wanted to, because it 'gave away' my root/shell login username (gulp).

 

Using SMTP, and defining a hostname (I don't even need to authenticate, hey, it's email going out anyway) does the trick.

 

Now you asked where it goes .....................

 

1. It would appear at first, that the function tep_mail() in osC is the only email function, but it is not, but it is the email function for _most_ of the osC 'emailing'. I'm still trying to find the time to dig deeper into osC code to see where else emails are sent. :(

 

2. tep_mail() eventually simply calls the PHP mail() function, so using "PHPXref", the mail() function, is referenced 5 times:

 

Referenced 5 times:

 

* /includes/classes/email.php -> line 500

* /includes/classes/email.php -> line 502

* /admin/includes/classes/email.php -> line 500

* /admin/includes/classes/email.php -> line 502

* /includes/modules/shipping/usps.php -> line 53

 

Let's take a look at .../email.php

 

499        if (EMAIL_TRANSPORT == 'smtp') {
500          return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
501        } else {
502          return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
503        }

 

this is part of the send() function. I think the 'trick' to finding out all the places where osC does emails is to actually search for strings like "smtp" , because that is how it is setup in the admin function.

 

As you can see by the above code , whether 'smtp' or 'sendmail', the PHP mail() function is still called, ...... no big deal really.

 

To finally answer 'where this snippet' goes, simply replace lines 500 and 502, or you may want to "force" SMTP and comment out lines 499,501-503, which removes the "IF" and forces line 500.

 

2. Now, to 'what it replaces', I had some notes about how I was going to do it, ... hang on ....

 

Just some PHP comments as follows:

 

// Note: the mail_object method is very similar to the PHP mail() function

// return $mail_object->send($recipients, $headers, $body);
// return mail($recipients, $subject, $body, "From: $user_contact");

 

As you can see, they are very similar, possibly just a few additional lines of code to extract the 'headers', and then you are right to use mail::factory.

 

Hope that helps,

 

Peter

Link to comment
Share on other sites

Hi Mike,

 

Just thinking a bit more about how this is to be done, because I need to get around to doing myself soon. :D

 

I would actually comment out ALL of the following lines in /catalog/includes/classes/email.php

 

499        if (EMAIL_TRANSPORT == 'smtp') {
500          return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
501        } else {
502          return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
503        }

 

then after line 498 add the following .........

 

<?
include("Mail.php");

$headers["From"]    = $user_contact;
$headers["To"]      = $to;
$headers["Subject"] = $subject;

$params["host"] = "localhost.yoursite.com";
$params["port"] = "25";
//$params["auth"] = true;
//$params["username"] = "user";
//$params["password"] = "password";

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $params);
return $mail_object->send($to_addr, $headers, $this->output);
?>

 

Obviously, put in authorisation, username and password if needed, I commented them out so you can see where they go.

 

The code is untested, just an uneducated guess, really, but why not give it a try, put a few echos before you do the final 'send', so make sure the variables and arrays are setup correctly.

 

1. I don't know as yet, how to adjust the code, to handle "extra headers", which I think can be parsed, see the array $xtra_headers. Although it would seem from line 553

 

$headers = array_merge($this->headers, $xtra_headers);

 

that the extra headers are already in the array $headers ??

 

2. In looking at how SMTP is done now with the PHP mail() function

 

return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));

 

notice there are also a lot of "$this->lf", which are line feeds, if you specify them in the 'admin' of osCommerce. I do not know at this stage if any PHP 'implodes' need to be done to format the email, before the final send. No dount a simple 'echo' of the $headers array will tell us if extra code is needed or not. It's not rocket science, see lines 37 to 41

 

    if (EMAIL_LINEFEED == 'CRLF') {
       $this->lf = "\r\n";
     } else {
       $this->lf = "\n";
     }

 

Hope that gets you going with using the Mail::factory method. :)

 

Peter

Link to comment
Share on other sites

Hi Toni,

 

But I still don't know how to setup!

 

I would actually comment out ALL of the following lines in /catalog/includes/classes/email.php

 

499        if (EMAIL_TRANSPORT == 'smtp') {
500          return mail($to_addr, $subject, $this->output, 'From: ' . $from . $this->lf . 'To: ' . $to . $this->lf . implode($this->lf, $this->headers) . $this->lf . implode($this->lf, $xtra_headers));
501        } else {
502          return mail($to, $subject, $this->output, 'From: '.$from.$this->lf.implode($this->lf, $this->headers).$this->lf.implode($this->lf, $xtra_headers));
503        }

 

then after line 498 add the following .........

 

<?
include("Mail.php");

$headers["From"]    = $user_contact;
$headers["To"]      = $to;
$headers["Subject"] = $subject;

$params["host"] = "localhost.yoursite.com";
$params["port"] = "25";
//$params["auth"] = true;
//$params["username"] = "user";
//$params["password"] = "password";

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $params);
return $mail_object->send($to_addr, $headers, $this->output);
?>

 

Obviously, put in authorisation, username and password if needed, I commented them out so you can see where they go.

 

It's untested modifications, just an educated guess. :D

 

Peter

Link to comment
Share on other sites

Hi Peterr

 

Is it still need <? and ?> ???

 

<?

include("Mail.php");

 

$headers["From"]    = $user_contact;

$headers["To"]      = $to;

$headers["Subject"] = $subject;

 

$params["host"] = "localhost.yoursite.com";

$params["port"] = "25";

//$params["auth"] = true;

//$params["username"] = "user";

//$params["password"] = "password";

 

// Create the mail object using the Mail::factory method

$mail_object =& Mail::factory("smtp", $params);

return $mail_object->send($to_addr, $headers, $this->output);

?>

Link to comment
Share on other sites

Hi Peter

 

I want to know what is the "host" for ?!

type what is correct?!

 

$params["host"] = "localhost.yoursite.com";

 

 

Thanks your reply~

Link to comment
Share on other sites

I was tried to change the code.

But it hasn't any effect ~

 

Why?!

 

And I tried to delete all the email.php content,

but it still can show me it send the email.

 

WHY?!

Link to comment
Share on other sites

Oh..sorry, I edit the wrong file~ ~.~

 

BUt I edit the email.php file and I sent the email, it shows:

 

Fatal error: Cannot redeclare big5_ishb() (previously declared in c:\appserv\www\catalog\includes\languages\tchinese\big5_func\big5_func.inc:69) in c:\appserv\www\catalog\includes\languages\tchinese\big5_func\big5_func.inc on

 

WHY?

 

Thx~

Link to comment
Share on other sites

Hi,

 

I want to know what is the "host" for ?!

type what is correct?!

 

$params["host"] = "localhost.yoursite.com";

 

Yes, what you have above is the correct format, replace 'yoursite.com' with your domain name obviously.

 

Peter

Link to comment
Share on other sites

Toni,

 

I was tried to change the code.

But it hasn't any effect ~

 

Why?!

 

And I tried to delete all the email.php content,

but it still can show me it send the email.

 

WHY?!

 

Why don't you test the Mail::factory method first, in isolation, not using osCommerce, just use this:

 

<?
include("Mail.php");

$headers["From"]    = $user_contact;
$headers["To"]      = $to;
$headers["Subject"] = $subject;

$params["host"] = "localhost.yoursite.com";
$params["port"] = "25";
//$params["auth"] = true;
//$params["username"] = "user";
//$params["password"] = "password";

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $params);
return $mail_object->send($to_addr, $headers, $this->output);
?>

 

Peter

Link to comment
Share on other sites

Hi Peter,

 

 

Why don't you test the Mail::factory method first, in isolation, not using osCommerce, just use this:

 

 

 

I don't know how to test the Mail::factory?

 

 

thx

 

Toni

Link to comment
Share on other sites

Toni,

 

I don't know how to test the Mail::factory?

 

Create the following PHP file, and save it as mailfactory_test.php

 

<?
include("Mail.php");

$user_contact = '[email protected]';
$to_addr = '[email protected]';
$subject = 'Mail factory test';
$email_body 'This is just a test to see if the mail::factory method works';

$headers["From"]    = $user_contact;
$headers["To"]      = $to_addr;
$headers["Subject"] = $subject;

$params["host"] = "localhost.yoursite.com";
$params["port"] = "25";
$params["auth"] = true;
$params["username"] = "user";
$params["password"] = "password";

// Create the mail object using the Mail::factory method
$mail_object =& Mail::factory("smtp", $params);
return $mail_object->send($to_addr, $headers, $email_body);

//return $mail_object->send($to_addr, $headers, $this->output);
?>

 

The run mailfactory_test.php from your website.

 

You will need to define the host, username, password, and have valid email 'from' and 'to' addresses. :D

 

Peter

Link to comment
Share on other sites

Hi Peterr

 

You will need to define the host, username, password, and have valid email 'from' and 'to' addresses

 

Is the host is means SMTP server?!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...