It is currently Thu Mar 28, 2024 10:49 am


All times are UTC


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: How to: Send mail using an external SMTP server
PostPosted: Sun Feb 27, 2005 1:17 am 
Noobie
Noobie

Joined: Mon Feb 21, 2005 7:02 pm
Posts: 27
Scroll down for files (in next post)
  1. Paste the code below into a file named smtp.php:
    Code:
    <?php

    function server_parse($socket, $response)
    {
       while (substr($server_response, 3, 1) != ' ')
       {
          if (!($server_response = fgets($socket, 256)))
          {
             die("Couldn't get mail server response codes");
          }
       }

       if (!(substr($server_response, 0, 3) == $response))
       {
          die("Ran into problems sending Mail. Response: $server_response");
       }
    }

    // Replacement or substitute for PHP's mail command
    function smtpmail($server, $user, $pass, $from, $subject, $message, $mail_to, $headers = '')
    {
       $message = preg_replace("#(?<!\r)\n#si", "\r\n", $message);

       if ($headers != '')
       {
          if (is_array($headers))
          {
             if (sizeof($headers) > 1)
             {
                $headers = join("\n", $headers);
             }
             else
             {
                $headers = $headers[0];
             }
          }
          $headers = chop($headers);

          $headers = preg_replace('#(?<!\r)\n#si', "\r\n", $headers);

          $header_array = explode("\r\n", $headers);
          @reset($header_array);

          $headers = '';
          while(list(, $header) = each($header_array))
          {
             if (preg_match('#^cc:#si', $header))
             {
                $cc = preg_replace('#^cc:(.*)#si', '\1', $header);
             }
             else if (preg_match('#^bcc:#si', $header))
             {
                $bcc = preg_replace('#^bcc:(.*)#si', '\1', $header);
                $header = '';
             }
             $headers .= ($header != '') ? $header . "\r\n" : '';
          }

          $headers = chop($headers);
          $cc = explode(', ', $cc);
          $bcc = explode(', ', $bcc);
       }

       if (trim($subject) == '')
       {
          die("No email Subject specified");
       }

       if (trim($message) == '')
       {
          die("Email message was blank");
       }

       if( !$socket = fsockopen($server, 25, $errno, $errstr, 20) )
       {
          die("Could not connect to smtp host : $errno : $errstr");
       }

       server_parse($socket, "220");

          fputs($socket, "EHLO $server\r\n");
          server_parse($socket, "250");

          fputs($socket, "AUTH LOGIN\r\n");
          server_parse($socket, "334");

          fputs($socket, base64_encode($user) . "\r\n");
          server_parse($socket, "334");

          fputs($socket, base64_encode($pass) . "\r\n");
          server_parse($socket, "235");

       fputs($socket, "MAIL FROM: <$from>\r\n");
       server_parse($socket, "250");

       $to_header = '';

       $mail_to = (trim($mail_to) == '') ? 'Undisclosed-recipients:;' : trim($mail_to);
       if (preg_match('#[^ ]+\@[^ ]+#', $mail_to))
       {
          fputs($socket, "RCPT TO: <$mail_to>\r\n");
          server_parse($socket, "250");
       }

       @reset($bcc);
       while(list(, $bcc_address) = each($bcc))
       {
          $bcc_address = trim($bcc_address);
          if (preg_match('#[^ ]+\@[^ ]+#', $bcc_address))
          {
             fputs($socket, "RCPT TO: <$bcc_address>\r\n");
             server_parse($socket, "250");
          }
       }

       @reset($cc);
       while(list(, $cc_address) = each($cc))
       {
          $cc_address = trim($cc_address);
          if (preg_match('#[^ ]+\@[^ ]+#', $cc_address))
          {
             fputs($socket, "RCPT TO: <$cc_address>\r\n");
             server_parse($socket, "250");
          }
       }

       fputs($socket, "DATA\r\n");

       server_parse($socket, "354");

       fputs($socket, "Subject: $subject\r\n");

       fputs($socket, "To: $mail_to\r\n");

       fputs($socket, "$headers\r\n\r\n");

       fputs($socket, "$message\r\n");

       fputs($socket, ".\r\n");
       server_parse($socket, "250");

       fputs($socket, "QUIT\r\n");
       fclose($socket);

       return TRUE;
    }

    ?>

  2. In the file where send() would be add the following just after <?php
    Code:
    include("smtp.php");

  3. Now you can call smtpmail($server, $user, $pass, $from, $subject, $message, $mail_to, $headers);
    $server is the SMTP server address
    $user is the SMTP server username
    $pass is the SMTP server password
    $from is the address that the email is coming from
    $subject is the subject line
    $message is the message content
    $mail_to is the recipient's address
    $headers is any other headers such as CC or BCC in a string of the form
    Code:
    Bcc: address\n
    Cc: address\n
    X-Mailer: PHP\n

    e.g:
    Code:
    $subj = "Example Mail from an example address";
    $msg = "This is a message\non multiple lines\nThe script will fix the\nraw newlines into \nstandards compatible ones";

    smtpmail("smtp.example.com", "user1", "password", "mail@example.com", $subj, $msg, "recipient@example.org", "Cc: mail2@example.net");

