【问题标题】:Sending mail through Localhost (in both WAMP and LAMP)通过本地主机发送邮件(在 WAMP 和 LAMP 中)
【发布时间】:2013-08-10 23:59:43
【问题描述】:

我需要使用 PHP 通过 localhost(在 LAMP 和 WAMP 中)发送邮件。我怎样才能做到这一点?我阅读了许多有关此要求的教程,但没有得到任何解决方案。我读到使用 SMTP 我们可以做到这一点,但我将如何获得使用 SMTP 的凭据?希望有人能帮助我做到这一点。

提前谢谢你。

【问题讨论】:

标签: php


【解决方案1】:

在 PHP 中有多种发送邮件的方法。

您可以使用PHP的邮件功能。

http://php.net/manual/en/function.mail.php

<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
mail('caffeinated@example.com', 'My Subject', $message);
?>

SwiftMailer 也值得一看

它具有许多以不同方式(传输类型、附件等)发送邮件的功能,并且易于使用。

http://swiftmailer.org/

http://swiftmailer.org/docs/sending.html

require_once 'lib/swift_required.php';

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

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

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

【讨论】:

  • 我可以提供任何用户名或密码来在 SwiftMailer 中设置凭据吗?
  • 连接到 SMTP 服务器并提供凭据时,它们必须是有效的。
  • 我正在使用 Godaddy 电子邮件服务。有谁知道我需要更改哪些设置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2020-08-03
  • 2013-10-01
  • 2013-08-19
  • 2010-12-19
相关资源
最近更新 更多