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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Change email form from PHP to SMTP ?

    Morning,

    I have the following code on my site, when a user fills out a form, I post to a page with the following code. Pretty basic I know, however my host has disabled php mail on all it's servers for goodness knows how long.

    My question is, How do I re-jig this code to work with SMTP instead ? (sorry, not very handy with PHP myself)

    Code:
    <?php
    
    $ip = $_POST['ip']; 
    $httpref = $_POST['httpref']; 
    $httpagent = $_POST['httpagent']; 
    $visitor = $_POST['visitor']; 
    $visitormail = $_POST['visitormail']; 
    $notes = $_POST['notes'];
    $attn = $_POST['attn'];
    
    
    if (eregi('http:', $notes)) {
    die ("Do NOT try that! ! ");
    }
    if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,"."))) 
    {
    echo "<h2>Please click 'BACK' and Enter a valid e-mail</h2>\n"; 
    $badinput = "<h2>Feedback was NOT submitted</h2>\n";
    echo $badinput;
    die ("Go back! ! ");
    }
    
    if(empty($visitor) || empty($visitormail) || empty($notes )) {
    echo "<h2>Please fill in all the required (<FONT COLOR='RED'>*</FONT>) fields</h2>\n";
    die ("Please click 'BACK' on your webbrowser and try again.<BR><CENTER><B>Your message has NOT been sent!</CENTER></B>"); 
    }
    
    $todayis = date("l, F j, Y, g:i a") ;
    
    $attn = $attn ; 
    $subject = $attn; 
    
    $notes = stripcslashes($notes); 
    
    $message = " $todayis [EST] \n
    Attention: $attn \n
    Message: $notes \n 
    From: $visitor ($visitormail)\n
    Additional Info : IP = $ip \n
    Browser Info: $httpagent \n
    Referral : $httpref \n
    ";
    
    $from = "From: $visitormail\r\n";
    
    
    mail("my_email@here.com", $subject, $message, $from);
    
    ?>
    Thanks for any and all help.

    Kosaic

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You got a sendmail path from you're host? If so, use the provided sendmail program by setting it as the fifth parameter to the mail command.
    If not, you'll need to establish a socket connection to you're smtp and fire it commands. To do so, you would start like so:
    PHP Code:
    if (!($smtp fsockopen("smtp.yoursmtpserver.com"25$errno$error)))
    {
        die(
    'Cannot connect to smtp server: ' $error);
    }

    list(
    $visitorUser$visitorDomain) = explode("@"$visitormail);

    // For now, we'll just ignore the server's replies.  For that, look into what the return codes are
    // and use fgets with $smtp to retrieve them.
    fwrite($smtp"HELO anyone\r\n");
    fwrite($smtp"MAIL FROM: <$visitormail>\r\n");
    fwrite($smtp"RCPT TO: <my_email@here.com>\r\n");
    fwrite($smtp"DATA\r\n");
    fwrite($smtp"Received: from $visitorDomain by here.com\r\n");
    fwrite($smtp"Date: " date('r') . "\r\n");
    fwrite($smtp"From: $visitormail\r\n");
    fwrite($smtp"Subject: $subject\r\n");
    fwrite($smtp"To: my_email@here.com\r\n");
    fwrite($smtp$message);
    fwrite($smtp"\r\nQUIT\r\n");

    fclose($smtp); 
    Something like that will replace the mail() call. No idea if it works though, I can't test anything ATM.
    You're $message will require proper endings though, \r\n not just \n. Its somewhat ugly to do this manually; use the sendmail path if the host has an alternate location for sending email.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    137
    Thanks
    1
    Thanked 21 Times in 21 Posts
    In the past, I've just used PHPMailer class for when I had to send mail via SMTP rather use the limited mail function in PHP.

    You could download PHPMailer and look at the example code they give and rewire your PHP form using PHPMailer.

    Just another option for you.

    PHPMailer
    http://phpmailer.worxware.com/


  •  

    Posting Permissions

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