1
<?php
    require_once "Mail.php";
    $from = "<niko@gmail.com>";
    $to = "<niko@hotmail.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";
    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "<niko@gmail.com>";
    $password = "somepassworrd";
    $headers = array ('From' => $from,'To' => $to,'Subject' => $subject);
    $smtp = Mail::factory('smtp', array ('host' => $host,'port' => $port,'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>");
?> 

When I try to execute these php script I get these error

  FATAL ERROR Class Mail NOT found ON number line 18

I know the above question is possible duplicate of these

Send email using the GMail SMTP server from a PHP page

But its not working.My PHP version is 5.3.4 Xampp 1.7.4 version.

 mail('caffeinated@example.com', 'My Subject', $message); \\ I tried these

But it shows a warning saying that missing headers.

  1. How do I send an email Using the php script?

  2. Can we send an email without using authentication in PHP ? Because vb.net uses server authentication but most of the code i found on google is without authentication for php. so I got a doubt

Finally please help me with these trying from an 2 hours or so!

Community
  • 1
  • 1
niko
  • 9,285
  • 27
  • 84
  • 131

1 Answers1

2

The PHP mail() function is for sending mail via Sendmail. If you want to use some SMTP server, you can use Zend_Mail which makes this thing very easy: http://framework.zend.com/manual/en/zend.mail.html

Using Zend_Mail you only need to write something like this:

$config = array('auth' => 'login',
            'username' => 'myusername',
            'password' => 'password');

$transport = new Zend_Mail_Transport_Smtp('mail.server.com', $config);

$mail = new Zend_Mail();
$mail->setBodyText('This is the text of the mail.');
$mail->setFrom('sender@test.com', 'Some Sender');
$mail->addTo('recipient@test.com', 'Some Recipient');
$mail->setSubject('TestSubject');
$mail->send($transport);

The above handles authentication for you and sends a mail.

Ofir
  • 415
  • 2
  • 7
  • Could you kindly show me other without authentication ? please! – niko Sep 11 '11 at 20:17
  • what should i update in the 'auth' field ? login itself or what ? – niko Sep 11 '11 at 20:20
  • Hi, you just omit the $config variable from the Zend_Mail_Transport_Smtp line. Meaning: $transport = new Zend_Mail_Transport_Smtp('mail.server.com'); – Ofir Sep 11 '11 at 20:21
  • You just need to change the username and password fields – Ofir Sep 11 '11 at 20:22
  • It says Fatal error: Class 'Zend_Mail_Transport_Smtp' not found in C:\xampp\htdocs\website\mail.php on line 6 – niko Sep 11 '11 at 20:27
  • You need to [download the framework](http://framework.zend.com/download/latest) and include Zend_Mail before you can use it. – Ofir Sep 11 '11 at 20:31