【问题标题】:Sending email from localhost in php in windows在windows中的php中从localhost发送电子邮件
【发布时间】:2012-04-06 13:54:36
【问题描述】:

我正在使用这个非常小的代码来测试电子邮件是否到达电子邮件目的地:

<?php
  mail('woodsy013@hotmail.com','Test mail','The mail function is working!');
  echo 'Mail sent!';
?>

但它似乎没有工作。我正在使用 WAMP。我已经安装了一个免费的 SMTP 服务器。而我的 php.ini 文件配置如下:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.tools.sky.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

在我提到的操作之后,我似乎没有收到发送至 woodsy130@hotmail.com 的电子邮件。

我收到此错误:

Warning: mail() [function.mail]: SMTP server response: 530 5.7.0 
Must issue a STARTTLS command first. ff2sm10904265wib.9 in 
C:\wamp\www\Derrysgc2\pages\pages\mailtest.php on line 2

有什么建议吗?

【问题讨论】:

  • 听起来您有一个 SMTP 服务器,它需要安全连接。我不确定PHP是否可以做到这一点。尝试本地 SMTP。

标签: php email smtp localhost


【解决方案1】:

尝试通过 gmail 使用 SMTP 服务器。

ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");

否则有很多 PHP 邮件程序库。这为您简化了事情并使其更易于使用。我最喜欢的是 swift mailer。最好的部分是你不必弄乱你的核心 php 配置文件,而且文档也很容易阅读。

例如,如果你想使用 PHP 的 swift mailer 库发送邮件,它就这么简单。

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password');

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself');

// Send the message
$result = $mailer->send($message);

更多信息可以参考官网http://swiftmailer.org/docs的文档

【讨论】:

    【解决方案2】:

    SMTP 不应该指向您的 SMTP 服务器吗? (我假设 smtp.tools.sky.com 是您的提供商)。 sendmail_from 也应该是正确的电子邮件地址。

    另请注意,一些邮件提供商会阻止从动态 IP 地址发送的电子邮件。

    【讨论】:

    • 所以我需要配置我已经安装在本地机器上的 SMTP 服务器?
    • 而且大多数时候您的 ISP 会阻止默认 SMTP 端口,即端口 25,您可以尝试将端口更改为 465
    • 如果您希望使用自己的 SMTP 服务器,那么您应该将 php.ini 指向您的本地服务器,然后将该服务器中继到您的提供商。
    猜你喜欢
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2017-06-19
    • 2012-07-03
    • 2012-12-03
    • 2018-11-27
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多