sending mail thru localhost

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cfemocha
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2008 9:47 am

sending mail thru localhost

Post by cfemocha »

my setup: PHP5 on Win xp with Apache.

I would like to test the PHP mail function without going to the net using localhost. My code when executed, does absolutely
nothing. No error, no reports to the maillog. It always returns false (not send).

Code: Select all

 
ini_set('SMTP', 'localhost');
ini_set('smtp_port', 25);
ini_set('sendmail_from', 'myemail@hotmail.com');
 
if (mail('toemail@hotmail.com', 'mail subject', 'mail message ', 'header'))
echo "Mail Sent.";
else
echo 'not send';
 
I don't know what else to try, please help.
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: sending mail thru localhost

Post by dyluck »

I had the exact same issue except with a different smtp server...

Firstly I would use phpmailer for sending emails :) I use it for my site and it really helps one understand how emailing works through php.

PHPMailer website here: http://phpmailer.codeworxtech.com/
Download for PHP4: http://sourceforge.net/project/showfile ... _id=604596
Download for php5: http://sourceforge.net/project/showfile ... id=604592\

Secondly, make sure your ISP isn't blocking your SMTP port. My ISP was blocking port 25... Also, some pop3/smtp mailers like hotmail may use different smtp ports, make sure you check this.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: sending mail thru localhost

Post by WebbieDave »

cfemocha wrote:

Code: Select all

ini_set('SMTP', 'localhost');
Do you have an SMTP server installed and running on your localhost?
cfemocha
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2008 9:47 am

Re: sending mail thru localhost

Post by cfemocha »

thank you for your replies.

mail() function used to work for me perfectly when i was using php4 and i didn't install SMTP server... i thought it came with win xp, no? if i need to install SMTP server, how do i start?

how do i check if ISP is not blocking port 25? Ive recently installed kaspersky anti-virus, although i've checked it's not blocking port 25 but could it be the reason that mail() function is not working for me locally?

thank you dyluck, i tried using PHPMailer and this is the error message i got: "Mailer Error: Could not instantiate mail function."

please help.
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: sending mail thru localhost

Post by dyluck »

Ok to answer your questions.

The "send out through SMTP" should be part of your apache addons, its not really a separate install... you have to enable SMTP in both your php.ini file (the one being used by the server, be carefull sometimes there are more then 1 php.ini file) and on your server too.... I assume you are using Apache.

in your active php.ini file, (if you are using apache, go into your apache folder under BIN folder and there should be a php.ini file in there, that is the active one)

uncomment the line here:
extension=php_smtp.dll <------------------

then look for the following lines:

[mail function]
; For Win32 only.
SMTP = smtp.yourdomain.com
smtp_port = 25

; For Win32 only.
sendmail_from = noreply@yourdomain.com

Ok in your PHPMAILER you only require a couple of the php files.
class.phpmailer.php and class.smtp.php

In the class.phpmail.php
edit the following lines:

public $From = 'noreply@yourdomain.com';

public $FromName = 'Name you want to display in the From field in your email';

public $Mailer = 'mail';

public $Host = 'smtp.yourdomain.com';

public $Port = 25;

public $SMTPAuth = true; <--- this is if your smtp requires a password!

public $Username = 'noreply@yourdomain.com'; <---- Make sure this is an ACTUAL EMAIL BOX!

public $Password = 'MyPassword1'; <---- Actual password for logging into that email box

Now save that...
Edit class.smtp.php


public $SMTP_PORT = 465;
<-- this is the port that you can use to send out your email.... I use SHAW CABLE for my ISP and they block 25 but they don't block 465... if your ISP blocks 25, you may need to change port 25 in the other file as well, but try it like how I have it first... Remember some mail sites requrire a specific SMTP port so make sure you check forums for the proper smtp port....

I noticed you are trying to use hotmail... that's really iffy... You may need to use a secure connection to the smtp.. this requires more work in your php files... Im not sure about this for hotmail, but you definitely need it for Gmail!. If you need to, set the following for however you would need to for hotmail. (only if it is required) you will need to activate SSL on your server settings and uncomment the line that deals with ssl in your php.ini file....
$host = "ssl://smtp.gmail.com";
public $Host = 'ssl://smtp.yourdomain.com'; etc. so keep an eye open for that.

It would be much easier for you to create a noreply@yourdomain.com email box and use the registrar's email smtp server in my oppinion. As far as I know, it is more streamline to use and generally doesn't use SSL.

here is an example of an email sent from from PHP mailer, this would go in whatever file you want to send the email from.

Code: Select all

require("class.phpmailer.php");
 
$mail = new PHPMailer();
 
$mail->From     = "noreply@yourdomain.com";
$mail->FromName = "Name displayed in from field";
$mail->Host     = "smtp.yourdomain.com";
$mail->Mailer   = "smtp";
 
<p>Dear '.$firstname.';</p>
<p>Thank you for registering!<br>
  Note: This message is automated, so please do not reply.</p>
<p><strong>Your username is:</strong> <span class="style3">'.$newun.'</span><br>
  <strong>Your password is:</strong> <span class="style3">Protected for security puroposes</span><br>
  <strong>Your referer id is:</strong> <span class="style3">'.$uid.'</span></p>
