使用Yahoo SMTP在PHP中发送邮件

时间:2010-08-18 12:26:26

标签: php

如何通过PHP中的 Yahoo!的SMTP服务器发送电子邮件?

4 个答案:

答案 0 :(得分:2)

您应该使用Swift MailerPHPMailer之类的内容。以下示例适用于Swift:

$message = Swift_Message::newInstance()
  ->setSubject('Your subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ->addPart('<q>Here is the message itself</q>', 'text/html')
;

$transport = Swift_SmtpTransport::newInstance('smtp.mail.yahoo.com', 465, 'ssl')
  ->setUsername('your username')
  ->setPassword('your password')
;

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

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

答案 1 :(得分:2)

您可以使用PHP的内置mail()功能发送邮件,但通常非常有限。例如,我认为您不能使用除php.ini文件中指定的SMTP服务器之外的其他SMTP服务器。

相反,你应该看一下Mail PEAR package。例如:

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <recipient@example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
?>

(我从http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm偷走了这个例子:P)

答案 2 :(得分:0)

阅读此http://php.net/manual/en/function.mail.php

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

答案 3 :(得分:0)

PHP mailer将允许您使用任何您喜欢的SMTP服务器,只要您拥有登录的凭据即可。