开发者

PHP mail with gmail example

开发者 https://www.devze.com 2023-02-27 08:04 出处:网络
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 i

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 = "<me1@gmail.com>";
    $to = "<me2@gmail.com>";
    $subject = "Test from iyearbook!";
    $body = "Hi,\n\nHow are you?";

    $host = "ssl://smtp.gmai开发者_运维技巧l.com";
    $port = "465";
    $username = "me@gmail.com";
    $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--->


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


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"] = "YOUR@EMAIL.COM";        // authentication.
$smtp_params["password"] = 'YOURPASSWORD'; 
$mail =& Mail::factory("smtp", $smtp_params);
$result = $mail->send($to, $headers, $body);
0

精彩评论

暂无评论...
验证码 换一张
取 消