php邮件与smtp不工作的Gmail [英] php mail for gmail with smtp not working

查看:228
本文介绍了php邮件与smtp不工作的Gmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码,我正在使用google应用程序进行业务。我在下面的代码中用 * 替换了我的密码。

 <!DOCTYPE html> 
< html>
< body>

< h1>我的第一个PHP页面< / h1>
<?php
require(class.phpmailer.php);

$ mail = new PHPMailer();

$ mail-> IsSMTP(); //告诉该类使用SMTP
$ mail-> SMTPAuth = true; // SMTP认证
$ mail-> Host =smtp.gmail.com; // SMTP服务器
$ mail->端口= 465; // SMTP端口
$ mail->用户名=adminbot@mycompany.com; // SMTP帐户用户名
$ mail->密码=*********; // SMTP帐户密码

$ mail-> SetFrom('adminbot@mycompany.com','John Doe'); // FROM
$ mail-> AddReplyTo('adminbot@mycompany.com','John Doe'); //回复b
$ b $ mail-> AddAddress('ZTruse1955@mycompany.com','Jane Doe'); //收件人电子邮件

$ mail-> Subject =First SMTP Message; // email subject
$ mail-> Body =Hi!\\\
\\\
这是我使用PHPMailer通过Google SMTP发送的第一封电子邮件。;

if(!$ mail-> Send()){
echo'消息未发送。';
echo'邮件错误:'。 $ MAIL-> ERRORINFO;
} else {
echo'消息已发送。';
}
?>

当我打开页面时,页面空白,打开页面时没有收到邮件。
我是否需要安装任何php库以实现此功能,以便安装默认的lap安装?

解决方案

两种不同的邮件方式适合您的要求。



第一种情况;

  // Pear Mail Library 
require_onceMail.php;

$ from ='< from.gmail.com>';
$ to ='< to.yahoo.com>';
$ subject ='嗨!';
$ body =\\\\\你好吗?

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

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

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

if(PEAR :: isError($ mail)){
echo('< p>。$ mail-> getMessage()。'< / p>') ;
} else {
echo('< p>邮件成功发送!< / p>');
}

第二种情况; ( http://ctrlq.org/code/19589-send-mail-php

  $ mail = new PHPMailer; 

$ mail-> isSMTP(); //设置邮件使用SMTP
$ mail-> Host ='smtp.gmail.com'; //指定主服务器和备份服务器
$ mail-> SMTPAuth = true; //启用SMTP认证
$ mail->用户名='amit@gmail.com'; // SMTP用户名
$ mail->密码='digitalinspiration'; // SMTP密码
$ mail-> SMTPSecure ='tls'; //启用加密,'ssl'也接受
$ mail->端口= 587; //设置SMTP端口号 - 587认证的TLS
$ mail-> setFrom('amit@gmail.com','Amit Agarwal'); //设置邮件的发件人是
$ mail-> addReplyTo('labnol@gmail.com','First Last'); //设置另一个回复地址
$ mail-> addAddress('josh@example.net','Josh Adams'); //添加收件人
$ mail-> addAddress('ellen@example.com'); //名称是可选的
$ mail-> addCC('cc@example.com');
$ mail-> addBCC('bcc@example.com');
$ mail-> WordWrap = 50; //设置自动换行为50个字符
$ mail-> addAttachment('/ usr / labnol / file.doc'); //添加附件
$ mail-> addAttachment('/ images / image.jpg','new.jpg'); //可选名称
$ mail-> isHTML(true); //将电子邮件格式设置为HTML

$ mail-> Subject ='这是主题';
$ mail-> Body ='这是HTML邮件正文< b>加粗!< / b>';
$ mail-> AltBody ='这是非HTML邮件客户端的纯文本正文';

//从外部文件读取HTML邮件正文,将引用的图像转换为嵌入式,
//将HTML转换为基本的纯文本备选正文
$ mail-> ; msgHTML(file_get_contents('contents.html'),dirname(__ FILE__));

if(!$ mail-> send()){
echo'消息无法发送。';
回声'梅勒错误:'。 $ MAIL-> ERRORINFO;
出口;
}

echo'消息已发送';

试试吧,希望它能解决您的问题!


Following is my code and I am using google apps for business. I have replaced my password with * in the following code. I am using apache2.

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>
<?php
    require("class.phpmailer.php");

    $mail = new PHPMailer();

    $mail->IsSMTP();  // telling the class to use SMTP
    $mail->SMTPAuth   = true; // SMTP authentication
    $mail->Host       = "smtp.gmail.com"; // SMTP server
    $mail->Port       = 465; // SMTP Port
    $mail->Username   = "adminbot@mycompany.com"; // SMTP account username
    $mail->Password   = "*********";        // SMTP account password

    $mail->SetFrom('adminbot@mycompany.com', 'John Doe'); // FROM
    $mail->AddReplyTo('adminbot@mycompany.com', 'John Doe'); // Reply TO

    $mail->AddAddress('ZTruse1955@mycompany.com', 'Jane Doe'); // recipient email

    $mail->Subject    = "First SMTP Message"; // email subject
    $mail->Body       = "Hi! \n\n This is my first e-mail sent through Google SMTP using PHPMailer.";

    if(!$mail->Send()) {
      echo 'Message was not sent.';
      echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
      echo 'Message has been sent.';
    }
?>

When I open the page I get blank with no mail received upon opening the page. Do I need to install any php libraries for this to work apart from default lap installation?

解决方案

I have shared two different mail methods for your requirement..

First case;

// Pear Mail Library
require_once "Mail.php";

$from = '<from.gmail.com>';
$to = '<to.yahoo.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' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

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

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

Second case; (http://ctrlq.org/code/19589-send-mail-php)

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'amit@gmail.com';                   // SMTP username
$mail->Password = 'digitalinspiration';               // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 587;                                    //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('amit@gmail.com', 'Amit Agarwal');     //Set who the message is to be sent from
$mail->addReplyTo('labnol@gmail.com', 'First Last');  //Set an alternative reply-to address
$mail->addAddress('josh@example.net', 'Josh Adams');  // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->addAttachment('/usr/labnol/file.doc');         // Add attachments
$mail->addAttachment('/images/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));

if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';

Try it and hope it will solve your issues !

这篇关于php邮件与smtp不工作的Gmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