motaz jalil
motaz jalil

Reputation: 7

PHPmailer Connect E-mail server with PHP

I am working on a project and in my registration form when the user registers I want to E-mail them an activation link. the registration form works, but I am having issues with the E-Mail server. This is my php code using Phpmailer that I have but it is not working and I do not know why !

require 'include/class.phpmailer.php';
    require 'include/PHPMailerAutoload.php';

function smtpmailer($to, $from, $from_name, $subject, $body) { 
global $error;
$mail = new PHPMailer();  // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;  // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465; 
$mail->Username = jalilmotaz;  
$mail->Password = *****;           
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
    $error = 'Mail error: '.$mail->ErrorInfo; 
    return false;
} else {
    $error = 'Message sent!';
    return true;
}

Any sugesstions on how to work this out by gmail or some other way?

Upvotes: 1

Views: 62

Answers (1)

Akshay
Akshay

Reputation: 2229

Multiple mistakes in your code.

1). SSL is deprecated. Use tls.

2). Put your full gmail email and password, and within double quoted ".

<?php
    require 'include/class.phpmailer.php';
        require 'include/PHPMailerAutoload.php';

    function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 1;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587; 
    $mail->Username = "[email protected]";  
    $mail->Password = "passwordhere";           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }
    ?>

Upvotes: 1

Related Questions