简体   繁体   English

电子邮件不是通过谷歌smtp发送的

[英]Emails not sent via google smtp

I'm using the following php code to send an email with an attachment to a predefined mail. 我正在使用以下PHP代码发送带有预定义邮件附件的电子邮件。

<?php

require_once('class.phpmailer.php');

$mail             = new PHPMailer();
$body             = $_POST['message'];
$mail->IsSMTP();
$mail->SMTPAuth   = true;
$mail->Host       = "smtp.gmail.com";
$mail->Port       = 465;
$mail->Username   = "dimal.chandrasiri@gmail.com";
$mail->Password   = "****";
$mail->SMTPSecure = 'tls';
$mail->SetFrom('dimal.chandrasiri@gmail.com', 'Your name');
$mail->AddReplyTo("dimal.chandrasiri@gmail.com","Your name");
$mail->Subject    = "Abstract submission for" .$_POST['fname'];
$mail->AltBody    = $_POST['message'];

if (isset($_FILES['uploaded_file']) && $_FILES['uploaded_file']['error'] == UPLOAD_ERR_OK){
    $mail->AddAttachment($_FILES['file']['tmp_name'],$_FILES['file']['name']);
}

$mail->MsgHTML($body);
$address = 'dimal.chandrasiri@gmail.com';
$mail->AddAddress($address, $name);

if($mail->Send()) {
      echo 'done';
} else {
      echo 'error';
}

?>

The passwords and other smtp details are correct but I'm unable to send any mail. 密码和其他smtp详细信息是正确的,但我无法发送任何邮件。 What am I doing wrong here? 我在这做错了什么? I use the following html form to post the data. 我使用以下html表单发布数据。

<form id="main-contact-form" class="abstract-form" name="contact-form" method="post" action="/script/submit.php">
<div class="row-fluid">
    <div class="span5">
        <label>First Name</label>
        <input type="text" name='fname' class="input-block-level" required="required" placeholder="Your First Name">
        <label>Last Name</label>
        <input type="text" name='lname' class="input-block-level" required="required" placeholder="Your Last Name">
        <label>Email Address</label>
        <input type="text" name='email' class="input-block-level" required="required" placeholder="Your email address">
        <label>File</label>
        <input type="file" name='file' class="input-block-level" required="required" placeholder="Your Abstract File">
    </div>
    <div class="span7">
        <label>Message</label>
        <textarea name="message" id="message" required="required" class="input-block-level" rows="8"></textarea>
    </div>
</div>
<button type="submit" class="btn btn-primary btn-large pull-right">Send Message</button>
<p> </p>    
</form>

I get the following error when I echo the error info. 当我回显错误信息时,我收到以下错误。

The following From address failed: dimal.chandrasiri@gmail.com : Called Mail() without being connected

Gmail users can access their account on the official website or by using first-party or third-party apps and services instead. Gmail用户可以在官方网站上访问其帐户,也可以使用第一方或第三方应用和服务。 A first party app is for instance Google's official Gmail app for Android, while Thunderbird and the mail client app of Windows 8 are third-party apps. 第一方应用程序是Google的Android官方Gmail应用程序,而Thunderbird和Windows 8的邮件客户端应用程序是第三方应用程序。

Google announced back in April 2014 that it would improve the sign-in security of its services and affect any application sending usernames and passwords to the company. 谷歌于2014年4月宣布 ,它将改善其服务的登录安全性,并影响向公司发送用户名和密码的任何应用程序。

The company suggested to switch to OAuth 2.0 back then but did not enforce it up until now. 该公司建议当时切换到OAuth 2.0,但直到现在才强制执行。

If you open the new less secure apps page under security settings on Google, you will notice that Google has disabled access by default. 如果您在Google上的安全设置下打开新的不太安全的应用页面,您会发现Google默认禁用了访问权限。

Note: You see the page only if you are not using Google Apps or have enabled two-factor authentication for the account. 注意:仅当您未使用Google Apps或已为该帐户启用双因素身份验证时,才会看到该页面。

You can flip the switch here to enable less secure applications again so that access is regained. 您可以在此处翻转开关以再次启用安全性较低的应用程序,以便重新获得访问权限。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM