0

can someone help me to find what is wrong with the following code that i used to send mail in php.

error_reporting(E_STRICT);
date_default_timezone_set('asia/kolkata');
require_once('class.phpmailer.php');
$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; 
$mail->IsHTML(true);
$mail->Username = "manikandan@gmail.com";
$mail->Password = "mypassword";
$mail->From = ("manikandan@gmail.com");
$mail->SetFrom("manikandan");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("peter1991@gmail.com");
 if(!$mail->Send())
    {
    echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
    echo "Message has been sent";
    }

?>

i am getting the following error:- 2015-01-05 04:32:10 Invalid address: manikandan i am using php 5.2.2 ,Apache 2.0 Handler in windows.

  • possible duplicate of [send email using Gmail SMTP server through PHP Mailer](http://stackoverflow.com/questions/16048347/send-email-using-gmail-smtp-server-through-php-mailer) – ecnepsnai Jan 05 '15 at 04:53
  • yes i did and tried changing php.ini also – Johnuser4418813 Jan 05 '15 at 04:59
  • If it is saying `Invalid address: manikandan` I think your problem is `$mail->SetFrom("manikandan");`. Change that to `$mail->SetFrom("manikandan@gmail.com");` – Mooseknuckles Jan 05 '15 at 08:09

3 Answers3

2

setFrom expects an email address as the first parameter. Try:

$mail->setFrom('manikandan@gmail.com');

If you also want to set a proper name, use the second parameter

$mail->setFrom('manikandan@gmail.com', 'John Smith');

http://phpmailer.github.io/PHPMailer/classes/PHPMailer.html#method_setFrom

Also, I think you should be using the autoloader instead of including the PHPMailer class directly.

Mathew Tinsley
  • 6,805
  • 2
  • 27
  • 37
  • No error message? If it is not displaying the same error message, but still not working it means you have another problem. – Mathew Tinsley Jan 05 '15 at 05:07
  • i think so @mtinsley i tried everything i know but i couldn1t fix it as i am new to programing – Johnuser4418813 Jan 05 '15 at 05:10
  • If you're not getting an error message it's difficult to say where to go from here. Try setting your error reporting to E_ALL at the very beginning of your script. Also make sure display errors is on: ini_set('display_errors', '1'); – Mathew Tinsley Jan 05 '15 at 05:13
  • yeah now i am getting smtp not found in class.phpmailer.php. i removed semi colon from PHPMailerAutoload.php in php.ini but no use – Johnuser4418813 Jan 05 '15 at 05:19
  • Have you tried using the autoloader: https://github.com/PHPMailer/PHPMailer/issues/113 – Mathew Tinsley Jan 05 '15 at 05:20
  • Use the autoloader (see the answer below with the require for PHPMailerAutoload rather than class.phpmailer.php). PHPMailer really needs to update their documentation because I've seen a lot of people run into this. – Mooseknuckles Jan 05 '15 at 05:25
  • yeah i tried PHPMailerAutoload.php and removed ; from extension=php_openssl.dll in php.ini and restarted the wamp server but it displays the following error SMTP ERROR: Failed to connect to server: (161281628) SMTP connect() failed. Mailer Error: SMTP connect() failed – Johnuser4418813 Jan 05 '15 at 06:18
  • I'm not sure what the problem is, but it looks like the issue you are experiencing now is different from the original issue. You may have better luck posting another question regarding this new error. – Mathew Tinsley Jan 05 '15 at 06:37
  • @Mooseknuckles I don't know what you mean; the documentation has said exactly this ever since the autoloader was added. I know because I wrote both. – Synchro Jan 05 '15 at 06:39
  • @Synchro What I mean is that all of their examples still say to include the class rather than the autoloader. I had to Google to figure that out (and, not ironically, found a StackOverflow thread about it to show me the answer.) http:// phpmailer.worxware.com/index.php?pg=examplebsmtp All of their examples still have requiring the class. And you can find a ton of questions on Stack that need that answer So, if you are affiliated with them please update your examples (tired of figuring out how to post a damn link in a comment, so copy/paste it.) – Mooseknuckles Jan 05 '15 at 08:15
  • Don't use the worxware site - it's not been where PHPMailer lives for many years. It's been [on GitHub](https://github.com/PHPMailer/PHPMailer) for a couple of years now, and it was on Google Code and SourceForge before that. None of the examples in the current location use the old mechanism. Worxware's site does say this and also points to GitHub, but I have no control over Worxware's site. – Synchro Jan 05 '15 at 08:17
  • Good to know. Unfortunately, Worxware still comes up first in a Google search for almost anything related to PHPMailer so I think that leads to a lot of confusion because people are following those examples. – Mooseknuckles Jan 05 '15 at 08:19
  • I know, its's annoying, but nothing I can do about that except play whack-a-mole with all the references that keep popping up... – Synchro Jan 05 '15 at 08:20
  • Fair enough :) Not your fault, for sure. You'd think if they aren't maintaining it anymore they'd just take the site down. That'd be too easy, though. – Mooseknuckles Jan 05 '15 at 08:21
0

try this:

$mail->setFrom('manikandan@gmail.com', 'manikandan'); //setFrom expect email 

Full code:

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

 //Create a new PHPMailer instance
 $mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->IsSMTP(); 
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "manikandan@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "mypassword";
//Set who the message is to be sent from
$mail->setFrom('manikandan@gmail.com', 'manikandan');
//Set an alternative reply-to address
$mail->addReplyTo('peter1991@gmail.com', 'peter');
//Set who the message is to be sent to
 $mail->addAddress('peter1991@gmail.com', 'peter1991');
 //Set the subject line
 $mail->Subject = 'Test';
//Read an HTML message body from an external file, convert referenced images to embedded,
 //convert HTML into a basic plain-text alternative body
  $mail->Body     = "Hi ,! Welcome to PHP mail function. \n\n  .";
//Replace the plain text body with one created manually
  $mail->AltBody = 'This is a plain-text message body';
 //Attach an image file
 //$mail->addAttachment('images/phpmailer_mini.gif');
 $mail->SMTPAuth = true;
 //send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>  
Priyank
  • 3,778
  • 3
  • 29
  • 48
  • Did you running it in your local? – Priyank Jan 05 '15 at 05:17
  • @Johnuser4418813 : also try $mail->SMTPDebug = 2; before sending your mail! – Priyank Jan 05 '15 at 05:23
  • hey thanks buddy but same problem smtp not found in class.phpmailer.php i remved ; from"PHPMailerAutoload.php" in php.ini but no use – Johnuser4418813 Jan 05 '15 at 05:23
  • tried ur updated answere its showing the following error SMTP ERROR: Failed to connect to server: (161281628) SMTP connect() failed. Mailer Error: SMTP connect() failed. – Johnuser4418813 Jan 05 '15 at 05:57
  • @Johnuser4418813 Are you running on Localhost? – Priyank Jan 05 '15 at 05:59
  • tried to debug the error but i failed... do i have to make some changed in php.ini – Johnuser4418813 Jan 05 '15 at 05:59
  • tried this 1. Open php.ini 2. Search for extension=php_openssl.dll 3. The initial will look like this ;extension=php_openssl.dll 4. Remove the ';' and it will look like this extension=php_openssl.dll 5. If you can't find the extension=php_openssl.dll, add this line extension=php_openssl.dll. – Priyank Jan 05 '15 at 06:01
  • @Johnuser4418813 search "extension=php_openssl.dll" in php.ini and remove semi-colon";" and restart wamp/xampp – Priyank Jan 05 '15 at 06:03
  • @Johnuser4418813 did you remove ";" form extension=php_openssl.dll and restart server? – Priyank Jan 05 '15 at 06:12
  • yes i did it again and restarted wamp server, when i run my program it took too much of time though its configuring to send mail but it displays the following error msg SMTP ERROR: Failed to connect to server: (161281628) SMTP connect() failed. Mailer Error: SMTP connect() failed. – Johnuser4418813 Jan 05 '15 at 06:16
  • can i use 'class.phpmailer.php' along with PHPMailerAutoload.php – Johnuser4418813 Jan 05 '15 at 06:21
  • No, loading the autoloader loads the other classes for you, that's what it's for. – Synchro Jan 05 '15 at 06:35
0

To resolved this just turn on access for less secure app in your google account. Hope this helps.

smuhero
  • 19
  • 10