Sending mail with php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carllucas
    New Member
    • Oct 2006
    • 35

    Sending mail with php

    php mail... I did this ages before but I'm having problems with it.

    Code:
    <?php
    
    //--- Start mail script ---
    
    $to = "address@gmail.com";
    
    $from = "address@gmail.com";
    
    $subject = "Please work";
    
    $body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
    
    $header = "Blah blah";
    
    $success = mail($to, $subject, $body, $header);
    
    if(!$success) {
    	echo "error";
    } else {
    	echo "success";
    }
    
    //--- End mail script ---
    
    ?>
    Can anyone explain why this won't work. I keep getting my "error" echo.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    The from should go into the 4th parameter (also called the header).
    [PHP]<?php
    //--- Start mail script ---
    $to = "address@gmail. com";
    $from = "address@gmail. com";
    $subject = "Please work";
    $body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
    $header = "Blah blah";
    $success = mail($to, $subject, $body, $from);
    if(!$success) {
    echo "error";
    } else {
    echo "success";
    }
    //--- End mail script ---
    ?>[/PHP]

    Ronald :cool:

    Comment

    • carllucas
      New Member
      • Oct 2006
      • 35

      #3
      For some reason this still doesn't seem to work.

      I even tried putting in the parameter without using variables it just won't work : (

      Comment

      • LacrosseB0ss
        New Member
        • Oct 2006
        • 112

        #4
        I've never seen it assigned to a variable before. Whenever I've sent an e-mail or seen code for it it's always looked like:

        [php]
        $to = someone@domain. com
        $subject = //whatever the subject is
        $text = //body text of the e-mail
        $headers = //e-mail headers

        mail($to, $subject, $text, $headers);
        [/php]

        As I said, I haven't seen it been assigned to a variable before. I find it hard to believe that could be the problem. But I also find it hard to believe ronverdonk can be wrong.

        Hope this helps
        LB

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          I am very sorry, I made a mistype in the code I showed. I forgot to prefix the sender with the literal 'From: '. The following is correct, so it should be okay for you now.
          [php]<?php
          //--- Start mail script ---
          $to = "address@gmail. com";
          $from = "From: address@gmail.c om";
          $subject = "Please work";
          $body = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
          $header = "Blah blah";
          $success = mail($to, $subject, $body, $from);
          if(!$success) {
          echo "error";
          } else {
          echo "success";
          }
          //--- End mail script ---
          ?>
          [/php]
          Ronald :cool:

          Comment

          • khalidbaloch
            New Member
            • Oct 2006
            • 61

            #6
            sender's email address should be appended in header
            so you code should look like this
            Code:
            $to="email@domain.com
            $subject = "this i subjec\r\n";
            $headers = "From: receipt@domain.com\r\n";
            $headers .= "Reply-To:receipt@domaincom\r\n";
            $headers .= "Return-Path: receipt@domain.com\r\n";
            $body= "type your messege here ";
            
            mail("$to","$subject","\n$body", "$headers\r");

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Originally posted by khalidbaloch
              $to="email@doma in.com
              $subject = "this i subjec\r\n";
              $headers = "From: receipt@domain. com\r\n";
              $headers .= "Reply-To:receipt@doma incom\r\n";
              $headers .= "Return-Path: receipt@domain. com\r\n";
              $body= "type your messege here ";

              mail("$to","$su bject","\n$body ", "$headers\r ");
              From the mail settings in this case only the 'From: ' is required in the 4th mail parameters (also called headers in the docs). The other header content is nice but NOT mandatory.

              Ronald :cool:

              Comment

              • carllucas
                New Member
                • Oct 2006
                • 35

                #8
                IT WON'T WORK!!! Aaargh! why?!?
                even copying and pasting, won't work!?!

                Code:
                $to = "email@gmail.com";
                $subject = "Subject";
                $body = "Body text and such stuff";
                $header = "From: email@gmail.com";
                
                $success = mail($to, $subject, $body, $header);
                
                if(!$success) {
                	echo "error";
                } else {
                	echo "sent";
                }
                Now from what I gather this should work provided that "email@gmail.co m" is replaced with a valid address. But it doesn't! I keep getting "error". I even tried it without assigning the function to variable $success... same story. I don't understand what sort of minute syntax tithle is missing here.

                I appriciate all the help, guys though this is UNIMAGINABLY trying :(

                Comment

                • carllucas
                  New Member
                  • Oct 2006
                  • 35

                  #9
                  Okay... I ran phpinfo() and under the section called Configuration PHP CORE I got this...

                  disable_functio ns: getmyinode, getopt, getrusage, extension_loade d, dl, mysql_pconnect, crack_check, crack_closedict , crack_getlastme ssage, crack_opendict, fsockopen, pfsockopen, mysql_list_dbs, mysql_stat, ini_get, ini_get_all, ini_alter, ini_set, get_current_use r, get_defined_con stants, get_include_pat h, php_ini_scanned _files, php_uname, phpcredits, restore_include _path, set_include_pat h, set_time_limit, version_compare , zend_version, getmypid, getmyuid, getmygid, assert_options, assert, fopen, fwrite, fread, file, fpassthru, file, mail, opendir, readdir, closedir

                  This seems to be getting more complicated than i care for. Can anyone please tell me why are some functions disabled and why is mail one of those functions and how can i enable some of these function or can i not.

                  ronverdonk?

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    You most probably are running PHP in safe mode. That mode is set at installation of PHP. This mode disables the functions you mention, see also the list of disabled functions at Functions restricted/disabled by safe mode
                    Originally posted by PHP manual
                    The PHP safe mode is an attempt to solve the shared-server security problem. It is architecturally incorrect to try to solve this problem at the PHP level, but since the alternatives at the web server and OS levels aren't very realistic, many people, especially ISP's, use safe mode for now.
                    See the PHP manual for a discussion on safe mode at Safe Mode

                    If you installed PHP yourself, remove the safe mode. If not, you'd better have a talk with your server provider.

                    Ronald :cool:

                    Comment

                    • carllucas
                      New Member
                      • Oct 2006
                      • 35

                      #11
                      Okay, thanks. I've emailed my server provider... they've promised a quick response so I'll see.

                      At least I can now stop bashing my head against the wall.

                      Comment

                      Working...