tags:

views:

69

answers:

5

Can some one suggrst me a code where i can use yahoo smtp to send mails in PHP.

A: 

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

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

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

gabe3886
A: 

You should use something like Swift Mailer or PHPMailer. The following example is for Swift:

$message = Swift_Message::newInstance()
  ->setSubject('Your subject')
  ->setFrom(array('[email protected]' => 'John Doe'))
  ->setTo(array('[email protected]', '[email protected]' => '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);
Shabbyrobe
+1  A: 

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 <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$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)

Frxstrem
+2  A: 

require("class.phpmailer.php"); $mail = new PHPMailer();

$mail->IsSMTP();

$mail->SMTPAuth = true; // enable SMTP authentication

$mail->SMTPSecure = "ssl";

$mail->Host = "plus.smtp.mail.yahoo.com";

$mail->Port = 465; // set the SMTP port

$mail->Username = "[email protected]";

$mail->Password = "password";

$mail->From = "[email protected]";

$mail->FromName = "myname";

$mail->AddAddress("[email protected]");

$mail->Subject = "Test PHPMailer Message";

$mail->Body = "Hi! \n\n This was sent with phpMailer_example3.php.";

if(!$mail->Send()) {

echo 'Message was not sent.';

echo 'Mailer error: ' . $mail->ErrorInfo;

} else {

echo 'Message has been sent.';

} ?>

After the run it is showing:

SMTP Error: Could not authenticate. Message was not sent.Mailer error: SMTP Error: Could not authenticate. SMTP server error:

were is the error??

Tiya