SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: sending emails!

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Reading
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sending emails!

    In my projectI am trying to set up and page in which you can send emails to the members, does anyone know any useful websites or examples I can use.

    I used the following example, however it gave me this error which I was unable to fix:

    Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Program Files\Apache Group\Apache2\htdocs\feedback.php on line 18

    I have configured my php.ini file correctly. And used that information in the code below.

    The code I used is as follows:

    PHP Code:
    <html>
        <head><title>Feedback</title></head>
        <body>

            <?php

            
    // Handle POST method.
            
    if ($_POST)
            {
                
    $name $_POST['name'];
                
    $email $_POST['email'];
                
    $comments $_POST['comments'];

                
    // Compose simple text message:
                
    $message "Message from $name ($email)\n\nComments:\n\n$comments";

                
    // Send message to 
                
    mail("khan1uk@yahoo.co.uk""localhost"$message);

                
    // Thank the generous user
                
    echo "<h1>Cheers!</h1>\n";
            }
            else
            {

            
    ?>

            <h1>Feedback</h1>

            <form action="<?= $PHP_SELF ?>" method="post">

                <p>Name: <input type="text" name="name" /></p>

                <p>Email: <input type="text" name="email" /></p>

                <p>Comments:</p>
                <p><textarea name="comments"></textarea></p>

                <p><input type="submit" value="Send!" /></p>

            </form>

            <?php

            
    }

            
    ?>

        </body>
    </html>
    Also one question, most of the examples that I have seen from books or the net, send to one specific email address, am I right in thinking this or can I send to any email address entered.

    Atia

  2. #2
    Free Geek computerages's Avatar
    Join Date
    Oct 2004
    Location
    /dev/null
    Posts
    1,071
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Atia
    In my projectI am trying to set up and page in which you can send emails to the members, does anyone know any useful websites or examples I can use.

    I used the following example, however it gave me this error which I was unable to fix:

    Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Program Files\Apache Group\Apache2\htdocs\feedback.php on line 18

    I have configured my php.ini file correctly. And used that information in the code below.

    The code I used is as follows:

    PHP Code:
    <html>
        <head><title>Feedback</title></head>
        <body>

            <?php

            
    // Handle POST method.
            
    if ($_POST)
            {
                
    $name $_POST['name'];
                
    $email $_POST['email'];
                
    $comments $_POST['comments'];

                
    // Compose simple text message:
                
    $message "Message from $name ($email)\n\nComments:\n\n$comments";

                
    // Send message to 
                
    mail("khan1uk@yahoo.co.uk""localhost"$message);

                
    // Thank the generous user
                
    echo "<h1>Cheers!</h1>\n";
            }
            else
            {

            
    ?>

            <h1>Feedback</h1>

            <form action="<?= $PHP_SELF ?>" method="post">

                <p>Name: <input type="text" name="name" /></p>

                <p>Email: <input type="text" name="email" /></p>

                <p>Comments:</p>
                <p><textarea name="comments"></textarea></p>

                <p><input type="submit" value="Send!" /></p>

            </form>

            <?php

            
    }

            
    ?>

        </body>
    </html>
    Also one question, most of the examples that I have seen from books or the net, send to one specific email address, am I right in thinking this or can I send to any email address entered.

    Atia
    You need to have a SMPT server in order to send mail with php. Check out http://php.net/mail for more info.

  3. #3
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you can edit the php.ini file to use another mail server.

    - Mark

  4. #4

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Atia
    Also one question, most of the examples that I have seen from books or the net, send to one specific email address, am I right in thinking this or can I send to any email address entered.
    You are right.

  5. #5
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most hosts will give you pop3 accounts nowadays, if yours does, why not look for a pop3 client? (I have a semi-completed one if you would like to use it, but its a bit buggy still)

  6. #6

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geniusgoalie
    Most hosts will give you pop3 accounts nowadays, if yours does, why not look for a pop3 client? (I have a semi-completed one if you would like to use it, but its a bit buggy still)
    Maybe I missed something but POP3 isnt the suspect in this case I guess .

  7. #7
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by drzoid
    Maybe I missed something but POP3 isnt the suspect in this case I guess .
    oops sorry, disregard my message, I didn't see exactly what you were trying to do!

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Reading
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    drzoid,
    So how do send mail so I can send it to anyone whom I choose from my database.

    I have changed a few things in my phi.ini file, and am still working on it. Looking at php.net which has been useful.

    Atia

  9. #9

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Atia
    drzoid,
    So how do send mail so I can send it to anyone whom I choose from my database.

    I have changed a few things in my phi.ini file, and am still working on it. Looking at php.net which has been useful.

    Atia
    I am not exactly sure about your message. Changing the recipient is quite easy. Just as easy as setting it - which you already did . Simply specify it in the first parameter you pass to mail().

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Reading
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok guys,

    I used one of the examples given on this very forum.
    It was very good and did everything that i wanted to do. BUT its not sending the emails.

    It doesnt give me and error in the code or anything, it just loops out the message from the code if the email can't be sent.

    Which is the following:
    Mail could not be sent. Sorry!

    This is the following code I have used, Cant anyone point out what I am doing wrong!

    PHP Code:
    <html>
    <head>
    <title> Sending Email </title>
    </head>
    <body>
    <?php
    // Read POST request params into global vars
    $to      $_POST['to'];
    $from    $_POST['from'];
    $subject $_POST['subject'];
    $message $_POST['message'];

    // Obtain file upload vars
    $fileatt      $_FILES['fileatt']['tmp_name'];
    $fileatt_type $_FILES['fileatt']['type'];
    $fileatt_name $_FILES['fileatt']['name'];

    $headers "From: $from";

    if (
    is_uploaded_file($fileatt)) {
      
    // Read the file to be attached ('rb' = read binary)
      
    $file fopen($fileatt,'rb');
      
    $data fread($file,filesize($fileatt));
      
    fclose($file);

      
    // Generate a boundary string
      
    $semi_rand md5(time());
      
    $mime_boundary "==Multipart_Boundary_x{$semi_rand}x";
      
      
    // Add the headers for a file attachment
      
    $headers .= "\nMIME-Version: 1.0\n" .
                  
    "Content-Type: multipart/mixed;\n" .
                  
    " boundary=\"{$mime_boundary}\"";

      
    // Add a multipart boundary above the plain message
      
    $message "This is a multi-part message in MIME format.\n\n" .
                 
    "--{$mime_boundary}\n" .
                 
    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                 
    "Content-Transfer-Encoding: 7bit\n\n" .
                 
    $message "\n\n";

      
    // Base64 encode the file data
      
    $data chunk_split(base64_encode($data));

      
    // Add file attachment to the message
      
    $message .= "--{$mime_boundary}\n" .
                  
    "Content-Type: {$fileatt_type};\n" .
                  
    " name=\"{$fileatt_name}\"\n" .
                  
    //"Content-Disposition: attachment;\n" .
                  //" filename=\"{$fileatt_name}\"\n" .
                  
    "Content-Transfer-Encoding: base64\n\n" .
                  
    $data "\n\n" .
                  
    "--{$mime_boundary}--\n";
    }

    // Send the message
    $ok = @mail($to$subject$message$headers);
    if (
    $ok) {
      echo 
    "<p>Mail sent! Yay PHP!</p>";
    } else {
      echo 
    "<p>Mail could not be sent. Sorry!</p>";
    }
    ?>
    </body>
    </html>

  11. #11

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all remove the @ in front of the mail() call to get a more detailed error message. Furthermore, are you running this script on Windows or on Unix?

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Reading
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    without the @ infront of the mail() it gives me the following error:

    Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Program Files\Apache Group\Apache2\htdocs\mail.php on line 57

    I dont get it, because I have configured the php.ini file correctly.
    Also I am running the script in Windows.

  13. #13

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you restarted Apache after having changed the php.ini?

  14. #14
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Reading
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes I have restarted Apache!


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
  •