0

A beginner here.

I have already checked the forum to find out answers but I was not successful, other questions were specifically on some parts of PHPMailer but mine is more general. So I hope no one will mark my question as duplicate as I am in learning curve.

I am working on a PHP project. How it works is that the user goes to the page and writes some comments or issues in a form (like a text editor) and clicks on the send button. I should be able to receive his message to my email. I have set my Gmail account here for testing purposes but later it will be my real email with my own domain.

Here is the error that I am receiving when I run on local host:

Fatal error: Uncaught exception 'phpmailerException' with message 'Could not execute: /usr/sbin/sendmail' in C:\xampp\htdocs\pp\classes\class.phpmailer.php:1100 Stack trace: #0 C:\xampp\htdocs\pp\classes\class.phpmailer.php(1026): PHPMailer->sendmailSend('Date: Thu, 9 Oc...', '--b1_9ea0b33e3f...') #1 C:\xampp\htdocs\pp\classes\class.phpmailer.php(935): PHPMailer->postSend() #2

Here is the code I am using:

<?php 
require_once("../../classes/class.phpmailer.php");

if($_POST['mode']=='send'){

    $mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

    $mail->IsSendmail(); // telling the class to use SendMail transport

        //I assume this part is to make it run on linux base on Google search
    $body = "New Bug Report from ".$_SESSION['name']."\n".$_POST['bug'];
    $mail->AddReplyTo('mj@gmail.com', 'MJ Team');
    $mail->AddAddress(''.$_SESSION['email'].'', ''.$_SESSION['name'].'');
    $mail->SetFrom(mj@gmail.com', 'MJ Team');
    $mail->AddReplyTo('mj@gmail.com', 'MJ Team');
    $mail->Subject = 'New bug report for the portal';
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
    $mail->MsgHTML($body);
    $mail->Send();

            //And I assume this part of the code makes it run on windows based on Google search
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = "smtp.postmarkapp.com";
        $mail->Port = 26;
        $mail->Username = "MJ";
        $mail->Password = "MJ";
        $mail->SetFrom('mj@gmail.com', 'mj');
        $mail->Subject = "An email for test";
        $mail->AddAddress($address, $name);

    if($mail){
        $message = 'Thanks. Bug report successfully sent. We will get in touch if we have any more questions.';
        }
        else {
            echo "Mailer Error: " . $mail->ErrorInfo;
        }
}
?>

Just as extra information I was not able to find any user and pass for SMTP so I just filled with with my name which obviously shouldn't be right. Since I am beginner I appreciate any comments and suggestion code that might help me run my code.

Thank you!

Phil
  • 157,677
  • 23
  • 242
  • 245
Irfana
  • 231
  • 3
  • 11
  • Forget my (deleted) answer. Look at the syntax highlighting. You're missing an opening quote in this line: `$mail->SetFrom(mj@gmail.com', 'MJ Team');` –  Oct 09 '14 at 02:21
  • Also, `$mail->AddAddress(''.$_SESSION['email'].'', ''.$_SESSION['name'].'');` this doesn't need `''` around the `$_SESSION` variables. – Ohgodwhy Oct 09 '14 at 02:22
  • @MikeW and Ohgodwhy , even after fixing the edit that you mentioned, still it is not working! – Irfana Oct 09 '14 at 02:27
  • is port 26 correct? Should it not be 25? – John Rah Oct 09 '14 at 02:35
  • @JohnRah I am not sure if port 26 is correct or not. But port 25 is not working either! – Irfana Oct 09 '14 at 02:42
  • As you can see from the message - it is trying to use sendmail. The error is created by the code above `//And I assume this part of the code makes it run on windows` You do not need BOTH versions, only the one that is for Windows. – Cheery Oct 09 '14 at 02:48
  • This code is just a mess; I'm not even going to attempt to fix it. You're setting lots of options *after* calling `send()`, which is completely pointless. I suggest you throw it away and start again using a vaguely correct script, such as [any of the examples provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/tree/master/examples). – Synchro Oct 09 '14 at 06:05

2 Answers2

0

Try this :

<?php 
session_start();

if(isset($_POST['mode']) && $_POST['mode']=='send' && 
   isset($_SESSION['email'], $_SESSION['name'])){

    require_once("PHPMailerAutoload.php");
    $mail = new PHPMailer(true);

    //Send mail using gmail
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "mj@gmail.com"; // GMAIL username
    $mail->Password = "mypassword"; // GMAIL password

    //Typical mail data
    $mail->AddAddress($_SESSION['email'], $_SESSION['name']);
    $mail->SetFrom('mj@gmail.com', 'mj');
    $mail->Subject = "This is a test message";
    $mail->Body = "An email for test";

    try{
        $mail->Send();
        echo "Thanks. Bug report successfully sent.
              We will get in touch if we have any more questions!";
    } catch(Exception $e){
        //Something went bad
        echo "Mailer Error: - " . $mail->ErrorInfo;
    }
}else{
    echo 'missing required values!';
}
meda
  • 45,103
  • 14
  • 92
  • 122
  • the IDE is giving error on this line: $mail->SetFrom($mj@gmail.com, 'mj'); are you sure I have to use $ ? – Irfana Oct 09 '14 at 04:19
  • @media, I tried your code, I do not get Fatal error any more but get another error: Mailer Error: - SMTP Error: Could not authenticate. Any suggestion? – Irfana Oct 09 '14 at 04:23
  • you have to fix your gmail credential I tested it – meda Oct 09 '14 at 04:31
  • Hello @Vivekpradhan, do you have any suggestion on the above code which might help me run my code and receive email? – Irfana Oct 09 '14 at 04:32
  • I have fixed my email credential and tested it and still not working here! – Irfana Oct 09 '14 at 04:33
  • This code will not work with latest PHPMailer because you are loading the wrong file - load the autoloader *or* load the SMTP class as well. Please read the docs. – Synchro Oct 09 '14 at 06:07
  • As MaryJane noticed, that call to `setFrom` is wrong. Please don't post non-working code. – Synchro Oct 09 '14 at 06:09
  • @Synchro That was a copy paste from op question, of course this is working code. I changed the require file to `PHPMailerAutoload.php` if that makes you happy. – meda Oct 09 '14 at 12:25
  • @MaryJane you still need help with this ? – meda Oct 13 '14 at 02:47
  • @meda, Hi Meda, thank you for remembering me, I managed to make it work and send the email to me. But now I am facing another problem with the $mail->Body which I have asked in this link, I will be happy if you could continue helping me there http://stackoverflow.com/questions/26332108/issue-with-mail-body-in-the-phpmailer?noredirect=1#comment41328091_26332108 – Irfana Oct 13 '14 at 02:49
  • @MaryJane I posted an answer for you mary – meda Oct 13 '14 at 02:56
-1

If all you want to do is send an email why not use the PHP Mail command (http://php.net/manual/en/function.mail.php)

replace your code with this:

$to      = 'mj@gmail.com';
$subject = 'the subject';
$message = 'New bug report for the portal';
$headers = 'From: mj@gmail.com' . "\r\n" .
    'Reply-To: mj@gmail.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
John Rah
  • 1,757
  • 1
  • 14
  • 13
  • I am trying to learn how to run the SMPT and PHPMailer, I have used PHP Mail before., – Irfana Oct 09 '14 at 03:09
  • Because calling `mail()` directly is a recipe for disaster - the vast majority of code posted that uses it is either wrong, incomplete or vulnerable to attack. For example, *all* the example code on the PHP docs page for `mail` falls into this category. – Synchro Oct 09 '14 at 06:00