0

I am newbie and i'm trying to send mail using php but unable to send mail when i click on submit button new window open which contain some part of my php code......i want to send email from user email id to my email id with the content of form

This is the first time i'm using php and i stuck in this

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

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "example@gmail.com";
$mail->Password = "example";
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = "example@gmail.com";
$mail->FromName = "ADMIN";

$mail->addAddress("example@gmail.com", "User 1");
$mail->IsHTML(true);

$mail->Subject = "form Submission";

$subject = $_POST['subject'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$name = $_POST['name'];
$company = $_POST['company'];
$city = $_POST['city'];
$message = $_POST['message'];

$mail->Body = "
<html>
<h2><br>Name: $name</br>
<br> Email: $email</br>
<br> Phone: $phone</br>
<br> Subject: $subject</br>
<br> Company: $company</br>
<br> City: $city</br>
<br> Message: $message </br></h2>
</html>";
$mail->AltBody = '';

if (! $mail->Send())
echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
else
echo "Message has been sent";

?>

html--> action="http://myIPhere:7070/projectname/sendemail.php"

please help me to resolve this problem

i'm using tomcat server 9.0

my entire php code got print i think php code is not executing

in my webcontent i added

class.smtp.php

class.phpmailer.php

class.PHPMailerAutoload.php

tom
  • 11
  • 6

2 Answers2

0
<?php

include "PHPMailer_5.2.4/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 = "test@gmail.com";
$mail->Password = "test@12#";
$mail->AddReplyTo("test@gmail.com", "test mail");
$mail->AddAddress('test@gmail.com', 'test mail ');
$mail->Body    = "message:hiii";

if ($mail->send())
    //if (mail($subject,$message, $headers))
   {
    echo "Thank you for sending mail us!";
    } else {
    echo "Mailed Error: " . $mail->ErrorInfo;
    }
?> 

this is the sample code..you can download smtp library function from github....

Mahesh
  • 93
  • 9
  • https://github.com/shalinshah1993/ImPatho/tree/master/server%20code/Ic2014/PHPMailer_5.2.4 – Mahesh Sep 18 '17 at 07:13
  • from this link you can download php mailer library – Mahesh Sep 18 '17 at 07:13
  • thanku ......but in this you are using a static mail id to send mail – tom Sep 18 '17 at 07:14
  • yes,for username and password we have to use statis mail id(gmail account)....To mail id we can add dynamically..... – Mahesh Sep 18 '17 at 07:17
  • https://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script please check this link...... – tom Sep 18 '17 at 07:23
  • that code will only work in live server...it will not work in localhost...if you're working in live sever,no need to use SMTP mailer...normal php mail function will work there.. – Mahesh Sep 18 '17 at 07:26
  • okay...but they didn't provide user id and password......that was confusing me – tom Sep 18 '17 at 07:32
  • yes tom...in live host no need to mention anything...but in localhost we have to give our mail id and password for mail authentication....then only mail function will work... – Mahesh Sep 18 '17 at 07:36
  • but for authentication we always required a user id and password.......sorry if this is a silly question but i'm confused – tom Sep 18 '17 at 07:46
  • while configuring your live host definitely you have to configure your email account...but in localhost you just download and install xampp or wamp...so that is the difference...in live host automatically takes your email configurations...but in localhost we have to use SMTP configuration with our email account... – Mahesh Sep 18 '17 at 08:11
  • still getiing error --> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. – tom Sep 18 '17 at 10:56
  • see, i just edited my question. – tom Sep 18 '17 at 11:14
0
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers (smtp.gmail.com if you can use Gmail Account)
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@gmail.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('from@example.com', 'Mailer');
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments
    $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();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

Use this configuration and download PHPMailer from GitHub. https://github.com/PHPMailer/PHPMailer

Marcos Riveros
  • 534
  • 4
  • 10