• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

need smtp email sending php script

mattie005

New Member
Hi,

I am looking for a script that will connect to smtp and allow me to send an email.

I have looked on google a few times but cant find anything that shows me how to connect, send to a smtp server. I am only sending, not receiving emails as well.

Thanks. :)
 
why do you need it to connect to your smtp? dont you just want to email from the site? if so, all you need to do is properly set the headers for the email in the php
 
PHP:
<?php
class smtp
{
	var $socket ;
	var $errno ;
	var $errstr ;
	
	function smtp( $hostname, $port = 25 )
	{
		if( !( $this->socket = fsockopen( $hostname, $port, $this->errno, $this->errstr, 5 ) ) )
		{
			die( sprintf( "smtp::%s( %s, %s ) failed, cannot connect to %s:%s\r\n", __METHOD__, $hostname, $port, $hostname, $port ) );	
		}		
	}
	function _mail( $from, $to, $subject, $message, $headers = null )
	{
		$message = sprintf( 
			"HELO %s\r\n".
			"MAIL FROM:<%s>\r\n".
			"RCPT TO:<%s>\r\n".
			"DATA\r\n".
			"%s".
			"Received: from %s by %s ; %s\r\n".
			"Date: %s\r\n".
			"From: %s <%s>\r\n".
			"Subject: %s\r\n".
			"\r\n".
			"%s\r\n".
			".\r\n".
			"QUIT\r\n", 
			$to, 
			$from, 
			$to, 
			is_array($headers) ? implode("\r\n", $headers ) . "\r\n" : "", 
			preg_replace("~^.*?@~", '', $from ), 
			preg_replace("~^.*?@~", '', $to ), 
			date('r'), 
			date('r'),
			$from, 
			$from,
			$subject, 
			$message 
		);
		if( fwrite( $this->socket, $message ) )
		{
			if( substr( fgets( $this->socket ), 0, 3 ) >= 200 )
			{
				return true ;
			}	
		}	
	}
}
$smtp = new smtp( "hozter.info" );
if( $smtp->_mail( "testing@krakjoe.info", "krakjoe@krakjoe.info", "Subject", "message" ) )
{
	print("Mail sent\r\n");
}

?>

got bored .... is possible, use above to make something useful .... your server might require authentication, search around for the rfc's for smtp protocol ..... or post again and I'll do it if I have too ......
 
got bored .... is possible, use above to make something useful .... your server might require authentication, search around for the rfc's for smtp protocol ..... or post again and I'll do it if I have too ......


I tried it but I dont get the email. Please could you possible do the rfc thing for smtp protocol (dont know what it is!) and allow it to use headers as well?

thanks
 
here, this should work for headers and everything. make a file and name it "contact.php"

PHP:
<?php

$break	= "<br/>";

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

	$errors = array(); // Initialize error array.
	
	// Check for a name
	if (empty($_POST['name'])) {
		$errors[] = 'You forgot to enter your name.';
	} else {
		$n = $_POST['name'];
	}
	
	// Check for a valid email address
	if (!preg_match("/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/", $_POST['email'])) {
		$errors[] = 'You need to enter a valid email address.';
	} else {
		$e = $_POST['email'];
	}

	// Check for a phone number
	if (empty($_POST['phone'])) {
		$p = 'NA';
	} else {
		$p = $_POST['phone'];
	}
	
	// Check for a message
	if (empty($_POST['msg'])) {
		$errors[] = 'You forgot to enter a message.';
	} else {
		$m = stripslashes($_POST['msg']);
	}

	if (empty($errors)) { // If everything is OK.

		$mailTo			 = "***PUT THE EMAIL ADDRESS YOU WANT TO RECIEVE THE EMAIL AT HERE***";
		$senderName		 = "***PUT YOUR NAME HERE***";
		$senderMail		 = "***PUT YOUR EMAIL ADDRESS HERE***";
		$eol			.= "\r\n";
		$sol			.= "\n";
		$headers		.= 'To: '.$mailTo.' <'.$mailTo.'>'.$eol;
		$headers		.= 'From: '.$senderName.' <'.$senderMail.'>'.$eol;
		$headers		.= 'Date: '.date("r").$eol;
		$headers		.= 'Sender-IP: '.$_SERVER["REMOTE_ADDR"].$eol;
		$headers		.= 'X-Mailser: MCT Adv.PHP Mailer 1.0'.$eol;
		$headers		.= 'MIME-Version: 1.0'.$eol;
		//$headers		.= 'Content-Type: text/html; charset="windows-1251"\r\n';
		$headers		.= 'Content-Type: text/html; charset="iso-8859-1"'.$eol;
		$subject		 = '***PUT YOUR SUBJECT HERE***';
		
		//foreach ($row2 as $key => $value)
		//	$msg .= $key.' - '.$value."\n";
		$emailMsg		.= '<font face=arial size=2>';
		$emailMsg		.= 'You have recieved a new e-mail from ***YOUR SITE NAME HERE***.'.$break.$break;
		$emailMsg		.= '<strong>Name:</strong>  '.$n.$break;
		$emailMsg		.= '<strong>E-Mail Address:</strong>  '.$e.$break;
		$emailMsg		.= '<strong>Phone Number:</strong>  '.$p.$break;
		$emailMsg		.= '<strong>Message:</strong>  '.$break.$m.$break;
		$emailMsg		.= $break;
		$emailMsg		.= '-----------------------------------------------'.$break;
		$emailMsg		.= 'MCT Adv.PHP Mailer 1.0 - by: Justin St. Germain';
		$emailMsg		.= '</font>';
		
		// Mail it
			mail($mailTo, $subject, $emailMsg, $headers);

?>

		<form action="contact.php" method="post">
			Name: &nbsp; *<br>
			<input name="name" type="text" value="<? echo $_POST['name']; ?>" tabindex="1" size="50" />
			<br>
			E-Mail Address: &nbsp; *<br>
			<input name="email" type="text" id="email" value="<? echo $_POST['email']; ?>" tabindex="2" size="50" />
			<br>
			Phone Number:<br>
			<input name="phone" type="text" id="phone" value="<? echo $_POST['phone']; ?>" tabindex="3" size="50" />
			<br>
			Message: &nbsp; *<br>
			<textarea name="msg" cols="38" rows="8" id="msg" tabindex="4" /><? echo "$msg"; ?></textarea>
			<br><br>
			<input name="submit" type="submit" value="Submit" tabindex="5" /><input type="hidden" name="submitted" value="TRUE" /> &nbsp; <input name="reset" type="reset" value="Reset" tabindex="6" /> &nbsp; <em>* = required</em>
		</form>	

<?php
} //Close the main IF-ELSE.
?>
 
Last edited:
You need to change the values to things that make sense for your server, and @mct : I think he wants something that utilizes smtp in particular without using mail( ) he's experienced enough to know about mail( ) by himself ......
 
no problem. if you can use one that is not smtp, the code for the one i posted above works really well.
 
Back
Top