I am trying to create a simple PHP Script which is used by a client to send advertising material to customers.
Currently if I send a test email it sends the emails perfectly but it shows the user the email thats being sent after its been sent, which is obviously not a wanted feature, I was wondering whether anyone knows why this might be happening (I will supply the code underneath this, I've replaced the email details with example data for this post)
Thanks for any help in advance
Satal 😃
email.php
<?php
require_once('Classes/clsEmail.php');
define("FROM_NAME","from name");
define("FROM_EMAIL","valid@email.com");
define("SUBJECT","Email Subject");
$errMsg = "";
if (isset($_GET['s']))
{
if (isset($_POST['emails']))
{
$arry_emails = split("\n", $_POST['emails']);
if (count($arry_emails) > 0)
{
$em = new clsEmail();
$em->setFromEmail(FROM_EMAIL);
$em->setFromName(FROM_NAME);
$em->setSubject(SUBJECT);
$email = getEmail();
$em->setMessage($email);
$em->setHTML(true);
$count = 0;
for($i = 0; $i <= count($arry_emails); $i++)
{
$count++;
$em->addTo(trim($arry_emails[$i]));
}
if ($count == 0)
{
$errMsg = "You must supply at least one email address";
}
else
{
$rtn = $em->sendMail();
if ($rtn == false)
{
$errMsg = "Sending the email failed";
}
else
{
$errMsg = "Sending was successful";
}
}
}
else
{
$errMsg = "You must supply at least one email address";
}
}
}
echo "<html><body>";
if($errMsg != "")
{
echo "<span style='color:red;'>".$errMsg."</span><br />";
}
echoForm();
echo "</body></html>";
function echoForm()
{
echo "<form action='email.php?s=1' method='post'>";
echo "Enter the email addresses to send to one per line in the text box below<br />";
echo "<textarea name='emails'></textarea><br />";
echo "<input type='submit' value='Send Emails' />";
echo "</form>";
}
function getEmail()
{
$myFile = "emailhtml.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
return $theData;
}
?>
clsEmail.php
<?php
/**
* clsEmail
*
* @package
* @author Satal Keto
* @copyright 2008
* @version v1.00.003
* @access public
*/
class clsEmail
{
protected $to = Array();
protected $fromName = "";
protected $fromEmail = "";
protected $subject = "";
protected $message = "";
protected $isHTML = false;
public function setFromName($fromName)
{
$this->fromName = $fromName;
}
public function setFromEmail($fromEmail)
{
$this->fromEmail = $fromEmail;
}
public function setSubject($subject)
{
$this->subject = $subject;
}
public function setMessage($message)
{
$this->message = $message;
}
public function addTo($email)
{
array_push($this->to,$email);
}
public function setHTML($isHTML)
{
$this->isHTML = $isHTML;
}
public function sendMail()
{
if (count($this->to) > 0)
{
$i = 0;
$to = "";
while ($i < count($this->to))
{
if ($to != "" && $i != (count($this->to) - 1))
{
$to .= ", ";
}
$to .= $this->to[$i];
$i++;
}
$subject = $this->subject;
$message = $this->message;
$header = "";
if ($this->isHTML)
{
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html; charset=iso-8859-1\n";
}
$header .= "From: " . $this->fromName . " <" . $this->fromEmail . ">\n";
$header .= "X-Mailer: PHP/" . phpversion();
echo "To: " . $to . "<br />Subject: " . $subject . "<br />Message: ";
echo $message . "<br />Headers: " . htmlentities($header);
$mailsent = mail($to,$subject,$message,$header);
if ($mailsent)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
}
?>