PHPmailer连接电子邮件服务器与PHP


PHPmailer Connect E-mail server with PHP

我正在做一个项目,当用户注册时,我想通过电子邮件向他们发送一个激活链接。注册表格工作,但我有问题与电子邮件服务器。这是我的php代码使用Phpmailer,我有,但它不工作,我不知道为什么!

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;
}

关于如何通过gmail或其他方式解决这个问题有什么建议吗?

你的代码有多个错误。

1)。SSL已弃用。使用tls。

2)。把你的gmail邮箱地址和密码放在"的双引号内。

<?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 = "youremail@gmail.com";  
    $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;
    }
    ?>