rahrahruby
rahrahruby

Reputation: 693

PHP mail with gmail example

I have the following code that I believe will work, but it requires Mail.php. I installed pear mail, but don't have an example of the Mail.php file. Does anyone have an example? I need help putting it in place so that the code will execute. Thanks

   require_once "Mail.php";

    $from = "<[email protected]>";
    $to = "<[email protected]>";
    $subject = "Test from iyearbook!";
    $body = "Hi,\n\nHow are you?";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "[email protected]";
    $password = "password";

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

?>  <!-- end of php tag--->

Upvotes: 1

Views: 3958

Answers (2)

denislexic
denislexic

Reputation: 11342

To simplify the emailing process I ended up creating my own function, something like pearMail( $to,$subject,$message,$header,$returnEmail );

//To define $to , $subject , $html
//Optional defines  $sender1 , $sender2


//REQUIRED EXAMPLE
   // $to = '';               // Email address of the person the mail goes to
   // $subject = $subject;    // Subject for the email
   //  $html = '<html><body><p>This is a html message</p></body></html>';  // HTML version of the email
   // $filename = 'mail/contact.html  // and create an array called $params to replace in the email.


//POINT CORRECTLY
include('PEAR/Mail.php');
include('PEAR/mime.php');

//INFO UNCHANGABLE
$crlf = "\n";
$headers = array('From' => $sender1, 'Return-Path' => $sender2, 'Subject' => $subject);
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp_params["host"] = "ssl://smtp.gmail.com"; // SMTP host
$smtp_params["port"] = "465"; // SMTP host
$smtp_params["auth"] = true;               
$smtp_params["username"] = "[email protected]";        // authentication.
$smtp_params["password"] = 'YOURPASSWORD'; 
$mail =& Mail::factory("smtp", $smtp_params);
$result = $mail->send($to, $headers, $body);

Upvotes: 2

Stewie
Stewie

Reputation: 3121

You dont have to manually write a Mail.php class file. When you install it via pear, it installs itself. Its all about the inlude path, if the include path is correctly set, the Mail.php will be available for use.

Check your pear include path. When you require Mail.php , does it give you any error ? Turn on error reporting and see. Most probably your pear include path is not set

Try this too:

<?php
require_once 'Mail.php';
var_dump(class_exists('Mail', false));
?> 

To verify/fix the pear include path check this: http://pear.php.net/manual/en/installation.checking.php

Upvotes: 5

Related Questions