Okay, as promised, here's a way to avoid the errors by using PHPMailer. If you didn't know if PHPMAiler is for you, trust me I had the same issue, but it's extraordinary easy to use.
Here's the code
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = true;
$mail->Username = $Agentmail;
$mail->Password = $smptpass;
$mail->From = $Agentmail;
$mail->FromName = $companyname;
$mail->addAddress($email, $CustomerFullName);
$mail->addReplyTo($Agentmail, $fullname);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->altBody = $altBody;
if(!$mail->send()) {
header("location:errorpage.php");
}
Okay, so these are all variables. And it works perfect. One thing, if you are making a commercial system and you are worried about storing passwords in the database in plain text, as you should be, you don't have too! Encrypt them when you store them and decrypt them before using them for PHP Mailer.
For the code above, I first decrypt the password:
$cipher = new Cipher('encrypt');
$smptpass = $cipher->decrypt($cipheredpass);
For this and PHPMailer to work, you need two files:
require_once "./PHPMailer/class.phpmailer.php";
require_once "functions.php";
PHPMailer is stored in folder with the same name for me, so change your path to wherever you put it. As for functions.php, here's how to handle the encrypt/decrypt:
<?php
class Cipher {
private $securekey;
function __construct($textkey) {
$this->securekey = md5($_SERVER['SERVER_ADDR']);
}
function encrypt($input) {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->securekey, $input, MCRYPT_MODE_ECB));
}
function decrypt($input) {
return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->securekey, base64_decode($input), MCRYPT_MODE_ECB));
}
}
?>
As you can see, I'm using $_SERVER
to pickup an environment variable, as system will be hosted on various machines, so I need something that always exists. After that, I md5()
it, just for extra security. You can put anything you want as your key, it really doesn't matter, I'm trying to avoid having the same key on multiple systems. You don't need to store the key anywhere. If you have functions.php file like this, here's how to use it further:
$cipher = new Cipher('encrypt');
$variable = $cipher->encrypt($input); // For encrypting
$cipher = new Cipher('decrypt');
$variable = $cipher->decrypt($input); // For decrypting