Jump to content

Please Help!! Change mail() to smtp. I?m lost!!!


Recommended Posts

Hi There This is my first post so would like to say Hello to all.

 

I am trying to change the script below that uses the mail() function to use smtp authentication. Tried lots of different things but cant get it right. I am sure it will be a piece of cake for one of you guys  :D

 

<?php

class Email

{

 

var $emailRegExp = '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i';

 

var $eol = "\r\n";

 

var $contentType = "text/plain";

 

var $charset = "utf-8";

 

function Email()

{

 

}

 

function send($to, $subject, $message, $sender)

{

if (!preg_match($this->emailRegExp, $to))

{

return false;

}

 

if (!preg_match($this->emailRegExp, $sender))

{

return false;

}

 

$headers  = "MIME-Version: 1.0" . $this->eol;

$headers .= "Content-type: ".$this->contentType."; charset=" . $this->charset . $this->eol;

$headers .= "From: $sender" . $this->eol;

$headers .= "Reply-To: $sender" . $this->eol;

 

 

$subject = '=?UTF-8?B?'.base64_encode($subject).'?=';

return @mail($to, $subject, $message, $headers);

}

 

function setCharset($charset)

{

$this->charset = $charset;

}

 

function setContentType($contentType)

{

if (!in_array($contentType, array('text/plain', 'text/html')))

{

return false;

}

$this->contentType = $contentType;

}

 

function setEol($eol)

{

$this->eol = $eol;

}

}

?>

 

Any Help appreciated

 

Thanks

 

 

Link to comment
https://forums.phpfreaks.com/topic/268010-please-help-change-mail-to-smtp-im-lost/
Share on other sites

working smtp email sending script (it's old , just for example)

<?
#configured for mail.ru
$config['smtp_username'] = '********@mail.ru'; //ur email adress
$config['smtp_port'] = '25'; //dont change it if u dunno
$config['smtp_host'] = 'smtp.mail.ru'; //server adress
$config['smtp_password'] = '*************'; 
$config['smtp_charset'] = 'UTF-8'; 
$config['smtp_from'] = 'TEST'; //"from:"


function smtpmail($mail_to, $subject, $message, $headers='') {
        global $config;
        $SEND =   "Date: ".date("D, d M Y H:i:s") . " UT\r\n";
        $SEND .=   'Subject: =?'.$config['smtp_charset'].'?B?'.base64_encode($subject)."=?=\r\n";
        if ($headers) $SEND .= $headers."\r\n\r\n";
        else
        {
                $SEND .= "Reply-To: ".$config['smtp_username']."\r\n";
                $SEND .= "MIME-Version: 1.0\r\n";
                $SEND .= "Content-Type: text/plain; charset=\"".$config['smtp_charset']."\"\r\n";
                $SEND .= "Content-Transfer-Encoding: 8bit\r\n";
                $SEND .= "From: \"".$config['smtp_from']."\" <".$config['smtp_username'].">\r\n";
                $SEND .= "To: $mail_to <$mail_to>\r\n";
                $SEND .= "X-Priority: 3\r\n\r\n";
        }
        $SEND .=  $message."\r\n";
         if( !$socket = fsockopen($config['smtp_host'], $config['smtp_port'], $errno, $errstr, 30) ) {
              return false;
         }

            if (!server_parse($socket, "220", __LINE__)) return false;

            fputs($socket, "HELO " . $config['smtp_host'] . "\r\n");
            if (!server_parse($socket, "250", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, "AUTH LOGIN\r\n");
            if (!server_parse($socket, "334", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, base64_encode($config['smtp_username']) . "\r\n");
            if (!server_parse($socket, "334", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, base64_encode($config['smtp_password']) . "\r\n");
            if (!server_parse($socket, "235", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, "MAIL FROM: <".$config['smtp_username'].">\r\n");
            if (!server_parse($socket, "250", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, "RCPT TO: <" . $mail_to . ">\r\n");

            if (!server_parse($socket, "250", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, "DATA\r\n");

            if (!server_parse($socket, "354", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, $SEND."\r\n.\r\n");

            if (!server_parse($socket, "250", __LINE__)) {
               fclose($socket);
               return false;
            }
            fputs($socket, "QUIT\r\n");
            fclose($socket);
            return TRUE;
}

function server_parse($socket, $response, $line = __LINE__) {
        global $config;
    while (substr($server_response, 3, 1) != ' ') {
        if (!($server_response = fgets($socket, 256))) {
                  return false;
                }
    }
    if (!(substr($server_response, 0, 3) == $response)) {
                  return false;
        }
    return true;
}

Thank you a reply. I have been trying to change the script to use smtp and cannot understand what would be wrong with my script? any more  suggestions would be welcome as I am turning grey!!

 

<?php
class Email
{
/**
* E-Mail regular expression
*
* @var string
* @access private
*/
var $emailRegExp = '/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i';
/**
* End of line
*
* @var string
* @access private
*/
var $eol = "\r\n";
/**
* Content type
*
* @var string
* @access private
*/
var $contentType = "text/plain";
/**
* Character set
*
* @var string
* @access private
*/
var $charset = "utf-8";
/**
* Constructor
*/
function Email()
{
	//constructor
}
/**
* Send mail
*
* @param string $to Receiver of the mail
* @param string $subject Subject of the email to be sent
* @param string $message Message to be sent
* @param string $sender Sender of the mail
* @access public
* @return bool Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise
*/
function smtpmail($to, $subject, $message, $sender)
{
        global $mail;
	if (!preg_match($this->emailRegExp, $to))
	{
		return false;
	}

	if (!preg_match($this->emailRegExp, $sender))
	{
		return false;
	}


	$headers  = "MIME-Version: 1.0" . $this->eol;
	$headers .= "Content-type: ".$this->contentType."; charset=" . $this->charset . $this->eol;
	$headers .= "From: $sender" . $this->eol;
	$headers .= "Reply-To: $sender" . $this->eol;
	/*
	$headers .= "Return-Path: $sender" . $this->eol;
	$headers .= "X-Mailer: PHP/" . phpversion() . $this->eol;
	$headers .= "Message-Id:" . md5(time()) . $this->eol;
	$headers .= "X-Originating-IP:" . $_SERVER['REMOTE_ADDR'];
	*/

	 $host = "mail.mydomain.com";
         $port = "26";
         $username = "*************";
         $password = "**********";

	 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'port' => $port,
     'auth' => true,
     'username' => $username,
     'password' => $password)) ;

  $mail = $smtp->send($to, $headers, $body);

	$subject = '=?UTF-8?B?'.base64_encode($subject).'?=';
	return @mail($to, $subject, $message, $headers);
}
/**
* Set character set
*
* @param string $charset Character set
* @access public
*/
function setCharset($charset)
{
	$this->charset = $charset;
}
/**
* Set content type
*
* @param string $contentType Content type
* @access public
* @return bool|void
*/
function setContentType($contentType)
{
	if (!in_array($contentType, array('text/plain', 'text/html')))
	{
		return false;
	}
	$this->contentType = $contentType;
}
/**
* Set end of line
*
* @param string $eol End of line (\r\n, \n...)
* @access public
*/
function setEol($eol)
{
	$this->eol = $eol;
}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.