PHP SendMail

Use this function as an alternative to build-in PHP sendmail function. You have to replace "your_domain.com" string with the domain name of your website, also make sure you have a valid POP3 account and password configured.

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($toaddress, $subject, $message)
    //SMTP + SERVER DETAILS 
    /* * * * CONFIGURATION START * * * */ 
    $smtpServer = "mail.your_domain.com";
    $port = "25"; 
    $timeout = "30"; 
    $username = "user@your_domain.com";
    $password = "pop3pass";
    $localhost = "mail.your_domain.com";
    $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: $email" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515); 
    $logArray['mailfromresponse'] = "$smtpResponse"; 
         
    //Email To 
    fputs($smtpConnect, "RCPT TO: $toaddress" . $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: <$toaddress>" . $newLine;
    $headers .= "From: $bname <$email>" . $newLine;
     
    fputs($smtpConnect, "To: $toaddress\nFrom: $email\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";     
?>

  • 92 A felhasználók hasznosnak találták ezt
Hasznosnak találta ezt a választ?

Kapcsolódó cikkek

Do you run PHP in CGI or ISAPI mode?

All of our servers are configured to run PHP in ISAPI mode. PHP in ISAPI mode provides a much...

What PHP version do you run?

Our current PHP version is 5.2.5. This is subject to change as we deploy newer services and newer...