-1

I have a very strange error. It says that my PHP file ends when it is unexpected. I do not see any errors in my code. Anyway it is sample code from tutorial of PHPMailer, so it should not be not working. Can somebody explain? My code:

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

$mail = new PHPMailer;                              // Passing `true` enables exceptions
try {
    //Server settings

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.comcast.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'hmmmm@gmail.com';                 // SMTP username
    $mail->Password = 'Password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->From = 'hmmm@gmail.com';
    $mail->FromName = 'Michael';
    $mail->addAddress('adress@gmail.com');     // Add a recipient
    $mail->addAddress('adress2.19@gmail.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
//  $mail->WordWrap = 50
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->send()

?>
  • 1
    you use `try {` and where is closed bracket `}` and also at the end of code you missing `;` – Bilal Ahmed Aug 10 '18 at 09:26
  • Welcome to stack overflow, I've answered your question, incase that helps, please mark the answer as correct by clicking the tickmark on the left of the answer. – Sujit Agarwal Aug 10 '18 at 09:29
  • MISSING SEMI COLON. The pain we all face. You'll get used to it the more times you miss one! – Adam Aug 10 '18 at 09:30

1 Answers1

2

You've missed a ; semicolon on the last line -

$mail->send();

Also, you need to end the try block with a } and a catch block

So your code would become -

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

$mail = new PHPMailer;                              // Passing `true` enables exceptions
try {
    //Server settings

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.comcast.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'hmmmm@gmail.com';                 // SMTP username
    $mail->Password = 'Password';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->From = 'hmmm@gmail.com';
    $mail->FromName = 'Michael';
    $mail->addAddress('adress@gmail.com');     // Add a recipient
    $mail->addAddress('adress2.19@gmail.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
//  $mail->WordWrap = 50
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    $mail->send();
   }
   catch(Exception $e)
   {
      var_dump($e);
   }

?>
Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79