0

我有一个商业系统,可以“从”添加他们的代理向客户发送电子邮件。

添加新代理时,会自动创建 agent@domain.com。所以我希望我的电子邮件来自 CompanyName - Agent 邮件。这就是我尝试处理的方式:

$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";

但是,我收到来自“user”(user@servername) 的电子邮件,虽然reply-to是正确的,如上所列。如果我删除reply-to,它会完美运行,收件箱中的电子邮件将显示为来自 CompanyName,打开时它将是 CompanyName (Agentmail),但是,此类电子邮件不会发送到@hotmail 地址,这是一个巨大的问题。

在您建议我将邮件方法完全切换为类似于 PHPMailer 的方式之前,请考虑我在下面发送电子邮件的方式,它可能不符合您的建议。谢谢!

完整的电子邮件代码:

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);

$body在 mailtemplate.php 中回显,因为它需要一些解析等。Mailtemplate.php 只是 html 电子邮件模板。

谢谢

4

1 回答 1

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
于 2013-09-22T23:27:12.033 回答