错误地将邮件发送到发件人邮箱

时间:2013-12-04 11:33:29

标签: php email gmail google-apps swiftmailer

我发送了两封电子邮件。给客户的收据和网店老板的收据。

生成电子邮件并将其保存到包含发件人和收件人电子邮件地址的数据库中。

从数据库中提取消息并循环播放这些消息,然后通过Google Apps(与Gmail相同的系统)通过Swiftmailer发送消息。

但是,收据不仅会发送给客户,还会发送给发件人邮箱。

以下是客户邮件的已修改版本:

Return-Path: <sendermail@domain.com>
Received: from www.domain.com ([xxx.xxx.xxx.xxx])
        by mx.google.com with ESMTPSA id xxxxxxxxxxxxxxxxx.14.2013.12.04.02.45.01
        for <receivermail@someotherdomain.com>
        (version=TLSv1 cipher=RC4-SHA bits=128/128);
        Wed, 04 Dec 2013 02:45:02 -0800 (PST)
Message-ID: <xxxxxxxxxxxxxxxxxxxxxx@www.domain.com>
Date: Wed, 04 Dec 2013 11:45:01 +0100
Subject: Receipt
From: xxx xxx <sendermail@domain.com>
To: xxx xxx <receivermail@someotherdomain.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Some mail text

以下是代码:

// Send mail
public function mailer($to, $from, $subject, $message, array $param = array())
{
    // Initialize swiftmailer
    require_once '/path/to/swiftmailer/lib/swift_required.php';

    $transport = Swift_SmtpTransport::newInstance($param['smtp_server'], $param['smtp_port'], 'ssl')
        ->setUsername($param['smtp_username'])
        ->setPassword($param['smtp_password']);

    $mailer = Swift_Mailer::newInstance($transport);

    // Create a message
    $content = Swift_Message::newInstance($subject)
        ->setFrom($from)
        ->setTo($to)
        ->setBody($message, $param['type']);

    // Send the message
    return $mailer->send($content);
}

// Execute tasks
public function execute_tasks()
{
    $functions = new Functions();
    $db = new Db();

    // Mail parameters
    $param = array
    (
        'smtp_server' => 'smtp.gmail.com',
        'smtp_port' => 465,
        'smtp_username' => 'sendermail@domain.com',
        'smtp_password' => 'xxxxxxx',
    );

    // Get tasks
    $sql = "SELECT * FROM tasks WHERE status = 0";

    // Loop through tasks
    $sql = $db->query($sql);
    while ($row = $db->fetchArray($sql))
    {
        if ($row['type'] == 'mail' AND $row['response'] == '' AND $row['status'] == 0)
        {
            $to = array($row['to_email'] => $row['to_name']);
            $from = array($row['from_email'] => $row['from_name']);

            $param['type'] = $row['mime'];

            $status = $this->mailer($to, $from, $row['subject'], $row['content'], $param);

            if ($status == 1)
            {
                $db->query("DELETE FROM tasks WHERE task_id = ".$row['task_id']);
            }
            else
            {
                $db->query("UPDATE tasks SET status = 1 WHERE task_id = ".$row['task_id']);
            }
        }
    }
}

0 个答案:

没有答案