【问题标题】:How can I use fake sendmail with SwiftMailer on Windows?如何在 Windows 上使用带有 SwiftMailer 的虚假 sendmail?
【发布时间】:2013-07-03 06:31:39
【问题描述】:

我希望在 Windows 上通过外部 SMTP 服务器使用 PHP 发送电子邮件。为此,我必须使用SwiftMailer。

我可以使用 PHP 的本机 mail() 函数和 Fake Sendmail for Windows 通过外部 SMTP 服务器发送电子邮件,但我无法让 SwiftMailer 正常工作。

使用 PHP 的 mail() 函数进行工作设置

这是我使用本机 mail() 函数成功发送邮件的 PHP 代码。

$returnValue = mail
(
   $params['to'     ],
   $params['subject'],
   $params['body'   ],
   "From: Sender McSender <{$params['from']}>"
);

echo "mail() returned "; var_dump($returnValue);

这是我的 php.ini 文件的相关部分。

[mail function]
sendmail_path = "C:\usr\lib\sendmail.exe"

Fake Sendmail for Windows 有自己的 sendmail.ini 文件,我在其中指定了我的 smtp_server、smtp_port、auth_username 和 auth_password。

当我通过在 Web 浏览器中加载页面来运行 PHP 代码时,输​​出表明 mail() 函数返回 true,并且电子邮件按预期到达了预期的目的地。

尝试使用 SwiftMailer 进行设置(不工作)

尝试使用SwiftMailer 实现相同的结果,我按照SwiftMailer documentation 中的说明将上面的PHP 代码替换为以下代码。

$transport = Swift_SendmailTransport::newInstance('/usr/lib/sendmail -t');
$mailer    = Swift_Mailer::newInstance($transport);
$message   = Swift_Message::newInstance()
->setSubject($params['subject'])
->setFrom($params['from'])
->setTo($params['to'])
->setBody($params['body']);

$nSent = $mailer->send($message);

echo "Number of emails sent: $nSent\n";

当我尝试通过在我的网络浏览器中加载一个页面来运行上面的 PHP 代码时,该页面将永远加载。我尽可能地跟踪了 SwiftMailer 库中的 PHP 代码,发现在下面的“if ($err = stream_get_contents($pipes[2])) {”行之后我看不到 echo 语句的输出。如果我剪切并粘贴'echo "Okay so far." 和 'die;'在“if”条件之后的行,无论是在“if”之后的花括号内部还是外部,页面都会一直加载。

/**
 * Opens a process for input/output.
 */
private function _establishProcessConnection()
{
echo "In StreamBuffer->_establishProcessConnection()\n";
    $command = $this->_params['command'];
    $descriptorSpec = array(
        0 => array('pipe', 'r'),
        1 => array('pipe', 'w'),
        2 => array('pipe', 'w')
        );
    $this->_stream = proc_open($command, $descriptorSpec, $pipes);
    stream_set_blocking($pipes[2], 0);
echo "Okay so far.\n";
die;
    if ($err = stream_get_contents($pipes[2])) {
        throw new Swift_TransportException(
            'Process could not be started [' . $err . ']'
            );
    }
    $this->_in =& $pipes[0];
    $this->_out =& $pipes[1];
}

我已经尝试过的事情

  • 我尝试在传递给 Swift_SendmailTransport::newInstance() 的 /usr/lib/sendmail 命令中使用 -bs 而不是 -t。我也尝试过不传递任何参数(即既不传递-bs 也不传递-t)。
  • 我尝试过传递“from”参数的方式与调用上述 PHP 的本机 mail() 函数的方式相同。当我这样做时,SwiftMailer 抱怨说我的“发件人”地址“不符合 RFC 2822”。
  • 我已经尝试过(感谢 Michael Sivolobov 下面的建议),将 '/usr/lib/sendmail -t' 替换为 'C:\usr\lib\sendmail -t' 和 'C:/usr/lib/发送邮件-t'。在这两种情况下,页面只会永远加载。
  • 我尝试不向 Swift_SendmailTransport::newInstance() 函数传递任何参数,希望 Swift 能够从我的 php.ini 文件中找出我的 sendmail.exe 的路径。这样做会导致以下错误“Uncaught exception 'Swift_TransportException' with message 'Process could not be started [The system cannot find the path specified.]” 上面复制的代码部分引发了异常。传递 '-t' 和 '-bs' 具有相同的效果。

我完全没有想法。任何人都可以就如何使 SwiftMailer 代码正常工作提供任何有用的建议,我们将不胜感激。

【问题讨论】:

  • 在受挫之后,我决定放弃 SwiftMailer 并安装 PHPMailer。我设置 PHPMailer 没有问题。

标签: php windows smtp swiftmailer sendmail.exe


【解决方案1】:

您需要使用正确的交通工具:

$transport = Swift_SendmailTransport::newInstance('C:\usr\lib\sendmail.exe');

更新 使用这一行:

$transport = Swift_MailTransport::newInstance();

【讨论】:

  • 感谢迈克尔的建议,但没有帮助。我已经更新了“我尝试过的事情”部分以包含您的建议。
  • @Tom,尝试使用$transport = Swift_MailTransport::newInstance();
  • 再次感谢,好主意,但这并没有解决问题。我又更新了我的问题。
  • 这与传输构造函数没有参数无关。这是关于另一个传输类:Swift_MailTransport 而不是Swift_SendmailTransport
  • 哦,对了。希望这会解决其他人的问题。我决定放弃 SwiftMailer 并安装 PHPMailer。我在设置 PHPMailer 时没有问题。感谢您的所有建议。
【解决方案2】:

假设您已将fake sendmail 安装到本地目录中,例如:

C:\wamp64\bin\sendmail\sendmail.exe

像这样编辑您的php.ini 文件:

sendmail_path = "C:\wamp64\bin\sendmail\sendmail.exe -t"

像这样编辑您的sendmail.ini 文件:

smtp_server=localhost
smtp_port=25
smtp_ssl=none

然后像这样使用swiftmailer SMTP 传输:

$transport = new Swift_SmtpTransport('localhost', 25);
  • Swift_SendmailTransport 仅适用于 (Linux/UNIX) 可执行文件。

对于 Laravel 用户

过程相同,但您还需要像这样编辑您的 .env 文件:

MAIL_DRIVER=smtp
MAIL_HOST=localhost
MAIL_PORT=25
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=
MAIL_FROM_ADDRESS=myemail@example.com
MAIL_FROM_NAME="My Name"

【讨论】:

  • 最后.. 谢谢!你拯救了我的一天!
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
  • 2021-07-20
相关资源
最近更新 更多