簡體   English   中英

帶有Gmail示例的PHP郵件

[英]PHP mail with gmail example

我有以下代碼,我相信它會起作用,但是需要Mail.php。 我安裝了梨形郵件,但是沒有Mail.php文件的示例。 有人有例子嗎? 我需要將其放置到位的幫助,以便代碼可以執行。 謝謝

   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.gmail.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--->

您不必手動編寫Mail.php類文件。 通過pear安裝時,它會自行安裝。 有關inlude路徑的所有信息,如果正確設置了include路徑,則可以使用Mail.php。

檢查您的梨包含路徑。 當您需要Mail.php時,是否會出現任何錯誤? 打開錯誤報告並查看。 您的梨包含路徑很可能未設置

也嘗試一下:

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

要驗證/修復梨包含路徑,請檢查以下內容: http : //pear.php.net/manual/en/installation.checking.php

為了簡化電子郵件發送過程,我最終創建了自己的函數,如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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM