Search code examples
phpphpmailer

phpmailer email send not working


hi all i try to send email through phpmail i try this code

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

$mail = new PHPMailer();

//$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'mypassword';

$mail->setFrom('[email protected]', 'Senaid Bacinovic');
$mail->addAddress('[email protected]');
$mail->Subject = 'SMTP email test';
$mail->Body = 'this is some body';

if ($mail->send())
    echo "Mail sent";

 ?>

i download phpmail from here and open my browser and http://localhost/mail/ its show blank page

note:os ubuntu and all php and phpmailer inside of mail folder

and i use apache2

this is my error log

[Thu Jul 26 12:35:37.405468 2018] [php7:error] [pid 1922] [client 127.0.0.1:40440] PHP Fatal error:  require(): Failed opening required 'phpmailer/PHPMailerAutoload.php' (include_path='.:/usr/share/php') in /var/www/html/lear/index.php on line 2
[Thu Jul 26 12:36:31.746585 2018] [php7:warn] [pid 1924] [client 127.0.0.1:40528] PHP Warning:  require(phpmailer/PHPMailerAutoload.php): failed to open stream: No such file or directory in /var/www/html/lear/index.php on line 2
[Thu Jul 26 12:36:31.746643 2018] [php7:error] [pid 1924] [client 127.0.0.1:40528] PHP Fatal error:  require(): Failed opening required 'phpmailer/PHPMailerAutoload.php' (include_path='.:/usr/share/php') in /var/www/html/lear/index.php on line 2
[Thu Jul 26 12:43:53.780141 2018] [php7:error] [pid 1921] [client 127.0.0.1:40950] PHP Fatal error:  Uncaught Error: Class 'PHPMailer' not found in /var/www/html/lear/index.php:4\nStack trace:\n#0 {main}\n  thrown in /var/www/html/lear/index.php on line 4
[Thu Jul 26 12:43:57.170161 2018] [php7:error] [pid 1923] [client 127.0.0.1:40952] PHP Fatal error:  Uncaught Error: Class 'PHPMailer' not found in /var/www/html/lear/index.php:4\nStack trace:\n#0 {main}\n  thrown in /var/www/html/lear/index.php on line 4
[Thu Jul 26 12:44:44.728442 2018] [php7:error] [pid 3665] [client 127.0.0.1:40958] PHP Fatal error:  Uncaught Error: Class 'PHPMailer' not found in /var/www/html/lear/index.php:4\nStack trace:\n#0 {main}\n  thrown in /var/www/html/lear/index.php on line 4

Solution

  • $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 = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsHTML(true);
    $mail->Username = "[email protected]";
    $mail->Password = "password";
    $mail->SetFrom("[email protected]");
    $mail->Subject = "Test";
    $mail->Body = "hello";
    $mail->AddAddress("[email protected]");
    
     if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
        echo "Message has been sent";
     }