0

I have a commercial system that will send emails to customers "from" the agent who added them.

When new agent is added, agent@domain.com is automatically created. So I want my email to be from CompanyName - Agent mail. This is how I tried to handle this:

$headers = "From:".$companyname." ".$Agentmail."\r\n";
$headers =  "Reply-To:".$Agentmail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

However, I get emails from "user"(user@servername) while reply-to is correct, as listed above. If I remove reply-to, it works perfect, e-mails in inbox will be shown as from CompanyName, and when opened, it will be CompanyName (Agentmail), however, such emails don't get sent to @hotmail addresses, which is a huge issue.

Before you suggest that I completely switch my mail method to something along the lines of PHPMailer, please consider the way I use to send e-mails below, it may not be compliant with your suggestion. Thank you!

Full e-mail code:

ob_start();
include("./email/mailtemplate.php");
$message = ob_get_clean();
$body = strtr($content, $replaceWord);
$headers = "From:".$companyname." ".$Agentmail."\r\n";
$headers =  "Reply-To:".$Agentmail."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($email, $subject, $message, $headers);

I echo $body inside mailtemplate.php, as it requires some parsing etc. Mailtemplate.php is simply html email template.

Thanks

Predrag Beocanin
  • 1,402
  • 3
  • 18
  • 25
  • I implemented PHPMailer, going to post solution in a sec. Arrives to any inbox, contains all information. =) – Predrag Beocanin Sep 22 '13 at 22:44
  • Should be `$headers .= "Reply-To:"` (noticed the `.=`) – Petah Sep 22 '13 at 22:52
  • Thanks @Petah, I'm glad to know I remade my emails because I was missing a dot! :D Anyways, it's better to use PHPMailer I guess. – Predrag Beocanin Sep 23 '13 at 12:54
  • I would presonally use Swift mailer over PHPMailer http://swiftmailer.org/ – Petah Sep 23 '13 at 19:57
  • @Petah is there a significant difference? I spent some hours implementing PHPMailer and making sure it works, I'd hate to do it all over again unless there's a huge difference or a security hole? – Predrag Beocanin Sep 23 '13 at 23:59
  • IMHO Swift Mailer is better, and more feature rich, but that is debatable: http://stackoverflow.com/questions/303783/phpmailer-vs-swiftmailer You are using the latest PHPMailer right? https://github.com/Synchro/PHPMailer – Petah Sep 24 '13 at 03:00
  • @Petah yeah, I got the latest. I really need some simple html E-mails sent, using smtp server, sometimes with attachments, and I picked PHPMailer because it was the first one I ran into, had zero issues so far =) I will turn to Swift if I encounter something I can't solve though =) – Predrag Beocanin Sep 24 '13 at 13:13

1 Answers1

0

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
Predrag Beocanin
  • 1,402
  • 3
  • 18
  • 25