Click to See Complete Forum and Search --> : [Resolved] Setting up SMTP server and mail()


camowel
06-28-2005, 03:28 PM
Hey, I'm new to this site, seems great

I've been on a few other forums for a while, getting help for projects and helping some people etc, but I've never seen a clear and useful post about setting up an SMTP server and getting the mail function to work properly.

Some info about my system:
Windows XP Home (SP2)
Apache 2.0.XX
PHP 5
MySQL 4.1
ActivePerl 5.8.7 (If anyone knows how to set this up with apache, please send me a pm, thanks)

I'd really like if someone could post a few links or someinfo on how to set up php's mail stuff

From what responses I've gotten in the past, I think I need an SMTP server installed on my computer, when I googled, the only results that I seemed to get were servers I would have to pay for. I'd also like to know about configuring PHP's mail functions (php.ini options) so that it will work corectly

Thanks for any replies

I'd just like to add that, if your reply is critisizing me for asking, or saying 'google it', it is not welcome, I'd like useful responses, or none at all, Thank you.

bradgrafelman
06-28-2005, 05:15 PM
Windows XP Home? This would indicate, to me, that you are a simple user at home attempting to run a website, using a simple home computer, connected to a simple residential ISP.

Other than the various warnings I would voice concerning the EULA, TOS, AUP, etc. your ISP might hold that strictly prohibits any webserver or SMTP server (as most ISP's do, especially broadband), I would ask you one thing:

Why not use your ISP's SMTP server?

If your ISP provides you with an e-mail address and POP3 access, then you can configure PHP to use their SMTP server to send mail for you. If you look in your php.ini configuration file, you'll see two settings, like so:

[mail function]
; For Win32 only.
SMTP = mail.telstar-online.net

; For Win32 only.
sendmail_from = (link in profile)

Simply fill in the correct values (it's probably just like mine; "mail." followed by your ISP's server, though if you can't find the correct address let me know) for both settings and try a simple call to mail (http://www.php.net/manual/en/function.mail.php)().

You might take note that PHP's mail() function does not authenticate with the SMTP server, nor does it support such authentication (at least in PHP 4, no clue about 5). If your ISP requires SMTP authentication, you will probably need to look for an alternative to PHP's built-in function.

Such an alternative might be the PHPMailer (http://phpmailer.sourceforge.net/) class.

camowel
06-28-2005, 07:09 PM
Thanks for reminding me about the server thing with my ISP, forgot about it, and yes, I'm quite a simple user tyring to develop my site. However I've set my firewall to block Apache's access to the internet, and I have disabled my port 80 forwards a while ago (after I stumbled on to that regulation looking for something completely unrelated)

I'm going to try and fill in the mail stuff, I just want to get this working properly so I know it will work when I upload my site to a remote server (lots of work to go until then)

Thank you very much for your response, its the first straight answer regarding this subject I've gotten (I've been asking around other forums for months)

If anyone knows, would this work with a gmail account (smtp.gmail.com)? I know you can connect to it and send mail through outlook etc, but would it work for this type of mail?

Thanks again for the great answer!

bradgrafelman
06-28-2005, 07:15 PM
Well, there are RFC standards out there, I don't see why G-Mail should be any exception.

One tip that contradicts the manual (but is required when dealing with certain servers, GMail being one of these servers) is that some non-RFC standard servers dont use "\r\n" as delimiters in headers, but instead use "\n". Keep this in mind if you have custon headers in your call to mail() !

camowel
06-28-2005, 07:25 PM
Wow thanks :) I'll be sure to remember that, depending on which server I choose to use for this,

That gave me an idea, what do you think of something like this:
<?
$pattern = '/\w+\\.([a-zA-Z_\\-]+)\\..+/';

preg_match($pattern, get_ini('SMTP'), $matches);

$server = $matches[1];

switch ($server) {
case 'gmail':
$linebreak = '\n';
case 'ispname':
$linebreak = '\r\n';
}

$headers = "SomeHeader: SomeThingElse".$linebreak;

?>

Just a quick idea, I'm going to try it out as soon as I find out my ISP's smtp address

Thanks again for your help so far!

bradgrafelman
06-28-2005, 07:36 PM
what do you think of something like this:

I'll let you know once I see test results. ;)

camowel
06-28-2005, 08:31 PM
Ok, here is what I did:

<?php
$subject = 'Testing PHP mail()';
$body = 'Does it work?';

preg_match('/\w+\.([a-zA-Z0-9_\-]+?)\..*/', ini_get('SMTP'), $matches);

switch ($matches[1]) {
case 'gmail':
echo 'Your using Gmail!';
$linebreak = '\n';
break;
case $myisp:
echo 'Your using '.$myisp;
default:
$linebreak = '\r\n';
break;
}

$headers = "MIME-Version: 1.0$linebreak";
$headers .= "Content-type: text/plain; charset=iso-8859-1$linebreak";
$headers .= "X-Priority: 3$linebreak";
$headers .= "X-MSMail-Priority: Normal$linebreak";
$headers .= "X-Mailer: php$linebreak";
$headers .= "From: \"".$fromname."\" <".$fromaddress.">\n";

mail($to, $subject, $body, $headers);
?>

I set a few variables above that and didn't display them (to avoid spam)

Here are the results using my ISP:
SMTP = smtp.myisp.com
Output from the script:
Your using myisp
Warning: mail() [function.mail]: Failed to connect to mailserver at "smtp.myisp.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Apache2\htdocs\index.php on line 32

Once again I've replaced my ISP for a bit of spam protection

SMTP = smtp.gmail.com
Output from script:
Your using Gmail!
Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 Must issue a STARTTLS command first p4sm2159249nzc in C:\Apache2\htdocs\index.php on line 32

It seems like the gmail problem would be the easiest to solve to me, as my ISP need authentication (which is probably hard to do)

I'll be back online later, so no rush

Thanks once again for your help!

bradgrafelman
06-28-2005, 09:40 PM
Actually, no - dealing with simple authentication with PHPMailer (link in one of my above posts) would be much, much easier than dealing with Gmail, because of one fact: G-Mail uses a secure connection.

Try taking a look at PHPMailer. You'll have to configure it, but it's making me nervous that it said it "Failed to connect" rather that it failed to send the mail (due to lack of authentication). Are you sure you used the correct SMTP address for your ISP? If you aren't sure, open your mailing software and look at the SMTP Server setting (and port!). If all else fails, call your ISP and ask them to give you the SMTP server and port number because you've lost it, need to reconfigure, whatever, though I would advise that you not say it's for a PHP script that sends mail through their servers!

camowel
06-28-2005, 11:53 PM
I've set up the smtp server and port properly, now I get:
Warning: mail() [function.mail]: SMTP server response: 530 authentication required - for help go to ***** in \Apache2\htdocs\index.php on line 32
So I'm looking at PHPMailer

If you know of any tutorials or articles on authenticating, that would be great too, I like making my own classes over using pre-made ones, (I've acctually tried PHPMailer before, it didn't work out too well because I didn't have things set up properly ;) )

camowel
06-29-2005, 12:55 AM
Thanks sooooo much for your help!

With a bit of reading through the PHPMailer documentation (and some clever clever thinking) I managed to get it working, and authenticating with my ISP's SMTP server.

Thank you once again, I have been wanting to get mail() to work for years! (yup.. ever since I found out it was possible to send mail with php I've wanted to, never had any good support before though).

zarkolazarov
04-29-2007, 10:47 PM
Windows XP Home? This would indicate, to me, that you are a simple user at home attempting to run a website, using a simple home computer, connected to a simple residential ISP.

Other than the various warnings I would voice concerning the EULA, TOS, AUP, etc. your ISP might hold that strictly prohibits any webserver or SMTP server (as most ISP's do, especially broadband), I would ask you one thing:

Why not use your ISP's SMTP server?

If your ISP provides you with an e-mail address and POP3 access, then you can configure PHP to use their SMTP server to send mail for you. If you look in your php.ini configuration file, you'll see two settings, like so:



Simply fill in the correct values (it's probably just like mine; "mail." followed by your ISP's server, though if you can't find the correct address let me know) for both settings and try a simple call to mail (http://www.php.net/manual/en/function.mail.php)().

You might take note that PHP's mail() function does not authenticate with the SMTP server, nor does it support such authentication (at least in PHP 4, no clue about 5). If your ISP requires SMTP authentication, you will probably need to look for an alternative to PHP's built-in function.

Such an alternative might be the PHPMailer (http://phpmailer.sourceforge.net/) class.

I happend to have the exact same problem with my smtp configuration and this totally saved the day. Thank you very very much