Jump to content

How to Send Email from a PHP Script Using SMTP Authentication


deziners.studio

Recommended Posts

Hello All!

 

I am not a professional php coder and even don't have great experience in php. I was coding a feedback form on my site using php and its working on one of my server but not working on another server. When I asked about it to the hosting company they told me to use SMTP authentication it. I don't know how to use SMTP authentication in my coding. So please can anybody help me in this... You can check my code here :

 

<?php

/* This form is designed and developed by Deziners Studio*/

 

$c_name = $_POST['c_name'];

$c_designation = $_POST['c_designation'];

$c_address = $_POST['c_address'];

$c_phone = $_POST['c_phone'];

$c_email = $_POST['c_email'];

$c_requirement = $_POST['c_requirement'];

 

$todayis = date("l, F j, Y, g:i a") ;

 

// Creating header for sending email to Site Administrator

$header = 'From:' . $c_email . "\r\n";

 

// Creating subject for sending email to Site Administrator

$subject = "$c_email has submitted contact request form on your site...";

 

// Putting Site Administrator's email id for sending email

$send_to = "[email protected]";

 

$message = "Dear Administrator,\n\n

You received career request through contact request form on $todayis [EST] from $c_email ...\n

The Details are as below:\n\n

<strong>$todayis [EST]</strong> \n

<strong>Name :</strong> $c_name \n

<strong>Designation :</strong> $c_designation \n

<strong>Address :</strong> $c_address \n

<strong>Phone No. :</strong> $c_phone \n

<strong>Email :</strong> $c_email \n

<strong>Requirement :</strong> $c_Requirement \n

";

 

// Send message to Site Administrator

$success = mail ($send_to, $subject, $message, $header);

 

if ($success)

{

//Creating confirmation message to send to the user

$user_email = $c_email;

$from_email = "[email protected]";

$receipt_subject = "Confirmation of your career request...";

$receipt_header = 'From: ' . $from_email . "\r\n";

$receipt_message = "Dear $c_name, \n\n

Your request for contact on $todayis [EST] has been received and forwarded to our

concerned department. We will get back to you within 24 hours. \n

Thanking you. \n\n

Best Regards, \n

Deziners Studio \n

www.dezinersstudio.com";

 

// Send the confirmation mail to the user

mail ($user_email, $receipt_subject, $receipt_message, $receipt_header);

 

header('Location: success_contact.html');

}

else

{

header('Location: contact.html');

}

?>

 

maybe this could help?

 

<?php 
//new function 

$to = "[email protected]"; 
$nameto = "Who To"; 
$from = "[email protected]"; 
$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.server.com"; 
    $port = "25"; 
    $timeout = "30"; 
    $username = "smtpusername"; 
    $password = "smtppassword"; 
    $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";     
} 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.