Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    php mail to external server?

    hello,
    I'm wondering, is there a way to send php email to an external smtp server, and if so how??

    thanks in advance for your time!
    -Ken

  • #2
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you dont need a external smtp server to send mail with php use this
    PHP Code:
            <?

          $ToEmail 
    "youremail";

           

          
    $ToSubject "testing mail";

           

          
    $EmailBody "hi! It worked!!";
      
           
      
          
    mail($ToName." <".$ToEmail.">",$ToSubject$EmailBody"From: ".$FirstName." <".$Email.">");
       
           
     
          
    ?>

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, i know all about the mail function, but thats not what I'm looking for.
    I want to use an external smtp server.

    thanks,
    -Ken

  • #4
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this
    PHP Code:
    <?php

    $from 
    " Sender <sender@example.com>";
    $to "Recipient <recipient@example.com>";
    $subject "Hi!";
    $body "Hi,\n\nHow are you?";

    $host "mail.example.com";
    $username "smtp_username";
    $password "smtp_password";

    $headers = array ('From' => $from,
      
    'To' => $to,
      
    'Subject' => $subject);
    $smtp Mail::factory('smtp',
      array (
    'host' => $host,
        
    'auth' => true,
        
    'username' => $username,
        
    'password' => $password));

    $mail $smtp->send($to$headers$body);

    if (
    PEAR::isError($mail)) {
      echo(
    "<p>" $mail->getMessage() . "</p>");
     } else {
      echo(
    "<p>Message successfully sent!</p>");
     }
    ?>

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    kool
    I also found this function on php.net:

    PHP Code:
    // modified to provide authenticated logins
    function mymail($to,$subject,$message,$headers)
    {

      
    // set as global variable
      
    global $GLOBAL;
      
      
    // get From address
      
    if ( preg_match("/From:.*?[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.*/"$headers$froms) ) {
         
    preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/"$froms[0], $fromarr);
         
    $from $fromarr[0];
      }

      
    // Open an SMTP connection
      
    $cp fsockopen ($GLOBAL["SMTP_SERVER"], $GLOBAL["SMTP_PORT"], &$errno, &$errstr1);
      if (!
    $cp)
       return 
    "Failed to even make a connection";
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "220") return "Failed to connect";

      
    // Say hello...
      
    fputs($cp"HELO ".$GLOBAL["SMTP_SERVER"]."\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "Failed to Introduce";
      
      
    // perform authentication
      
    fputs($cp"auth login\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "334") return "Failed to Initiate Authentication";
      
      
    fputs($cpbase64_encode($GLOBAL["SMTP_USERNAME"])."\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "334") return "Failed to Provide Username for Authentication";
      
      
    fputs($cpbase64_encode($GLOBAL["SMTP_PASSWORD"])."\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "235") return "Failed to Authenticate";

      
    // Mail from...
      
    fputs($cp"MAIL FROM: <$from>\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "MAIL FROM failed";

      
    // Rcpt to...
      
    fputs($cp"RCPT TO: <$to>\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "RCPT TO failed";

      
    // Data...
      
    fputs($cp"DATA\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "354") return "DATA failed";

      
    // Send To:, From:, Subject:, other headers, blank line, message, and finish
      // with a period on its own line (for end of message)
      
    fputs($cp"To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "250") return "Message Body Failed";

      
    // ...And time to quit...
      
    fputs($cp,"QUIT\r\n");
      
    $res=fgets($cp,256);
      if(
    substr($res,0,3) != "221") return "QUIT failed";

      return 
    true;


  • #6
    Regular Coder googleit's Avatar
    Join Date
    Jul 2006
    Location
    birmingham, uk
    Posts
    189
    Thanks
    0
    Thanked 0 Times in 0 Posts
    let me know if my code works

  • #7
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    your code *looks* like it should work, though I'm using the function that I posted it has a little more functionality for my needs.

    cheers,
    -Ken

  • #8
    Regular Coder
    Join Date
    Dec 2004
    Location
    Manchester, UK
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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