I hope this helps, might be a little complex for those not used to programming languages though! I will try and get round to a ready made script for people just to set the config and upload.

Just reply for any help if needed.

(based on phpBB, (C) 2001, 2005 phpBB Group)


Last edited by edgemaster on Mon Mar 07, 2005 12:47 pm, edited 7 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 1:39 am 
Noobie
Noobie

Joined: Mon Feb 21, 2005 7:02 pm
Posts: 27
The files all packaged nicely available from here (SMTP.zip).


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 9:22 am 
Site Admin
Site Admin

Joined: Fri Feb 11, 2005 7:43 am
Posts: 1818
Location: 100WebSpace's Headquarters
This post has been put in the F.A.Q section.

_________________
Read the F.A.Q. before asking a question!


Top
 Profile  
 
 Post subject: where do I get smtp address?
PostPosted: Mon Mar 07, 2005 3:06 am 
Noobie
Noobie

Joined: Mon Mar 07, 2005 2:35 am
Posts: 3
Location: Bandung- west Java - Indonesia
Can I get a free smtp address?
I need your help

_________________
nancesity77
"it's nice to know that you were there
thanks for asking like you cared
And making me feel like I was the only one "


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 5:30 am 
Experienced
Experienced

Joined: Fri Feb 18, 2005 4:41 pm
Posts: 54
Hum... I'm familiar with part of this code. It's based on phpBB's, isn't it? Nice job nonetheless :wink:
I'll try it out as soon as I can.

@nasse:
Use your ISP's SMTP server (provided they allow you to use it from outside their network through login)


Top
 Profile  
 
 Post subject: Idea
PostPosted: Mon Mar 07, 2005 5:34 am 
Experienced
Experienced

Joined: Fri Feb 18, 2005 4:41 pm
Posts: 54
Perhaps 100 WebSpace could implement this sort of SMTP server selection from the user's control panel. A user could then set a SMTP server, username and password from their profile, who would be used.

It seems a bit of a hassle to set up, but it would benefit users alot! How about it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 7:36 am 
Site Admin
Site Admin

Joined: Fri Feb 11, 2005 7:43 am
Posts: 1818
Location: 100WebSpace's Headquarters
Nice idea, I will forward it to the management:)

_________________
Read the F.A.Q. before asking a question!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 07, 2005 12:56 pm 
Noobie
Noobie

Joined: Mon Feb 21, 2005 7:02 pm
Posts: 27
Modified zip to include copyright information.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ]  Moderators: fhmagic, KJ, Moderators, Support Team

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
100WebSpace © 2011