Sending mails in PHP using Yahoo SMTP

前端 未结 4 1044
走了就别回头了
走了就别回头了 2021-01-13 15:46

How can I send an email via Yahoo!\'s SMTP servers in PHP?

相关标签:
4条回答
  • 2021-01-13 16:04

    You should use something like Swift Mailer or PHPMailer. The following example is for 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);
    
    0 讨论(0)
  • 2021-01-13 16:04

    You can use PHP's built-in mail() function to send mails, however it is generally very limited. For instance, I don't think you can use other SMTP servers than the one specified in your php.ini file.

    Instead you should take a look at the Mail PEAR package. For example:

    <?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>");
    }
    ?>
    

    (I stole this example from http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm :P)

    0 讨论(0)
  • 2021-01-13 16:04

    PHP mailer will let you use any SMTP server you like, so long as you have the credentials to log on with.

    0 讨论(0)
  • 2021-01-13 16:08

    Read this 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);
    ?>
    
    0 讨论(0)
提交回复
热议问题