3

I'm using the following php code to send an email with an attachment to a predefined mail.

<?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. What am I doing wrong here? I use the following html form to post the data.

<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
Imesh Chandrasiri
  • 5,558
  • 15
  • 60
  • 103

1 Answers1

0

Gmail users can access their account on the official website or by using first-party or third-party apps and services instead. 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 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.

The company suggested to switch to OAuth 2.0 back then but did not enforce it up until now.

If you open the new less secure apps page under security settings on Google, you will notice that Google has disabled access by default.

Note: You see the page only if you are not using Google Apps or have enabled two-factor authentication for the account.

You can flip the switch here to enable less secure applications again so that access is regained.

enter image description here

Hackoo
  • 18,337
  • 3
  • 40
  • 70