以编程方式更改SMTP服务器

时间:2012-10-30 09:34:35

标签: php email smtp

我们有一个CRM系统需要在管理员更新后更改SMTP服务器。 我们有一个表,它返回一组数据,其中包含状态为1的SMTP设置。问题是它仍然转发到localhost smtp,即使我已经将SMTP设置发送到htmlMimeMail.php(类)这是我的代码:

function sendEmail($txtFrom, $subject, $to, $cc = NULL, $bcc = NULL, $strHTML, $txtModel='')
    {
        $sql  = "SELECT * FROM smtp_server WHERE status='1'";
        $rstemp = $this->cn2->Execute($sql);
        while(!$rstemp->EOF)
        {
            $SMTPid = $rstemp->fields['id'];
            $SMTPServer = $rstemp->fields['server'];
            $SMTPPort = $rstemp->fields['port'];
            $SMTPusername=$rstemp->fields['user'];
            $SMTPpass=$rstemp->fields['pass'];
            $SMTPauth = $rstemp->fields['auth'];
            $rstemp->MoveNext();
        }
        $rstemp->Close();


        $fromEmail  = $txtFrom;
        $from       = $fromEmail;

        $mail = new htmlMimeMail();
        $mail->setTextCharset('utf-8');
        $mail->setHtmlCharset('utf-8');
        $mail->setHeadCharset('utf-8');
        $mail->setSMTPParams($SMTPServer, $SMTPPort,$SMTPid,base64_encode($SMTPusername),base64_encode($SMTPpass),$SMTPServer);         
        $mail->setReturnPath($fromEmail);
        $mail->setFrom($from);
        $mail->setSubject($subject);
        $mail->setHTML($strHTML);

        if(trim($txtModel) != '')
        {
            $file   = strtoupper($txtModel).".pdf";

            if(file_exists($this->dir.$file))
            {
                $attachment = $mail->getFile($this->dir.$file);
                $mail->addAttachment($attachment, $file, "application/pdf");
            }
        }

        if (!is_null($cc) && $cc != '') 
        {
            //$arrEmail = explode(",", $cc);
            $mail->setCc($cc);
            //unset($arrEmail);
        }

        $mail->setBcc($bcc.', sample@email.com');

        $arrEmail = explode(",", $to);      
        ini_set("SMTP",$SMTPServer);
        $result = $mail->send($arrEmail);

        return $result;
    }

我的代码中遗漏了什么? 我还添加了ini_set

这是接收参数的函数。

function setSMTPParams($host = null, $port = null, $auth = null, $user = null, $pass = null,$helo = null)
{
    if (!is_null($host)) $this->smtp_params['host'] = $host;
    if (!is_null($port)) $this->smtp_params['port'] = $port;
    if (!is_null($helo)) $this->smtp_params['helo'] = $helo;
    if($auth == 4){
        if (!is_null($auth)) $this->smtp_params['auth'] = true;
    }else{
        if (!is_null($auth)) $this->smtp_params['auth'] = false;
    }
    if (!is_null($user)) $this->smtp_params['user'] = $user;
    if (!is_null($pass)) $this->smtp_params['pass'] = $pass;
}
Table definition
________________________________________
id|server           |port|user|pass|auth |status
________________________________________
1 |localhost        |25  |null|null|false|0
4 |somesmtp@mail.com|25  |user|pass|true |1
________________________________________

1 个答案:

答案 0 :(得分:0)

从我所看到的,您已将SMTP数据发送到该类,但您忽略了告诉该类使用该数据:

$result = $mail->send( $a_to, 'smtp' )

未能在发送结果中设置第二个参数导致类默认为“邮件”#39;作为发送类型。

function send($recipients, $type = 'mail')

然后导致您的系统使用默认的PHP:

$result = mail($to, $subject, $this->output, implode(CRLF, $headers));

中的:

case 'mail':

函数send()

我不清楚为什么设置采用了启动htmlMimeMail类时设置的SMTP默认值,可能会对代码产生一些进一步的修改,但我不能说。

我意识到这个问题有点老了,当前的htmlMimeMail无论如何都不会在当前的PHP上运行,但是想想也可以给出一个答案。

相关问题