SitePoint Sponsor

User Tag List

Results 1 to 25 of 25

Thread: Email Problem

  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email Problem

    Hi all,

    Why won't this send an email. SMTP is running.

    PHP Code:
     $headers  "MIME-Version: 1.0\r\n"
     
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"
     
    $headers .= "From: Training Department\n"
     
    $subject  ucwords('hello'); 
     
    $content  "From: ".ucwords($name)."\n"
                         
    ."Message: \n Hi all \n"
                         
    .$note."Regards, \n"
                         
    .ucwords($name);
     
     foreach (
    $email as $key => $value
     {
                      
    $value .= '@yahoo.com';
                     echo 
    $key .'=>'$value.'<br />';
                     
                     
    mail($value$subject$content$headers);
     } 

  2. #2
    designer
    Join Date
    Dec 2004
    Location
    Over the hill and through the woods...
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $value should be who the email is to, not from. The first thing in a mail syntax is always the recepients email.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. I know that. $value is who the email should be sent to.

  4. #4
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What trouble are you having? Sends ok for me!

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It doesn't seem to send the email.

    Here's my php.ini settings:
    PHP Code:
     ; For Win32 only.
     
    SMTP localhost
     smtp_port 
    25
     
     
    ; For Win32 only.
     
    sendmail_from me@mysite.com 
    What could be wrong?

  6. #6
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you ever managed to send mail through this server? Obviously the webserver and mailserver are running on the same machine, right?

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The webserver is on this machine.
    For the mail server all I did was get the SMTP service running on the same computer. (Using Windows)

    Anything else I should have done?

  8. #8
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you solved this yet? If not I have a solution.

  9. #9
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check your isp, and see if they disable port 25. (Mine does, so I can't send mail)
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  10. #10
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or check your SMTP log to see if at least he try to send the email, even if the ISP block port 25 the SMTP on localhost should log the attempt to send the email

  11. #11
    Under Construction silver trophybronze trophy AussieJohn's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    776
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    You might want to try and check the logs of your SMTP server.
    At home I use 'PostCast Server' - a free SMTP server for windows to test mail scripts. (http://www.postcastserver.com).

    It has to option to only 'manually' send the mail (so you have the chance to open up the admin program and see what mails are waiting to get sent out).

    I would also suggest you try:
    PHP Code:
     <?php
    $date 
    =  date('Y-h-d H:i:s');
    if (
    mail('sometest@account.com''Test''This is a test mail - at ' $date)) {
      print 
    'Mail was sent...'
    }
    else {
      print 
    'Mail was not sent - check any error output that might be printed.';
    }
    ?>
    This will help you establish wether or not php's mail function is actually working on your box.

    Hope this helps ...
    var details = {
    . . web: "afterlight.com.au",
    . . photos: "jvdl.id.au",
    . . psa: "usethelatestversion.com"
    }

  12. #12
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Of course it's not working. He is not sending authentication, which on a windows server is impossible with mail(). There are only two choices: 1) configure the mailserver not to require authentication from localhost or 2) use fsockopen and send authentication. I have written a function that does number 2 and there is also php mailer or the pear module.

    So if you want to be able to use mail() only option is configure the mailserver to allow it.

  13. #13
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi bokehman. How do I configure the mailserver? or how do I use option 2? fsockopen and authentication?

    Thanks.

    Kevin.

  14. #14
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Option 2.

    Change your mail() command to win_mail and then put the function I am posting at the bottom of your script or in an external file and require() it. Don't forget to fill in your username and pasword in the first line of the function. Tell me how you get on.

    PHP Code:
    <?php
    function win_mail($to$subject$message$headers NULL$username 'your username here'$password 'your password here'$mailserver NULL)
    {    
        function 
    error($error$connect NULL)
        {
            if(isset(
    $_GET['debug']))print $error// comment this out once you get the script working
            
    if(!empty($connect)){
                
    fputs($connect,"QUIT\r\n");
                
    fclose($connect);
            }
            return 
    FALSE;
        } 
    // end function error()
        
        
    if(isset($headers)){
            foreach(
    explode("\n"$headers) as $v){
                if(
    eregi('from:'$v) and eregi('@'$v)){
                    
    $from TRUE;
                    
    $mail_from $v;
                    if(
    preg_match('/<(.+?)>/'$mail_from$match)){
                        
    $mail_from $match['1'];
                    }else{
                        
    $mail_from trim(trim(strstr($mail_from':'), ':'));
                    }
                }
            }
        }
        if(empty(
    $mail_from)) $mail_from ini_get('sendmail_from');
        if(empty(
    $mail_from)) $mail_from ='php-mailer@'.$_SERVER['HTTP_HOST'];
        if(empty(
    $from)) {$from "From: $mail_from\n";}else{$from NULL;}
        if(empty(
    $headers))$headers '';
        
    $message "Date: ".date('D, d M Y H:i:s O')."\n".$from."To: ".$to."\nSubject: ".$subject."\n".$headers."\n\n".$message;
        if(!isset(
    $mailserver))$mailserver ini_get('SMTP');
        if(!
    $connect = @fsockopen($mailserver25$errno$errstr5)) return(error('Could not connect to mail server'));
        
    stream_set_timeout($connect10);
        if (!
    ereg("^220"fgets($connect1024)))return(error('No 220'$connect));
        
    fputs ($connect"EHLO [127.0.0.1]\r\n");
        
    $out fgets $connect1024 );
        while(!
    ereg('^250'$out)&&(!empty($out))){
            
    stream_set_timeout($connect05000);    
            
    $out fgets $connect1024 );
            
    stream_set_timeout($connect5);
        }
        for(
    $tries 15$tries 0$tries--){
            if(
    ereg('^250'$out)){
                if(
    eregi('AUTH=LOGIN|AUTH LOGIN'$out))$auth_login TRUE;
                
    $for_loop_hop $out;
            }elseif(empty(
    $out)){
                
    usleep(100000);
            } 
            
    stream_set_timeout($connect05000);
            
    $out fgets($connect1024);
            
    stream_set_timeout($connect5);
        }
        if(!
    ereg('^250'$for_loop_hop))return(error('No 250'$connect));
        if(!isset(
    $auth_login))return(error('No AUTH LOGIN'));
        
    fputs ($connect"AUTH LOGIN\r\n");
        if (!
    ereg("^334"fgets($connect1024)))return(error('No 334 (username)'$connect));
        
    fputs ($connectbase64_encode($username)."\r\n");
        if (!
    ereg("^334"fgets($connect1024)))return(error('No 334 (password)'$connect));
        
    fputs ($connectbase64_encode($password)."\r\n");
        if (!
    ereg("^235"fgets($connect1024)))return(error('No 235 (not authenticated)'$connect));
        
    fputs ($connect"MAIL FROM: <{$mail_from}>\r\n");
        if (!
    ereg("^250"fgets($connect1024)))return(error('No 250 (after MAIL FROM:)'$connect));
        
    fputs ($connect"RCPT TO: <{$to}>\r\n");
        if (!
    ereg("^250"fgets($connect1024)))return(error('No 250 (after RCPT TO:)'$connect));
        
    fputs($connect,"DATA\r\n");
        
    stream_set_timeout($connect,20);
        if (!
    ereg("^354"fgets($connect,512)))return(error('No 354 (Enter mail)'$connect));
        
    fputs($connect"$message\r\n.\r\n");
        if (!
    ereg("^250"fgets($connect,512)))return(error('No 250 (End of mail)'$connect));
        
    fputs($connect,"QUIT\r\n");
        
    fclose($connect);
        return 
    TRUE;
    }
    ?>

  15. #15
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Bokehman. No luck yet. I must have a semi-colon or ' missing somewhere as all I get is a blank page...

    Any ideas?
    PHP Code:
     <?php
     $to 
    'myname@yahoo.ie';
     
    $message 'hello';
     
    $subject 'News';
     
     
    win_mail($to$subject$message$headers);
     
     
     function 
    win_mail($to$subject$message$headers NULL$username 'uname'$password 'pword'$mailserver NULL)
     {    
         function 
    error($error$connect NULL)
         {
             if(isset(
    $_GET['debug']))print $error// comment this out once you get the script working
             
    if(!empty($connect)){
                 
    fputs($connect,"QUIT\r\n");
                 
    fclose($connect);
             }
             return 
    FALSE;
         } 
    // end function error()
         
         
    if(isset($headers)){
             foreach(
    explode("\n"$headers) as $v){
                 if(
    eregi('from:'$v) and eregi('@'$v)){
                     
    $from TRUE;
                     
    $mail_from $v;
                     if(
    preg_match('/<(.+?)>/'$mail_from$match)){
                         
    $mail_from $match['1'];
                     }else{
                         
    $mail_from trim(trim(strstr($mail_from':'), ':'));
                     }
                 }
             }
         }
         if(empty(
    $mail_from)) $mail_from ini_get('sendmail_from');
         if(empty(
    $mail_from)) $mail_from ='php-mailer@'.$_SERVER['HTTP_HOST'];
         if(empty(
    $from)) {$from "From: $mail_from\n";}else{$from NULL;}
         if(empty(
    $headers))$headers '';
         
    $message "Date: ".date('D, d M Y H:i:s O')."\n".$from."To: ".$to."\nSubject: ".$subject."\n".$headers."\n\n".$message;
         if(!isset(
    $mailserver))$mailserver ini_get('SMTP');
         if(!
    $connect = @fsockopen($mailserver25$errno$errstr5)) return(error('Could not connect to mail server'));
         
    stream_set_timeout($connect10);
         if (!
    ereg("^220"fgets($connect1024)))return(error('No 220'$connect));
         
    fputs ($connect"EHLO [127.0.0.1]\r\n");
         
    $out fgets $connect1024 );
         while(!
    ereg('^250'$out)&&(!empty($out)))
             {
             
    stream_set_timeout($connect05000);    
             
    $out fgets $connect1024 );
             
    stream_set_timeout($connect5);
         }
         for(
    $tries 15$tries 0$tries--)
             {
             if(
    ereg('^250'$out))
                     {
                 if(
    eregi('AUTH=LOGIN|AUTH LOGIN'$out))$auth_login TRUE;
                 
    $for_loop_hop $out;
             }elseif(empty(
    $out))
                     {
                 
    usleep(100000);
             }
             
    stream_set_timeout($connect05000);
             
    $out fgets($connect1024);
             
    stream_set_timeout($connect5);
         }
         if(!
    ereg('^250'$for_loop_hop))return(error('No 250'$connect));
         if(!isset(
    $auth_login))return(error('No AUTH LOGIN'));
         
    fputs ($connect"AUTH LOGIN\r\n");
         if (!
    ereg("^334"fgets($connect1024)))return(error('No 334 (username)'$connect));
         
    fputs ($connectbase64_encode($username)."\r\n");
         if (!
    ereg("^334"fgets($connect1024)))return(error('No 334 (password)'$connect));
         
    fputs ($connectbase64_encode($password)."\r\n");
         if (!
    ereg("^235"fgets($connect1024)))return(error('No 235 (not authenticated)'$connect));
         
    fputs ($connect"MAIL FROM: <{$mail_from}>\r\n");
         if (!
    ereg("^250"fgets($connect1024)))return(error('No 250 (after MAIL FROM:)'$connect));
         
    fputs ($connect"RCPT TO: <{$to}>\r\n");
         if (!
    ereg("^250"fgets($connect1024)))return(error('No 250 (after RCPT TO:)'$connect));
         
    fputs($connect,"DATA\r\n");
         
    stream_set_timeout($connect,20);
         if (!
    ereg("^354"fgets($connect,512)))return(error('No 354 (Enter mail)'$connect));
         
    fputs($connect"$message\r\n.\r\n");
         if (!
    ereg("^250"fgets($connect,512)))return(error('No 250 (End of mail)'$connect));
         
    fputs($connect,"QUIT\r\n");
         
    fclose($connect);
         return 
    TRUE;
     }
     
    ?>

  16. #16
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by obrienkev
    I must have a semi-colon or ' missing somewhere as all I get is a blank page...
    Of course it doesn't print anything... the doesn't contain any print or echo's. It sends ok for me using the code you posted. Try the following to see some output. If it returns mail not sent add ?debug at the end of the URL to see where it crashes.

    PHP Code:
    $to 'myname@yahoo.ie';
    $message 'hello';
    $subject 'News';

    if(
    win_mail($to$subject$message)){
        echo 
    'mail sent';
    }else{
        echo 
    'mail not sent';


  17. #17
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bokehman, it still doesn't return anything. Just blank page. And tried adding ?debug but same story.

  18. #18
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put this at the top:
    PHP Code:
    ini_set('display_errors''1');
    error_reporting(E_ALL); 

  19. #19
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found missing } at end of else clause.

    Now it displays 'mail not sent'
    Last edited by obrienkev; Sep 9, 2005 at 07:13. Reason: syntax error found

  20. #20
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change the function to the following and use ?debug at the end of your URL so it prints the error the mailserver returns.
    PHP Code:
    function win_mail($to$subject$message$headers NULL$username 'user'$password 'pass'$mailserver NULL)
    {    
        function 
    error($error$connect NULL)
        {
            if(isset(
    $_GET['debug']))print $error// comment this out once you get the script working
            
    if(!empty($connect)){
                
    fputs($connect,"QUIT\r\n");
                
    fclose($connect);
            }
            return 
    FALSE;
        } 
    // end function error()
        
        
    if(isset($headers)){
            foreach(
    explode("\n"$headers) as $v){
                if(
    eregi('from:'$v) and eregi('@'$v)){
                    
    $from TRUE;
                    
    $mail_from $v;
                    if(
    preg_match('/<(.+?)>/'$mail_from$match)){
                        
    $mail_from $match['1'];
                    }else{
                        
    $mail_from trim(trim(strstr($mail_from':'), ':'));
                    }
                }
            }
        }
        if(empty(
    $mail_from)) $mail_from ini_get('sendmail_from');
        if(empty(
    $mail_from)) $mail_from ='php-mailer@'.$_SERVER['HTTP_HOST'];
        if(empty(
    $from)) {$from "From: $mail_from\n";}else{$from NULL;}
        if(empty(
    $headers))$headers '';
        
    $message "Date: ".date('D, d M Y H:i:s O')."\n".$from."To: ".$to."\nSubject: ".$subject."\n".$headers."\n\n".$message;
        if(!isset(
    $mailserver))$mailserver ini_get('SMTP');
        if(!
    $connect = @fsockopen($mailserver25$errno$errstr5)) return(error('Could not connect to mail server'));
        
    stream_set_timeout($connect10);
        if (!
    ereg("^220"fgets($connect1024)))return(error('No 220'$connect));
        
    fputs ($connect"EHLO [127.0.0.1]\r\n");
        
    $out fgets $connect1024 );
        while(!
    ereg('^250'$out)&&(!empty($out))){
            
    stream_set_timeout($connect05000);    
            
    $out fgets $connect1024 );
            
    stream_set_timeout($connect5);
        }
        for(
    $tries 15$tries 0$tries--){
            if(
    ereg('^250'$out)){
                if(
    eregi('AUTH=LOGIN|AUTH LOGIN'$out))$auth_login TRUE;
                
    $for_loop_hop $out;
            }elseif(empty(
    $out)){
                
    usleep(100000);
            } 
            
    stream_set_timeout($connect05000);
            
    $out fgets($connect1024);
            
    stream_set_timeout($connect5);
        }
        if(!
    ereg('^250'$for_loop_hop))return(error('No 250 Remote: '.$for_loop_hop$connect));
        if(!isset(
    $auth_login))return(error('No AUTH LOGIN'));
        
    fputs ($connect"AUTH LOGIN\r\n");
        
    $out fgets($connect1024);
        if (!
    ereg("^334"$out))return(error('No 334 (username) Remote: '.$out$connect));
        
    fputs ($connectbase64_encode($username)."\r\n");
        
    $out fgets($connect1024);
        if (!
    ereg("^334"$out))return(error('No 334 (password) Remote: '.$out$connect));
        
    fputs ($connectbase64_encode($password)."\r\n");
        
    $out fgets($connect1024);
        if (!
    ereg("^235"$out))return(error('No 235 (not authenticated) Remote: '.$out$connect));
        
    fputs ($connect"MAIL FROM: <{$mail_from}>\r\n");
        
    $out fgets($connect1024);
        if (!
    ereg("^250"$out))return(error('No 250 (after MAIL FROM:) Remote: '.$out$connect));
        
    fputs ($connect"RCPT TO: <{$to}>\r\n");
        
    $out fgets($connect1024);
        if (!
    ereg("^250"$out))return(error('No 250 (after RCPT TO:) Remote: '.$out$connect));
        
    fputs($connect,"DATA\r\n");
        
    $out fgets($connect1024);
        if (!
    ereg("^354"$out))return(error('No 354 (Enter mail) Remote: '.$out$connect));
        
    fputs($connect"$message\r\n.\r\n");
        
    $out fgets($connect1024);
        if (!
    ereg("^250"$out))return(error('No 250 (End of mail) Remote: '.$out$connect));
        
    fputs($connect,"QUIT\r\n");
        
    fclose($connect);
        return 
    TRUE;


  21. #21
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get: No AUTH LOGIN mail not sent

    So I guess pword/username wrong. I log onto the server from my own computer(internal server) Use my own login details.

    So what should I use for $username and $password?

  22. #22
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No. Its not that!

    Go to the dos command prompt and type in

    telnet localhost 25

    then press enter. It will return some lines starting 220. Then enter

    EHLO [127.0.0.1]

    then press enter. It should return some lines that start with 250. Copy those lines here so I can see them. Easiest way might be a screen grab.

  23. #23
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get this...

    250 - Webservertest.cte.com Hello [127.0.0.1]
    250 - Turn
    250 - Size 2097152
    250 - DSN
    250 - VRFY
    250 - OK

  24. #24
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK! Well for my code to work it needs to return AUTH LOGIN which 99% of mailservers do. Let me think how to bypass this.

  25. #25
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any more ideas on this? Still no solution.

    Thanks.

    Kevin.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •