1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Sending Email from PHP script

Discussion in 'PHP' started by Alley Cat, Aug 9, 2009.

  1. #1
    Recently I changed server for hosting my php site, but I have found out that I cannot automatically send emails from my contact form, either to myself or to the sender.

    I have done a phpinfo test of my current server and below is a section of the results, these I believe are relevant to me not being able to send emails.

    safe_mode On Off
    safe_mode_exec_dir no value no value
    safe_mode_gid Off Off
    safe_mode_include_dir no value no value
    sendmail_from no value no value
    sendmail_path /usr/sbin/sendmail -t -i /usr/sbin/sendmail -t -i
    serialize_precision 100 100
    short_open_tag On On
    SMTP localhost localhost
    smtp_port 25 25

    Below is the code I am using to try and get the emails generated automatically, but without any joy whatsoever.

    
    <?php
    
    
    
    $subt=$_POST['Send']; 
    
    if ( isset($subt) ) : ?>
    
    
    
    <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    
    <link href="http:/www.my-site.com/style-sheet.css" rel="stylesheet" type="text/css"/>
    
    <title>Contact Form</title>
    
    </head>
    
    <body>
    
    <?php 
    
    $Name=$_POST['name'];
    
    $Email=$_POST['mail'];
    
    $Comments=$_POST['comments'];
    
    include("Mail.php"); 
    
    
    
    $recipients = "me@my-site.com";
    
    
    
    $headers["From"] = $Email;
    
    $headers["To"] = "me@my-site.com";
    
    $headers["Subject"] = "Website Feedback";
    
    
    
    $body = $Comments;
    
    
    
    $params["host"] = "smtp.my-site.com";
    
    $params["port"] = "25";
    
    $params["auth"] = true;
    
    $params["username"] = "me@my-site.com";
    
    $params["password"] = "password";
    
    
    
    // Create the mail object using the Mail::factory method
    
    $mail_object =& Mail::factory("smtp", $params);
    
    
    
    $didit = $mail_object->send($recipients, $headers, $body);
    
    if (!$didit)
    
         {
    
    	echo("Your message has not been received, please try contacting us later");
    
         // any code you want
    
         } else{
    
    echo("Thank you $Name we have received your comments, we will get back to you at $Email about $Comments");
    
         // any code you want
    
         }
    
    ?>
    
    
    
    </body>
    
    </html>
    
    
    
    
    
    
    
    <?php else: ?>
    
    <html>
    
    <head>
    
    <link href="http://www.my-site.com/style-sheet.css" rel="stylesheet" type="text/css"/>
    
    <title>Please tell us your name</title>
    
    </head>
    
    
    
    <div class="centre">
    
    
    
    <form method="POST" action="<?=$PHP_SELF?>">
    
    <body>
    
    
    
    <h1>Contact Us</h1>
    
    
    
    Please enter your name: 
    
    <p>Name: <input name="name" type="text"></p>
    
    <p>Email: <input name="mail" type="text"></p>
    
    <p>Comments: <input name="comments" type="textarea" id="textarea" rows="4" cols="50"></p>
    
    <p><input type="submit" name="Send" value="Submit"> 
    
    <input type="reset" name="Submit2" value="Reset"></p>
    
    </form> 
    
    
    </div>
    
    
    
    </body>
    
    </html>
    
    
    
    <?php
    
    endif;
    
    ?>
    
    Code (markup):
    If anyone can help me I would very much appreciate it.

    I would also like to know if there is a way that I can use the following code, either in conjunction or adapted to meet the requirements in this script. It is a script I found on Codewalkers.
    
    <?php
    //new function
    
    ?>
    
    $to = "post@example.com";
    $nameto = "Who To";
    $from = "post@example.com";
    $namefrom = "Who From";
    $subject = "Hello World Again!";
    $message = "World, Hello!"
    authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
    
    <?php
    /* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */ 
    
    //Authenticate Send - 21st March 2005
    //This will send an email using auth smtp and output a log array
    //logArray - connection, 
    
    function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
    {
    //SMTP + SERVER DETAILS
    /* * * * CONFIGURATION START * * * */
    $smtpServer = "mail.my-domain.com";
    $port = "25";
    $timeout = "30";
    $username = "me@my-domain.com";
    $password = "password";
    $localhost = "localhost";
    $newLine = "\r\n";
    /* * * * CONFIGURATION END * * * * */
    
    //Connect to the host on the specified port
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
    $smtpResponse = fgets($smtpConnect, 515);
    if(empty($smtpConnect)) 
    {
    $output = "Failed to connect: $smtpResponse";
    return $output;
    }
    else
    {
    $logArray['connection'] = "Connected: $smtpResponse";
    }
    
    //Request Auth Login
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authrequest'] = "$smtpResponse";
    
    //Send username
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authusername'] = "$smtpResponse";
    
    //Send password
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authpassword'] = "$smtpResponse";
    
    //Say Hello to SMTP
    fputs($smtpConnect, "HELO $localhost" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['heloresponse'] = "$smtpResponse";
    
    //Email From
    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailfromresponse'] = "$smtpResponse";
    
    //Email To
    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailtoresponse'] = "$smtpResponse";
    
    //The Email
    fputs($smtpConnect, "DATA" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['data1response'] = "$smtpResponse";
    
    //Construct Headers
    $headers = "MIME-Version: 1.0" . $newLine;
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    $headers .= "To: $nameto <$to>" . $newLine;
    $headers .= "From: $namefrom <$from>" . $newLine;
    
    fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['data2response'] = "$smtpResponse";
    
    // Say Bye to SMTP
    fputs($smtpConnect,"QUIT" . $newLine); 
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['quitresponse'] = "$smtpResponse"; 
    }
    ?>
    
    Code (markup):
     
    Last edited: Aug 9, 2009
    Alley Cat, Aug 9, 2009 IP