<p>To complete your registration and begin using us, please  click on the link below.</p>
<p class="style4">Activate now: <a href="http://www.exampledomain.com/activate.php?activ='.$activecode.'">http://www.ehapi.com/activate.php?activ='.$activecode.'</a></p>
<p>Once you have clicked on the link, you will be brought to  the login page and will be able to access the member’s area.</p>
<p>Good luck!</p>
<p>Sincerely,</p>
<p>Your Admin and your referrer: '.$ref.'</p>';
 
    // Plain text body (for mail clients that cannot read HTML)
    $text_body  = "Hello " . $firstname . " " . $lastname . ", \n\n";
    $text_body .= "Thank you for registering with us!.\n\n";
    $text_body .= "Note: This message is automated, so please do not reply. \n";
    $text_body .= "Your username is: ".$newun."";
    $text_body .= "Your password is: Protected for security";
    $text_body .= "Your referer id is: ".$uid."";
    $text_body .= "";
    $text_body .= "To complete your registration and begin using us, please copy and paste the URL ";
    $text_body .= "below into your browser address bar and hit enter.";
    $text_body .= "";
    $text_body .= "http://www.exampledomain.com/activate.php?activ=".$activecode."";
    $text_body .= "";
    $text_body .= "Once you have clicked on the link, you will be brought to the login page and will be able";
    $text_body .= "to access the member’s area.";
    $text_body .= "";
    $text_body .= "Good luck!";
    $text_body .= "";
    $text_body .= "Sincerely,";
    $text_body .= "Your Admin and your referrer: ".$ref."";
 
    $mail->Subject = "Welcome to Example Domain - Please Activate Your Account";
    $mail->Body    = $body;
    $mail->AltBody = $text_body;
    $mail->AddAddress($email, $firstname." ".$lastname);
    //$mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");  
 
    if(!$mail->Send()) {
        echo "There has been a mail error sending to " . $email . " please try again<br>";
        
    // Clear all addresses and attachments for next loop
    $mail->ClearAddresses();
    $mail->ClearAttachments();
    exit;}
    
     $mail->ClearAddresses();
    $mail->ClearAttachments();
//finish mail
 
Are you hosting your own site or are you using a service??? If you are hosting your own, what server are you using including version?

Good luck, took me a good couple days to figure out how to get email out with my domain's SMTP because I'm hosting my own site until its fully opperational..
By the way, if you are not hosting your own site on your own server, and you are using a professional hosting company, you shouldn't have SMTP port problems.

Let me know how it goes!
cfemocha
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2008 9:47 am

Re: sending mail thru localhost

Post by cfemocha »

thank you dyluck for your time and help.

I followed your method and changed all necessary variables and still get the same error message "Mailer Error: Could not instantiate mail function."

Im not hosting my own site and i've created a local email box. I've also noticed that my kaspersky pops up the same message whenever i tried to send mail using php - "The application apache.exe cannot establish connection with server 127.0.0.1. If you have a firewall installed, please check that the application avp.exe is allowed internet access."
I've checked the windows firewall several times and it's not blocking the local server, i've even turned off both windows firewall and anti-visrus software and the mail() function still cannot be instantiated.

im running out of ideas. any help is appreciated.
User avatar
dyluck
Forum Commoner
Posts: 54
Joined: Thu Jun 26, 2008 1:44 pm

Re: sending mail thru localhost

Post by dyluck »

Ok well that definitely sheds some light on the subject. You should contact your hosting company and get the proper information. They may not be using localhost for php.... Only they would be able to answer that.
127.0.0.1 is localhost... that said, everywhere where it says "localhost" on those phpmailer pages, you would have to probably change to whatever host they have set in their servers. Give them a call and let them know what you are doing. 99% of hosts are pretty good about telling you what you need.
cfemocha
Forum Newbie
Posts: 4
Joined: Thu Jun 26, 2008 9:47 am

Re: sending mail thru localhost

Post by cfemocha »

thank you dyluck for your replies.

I contacted my hosting company but didn't get any helpful information so I tried another option, install xampp.

amazingly, everything starts to work. mail function now works perfectly.
For anyone that experienced the same problem, can try dyluck's method or if you are a newbie like me, can try install xampp.

Thank you for your help again!!
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: sending mail thru localhost

Post by avirup »

hi

At the PHP.ini(in apache bin folder) file i have set SMTP as smra.dataone.in
and the port no is 25.

But in the PHP script I am getting error where I am using gmail id.Coz I dont have my ISP id at all.
Was that right to use??

Altough I am gettting error still and surely not receiving any sort of mail yet.
The error is
“Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini”

Code is seems to be ok…I am using Wamp server 2 ….!!
Anybody plz help me out!!!
Thank you
Last edited by avirup on Mon Sep 15, 2008 3:13 pm, edited 1 time in total.
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: sending mail thru localhost

Post by avirup »

Here is the code I'm using

Code: Select all

<?php
 
    $to = "avirup.it@gmail.com";
    $from = "noreply@dataone.com";
    $subject = "Test Message";
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From: mydomainName <$from>\r\n";
    $message = "Hi This is a test mail";
    if(mail($to, $subject, $message, $headers))
        {
            echo "<BR>Mail Sent";
        }
    else
        {
            echo "<BR>Mail Could not be sent";
        }
?>
 
avirup
Forum Newbie
Posts: 12
Joined: Mon Sep 15, 2008 3:07 pm

Re: sending mail thru localhost

Post by avirup »

I read ur idea up there..but in my php.ini file there is no such a line which I can active it...

Only these lines are available

;extension=php_shmop.dll
;extension=php_snmp.dll
;extension=php_soap.dll
;extension=php_sockets.dll
extension=php_sqlite.dll
Post Reply