Skip to content
Advertisement

phpmailer working on localhost but when i upload it on my ubuntu server it gives me error and mail is not sending(I am using gmail smtp)

    <?php 
    if(isset($_POST['sendmail'])) {
        
        require 'PHPMailerAutoload.php';
        require 'credential.php';

        $mail = new PHPMailer;

        $mail->SMTPDebug = 4;                               // Enable verbose debug output

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = EMAIL;                 // SMTP username
        $mail->Password = PASS;                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        $mail->setFrom(EMAIL, 'litdeveloper');
        $mail->addAddress($_POST['email']);     // Add a recipient

        $mail->addReplyTo(EMAIL);
        // print_r($_FILES['file']); exit;
        for ($i=0; $i < count($_FILES['file']['tmp_name']) ; $i++) { 
            $mail->addAttachment($_FILES['file']['tmp_name'][$i], $_FILES['file']['name'][$i]);    // Optional name
        }
        $mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = $_POST['subject'];
        $mail->Body    = '<div style="border:2px solid red;">This is the HTML message body <b>in bold!</b></div>'.$_POST['message'];
        $mail->AltBody = $_POST['message'];

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

How can I resolve this error i also tried after commenting this $mail->isSMTP() but it is not working. It is working properly on XAMP Email id and password is correct what should i do plz help me out

Advertisement

Answer

The most important thing to do when you have an error message is read what it says, so when it says:

534-5.7.14 Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 g206sm18050557pfb.178 - gsmtp

It should not come as a surprise that the way to fix this is to log in to your gmail account using a normal web browser, and follow that link if you want to find out why they ask you to do this.

For other information, you should follow the link to the troubleshooting guide that appears in your debug output (and you reposted here twice!), in particular this part, which addresses the exact problem you have.

Apart from the fact that you’re using a very old version of PHPMailer, your config and code looks correct; that’s not where the problem lies.

Incidentally, you should now change your gmail password as it is displayed in an easily decoded format in your debug output.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement