PHP使用谷歌应用程序smtp发送电子邮件

时间:2013-08-19 06:52:27

标签: php gmail phpmailer google-apps

我有一个域名包含一些电子邮件并使用此域名谷歌应用程序,在这种情况下,我想使用smtp的gmail发送没有问题的一些电子邮件给我的客户

Tha案例脚本对我不起作用,我把案例

date_default_timezone_set('Europe/Paris');

require_once("class.phpmailer.php");
require_once("class.smtp.php");


$mail             = new PHPMailer();
$body             = "gdssdh";
//$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "user@gmail.com";  // GMAIL username
$mail->Password   = "123456789";            // GMAIL password

$mail->SetFrom('accountemail@mydomain.com', 'PRSPS');

//$mail->AddReplyTo("user2@gmail.com', 'First Last");

$mail->Subject    = "Hello it´s a test";

//$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

1 个答案:

答案 0 :(得分:1)

如何使用梨来邮寄,我有一个工作脚本,但

require_once "pear/Mail.php";
include "pear/Mail/mime.php" ;

$from = '<admin@gmail.com>';
$to = '<yourmail@gmail.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'admin@gmail.com',
        'password' => 'password'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}