我正在尝试配置谷歌SMTP

时间:2017-03-02 10:51:12

标签: php phpmailer

我正在尝试配置谷歌SMTP,但每次我得到SMTP连接()失败。消息无法发送.Mailer错误:SMTP connect()失败错误,我尝试了多个解决方案,但这里没有任何工作。如何启用smtp邮件服务。这是我的代码

<html>
<head>
    <meta charset="UTF-8">
    <title>Send email via Gmail SMTP server in PHP</title>
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
    <div id="main">
        <h1>Send Email via Gmail SMTP Server in PHP</h1>
        <div id="login">
            <h2>Send Email</h2>
            <hr/>
            <form action="index.php" method="post">
                <input type="text" placeholder="Enter your Gmail ID" name="email"/>
                <input type="password" placeholder="Enter your Gmail Password" name="password"/>
                <input type="text" placeholder="To : Email Id " name="toid"/>  
                <input type="text" placeholder="Subject : " name="subject"/>
                <textarea rows="4" cols="50" placeholder="Enter Your Message..." name="message"></textarea>
                <input type="submit" value="Send" name="send"/>
            </form>    
        </div>
    </div>
    <?php

        require 'phpmailer/PHPMailerAutoload.php';
        if(isset($_POST['send'])){      
            $mail = new PHPMailer;
            $mail->SMTPDebug = 2;  //Enable SMTP debugging. 
            $mail->isSMTP();/*Set mailer to use SMTP*/
            $mail->Host = 'smtp.gmail.com';/*Specify main and backup SMTP servers*/
            $mail->Port = 587;
            $mail->SMTPAuth = true;/*Enable SMTP authentication*/
            $mail->Username = $email;/*SMTP username*/
            $mail->Password = $password;/*SMTP password*/
            $mail->SMTPSecure = 'tls';/*Enable encryption, 'ssl' also accepted*/
            $mail->From = 'abc';
            //$mail->FromName = $name;
            $mail->addAddress('mmmmmm@gmail.com', 'demo');/*Add a recipient*/
            //$mail->addReplyTo($email, $name);
            /*$mail->addCC('cc@example.com');*/
            /*$mail->addBCC('bcc@example.com');*/
            $mail->WordWrap = 70;/*DEFAULT = Set word wrap to 50 characters*/
            $mail->addAttachment('../tmp/' . $varfile, $varfile);/*Add attachments*/
            /*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
            /*$mail->addAttachment('/tmp/image.jpg', 'new.jpg');*/
            $mail->isHTML(true);/*Set email format to HTML (default = true)*/
            $mail->Subject = $subject;
            $mail->Body    = "<i>Mail body in HTML</i>";
            $mail->AltBody = "This is the plain text version of the email content";
            if(!$mail->send()){
                echo 'Message could not be sent.';
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            }else{
                header("Location: http://cetpatechnologies.com/mail/index.php");
            }
        }
    ?>
</body>

1 个答案:

答案 0 :(得分:-1)

您无法以这种方式使用Gmail。

Gmail提供的SMTP支持可用于个人电子邮件客户端。它不是一般的电子邮件递送服务,也不能有效地用作一个。

如果您需要从PHP脚本发送邮件,请使用ESP(电子邮件服务提供商)设置帐户。在线提供了大量这些,其中许多提供免费帐户,用于低容量使用。