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

3 Answers3

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
  • 108
  • 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

Try this code helps you its basic code for php mail.

you can download phpmailer https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.21 and config it.

<?php
    /*https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.21*/
    require('phpmailer/PHPMailerAutoload.php');
    $mail = new PHPMailer();
    $subject = "Test Mail using PHP mailer";
    $content = "<b>This is a test mail using PHP mailer class.</b>";
    $email = "your@gmail.com";
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;
    $mail->SMTPAuth = TRUE;
    $mail->SMTPSecure = "tls";
    $mail->Port     = 25; //587;  
    $mail->Username = "";                   //smtp username        
    $mail->Password = "";                           //smtp Password
    $mail->Host     = "smtp.sendgrid.net";                  //Host server
    $mail->Mailer   = "smtp";                               //mail through
    $mail->SetFrom("yourmail id", "karthikeyan"); //From
    $mail->AddReplyTo("replay mail id", "karthikeyan");      //Replay To
    $mail->AddAddress($email,"TEST");   // To   
    $mail->Subject = $subject;                                      //Subject   
    $mail->WordWrap   = 80;
    $mail->MsgHTML($content);
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
    $mail->IsHTML(true);
    if(!$mail->Send()){
        //echo "<pre>"; print_r($mail->ErrorInfo); //php error debuging         
    } 
    else {
    echo "Mail sent";
    }
    ?>


Use this format


-Mail \\Main Folder
    -phpemail.php \\File    
    -phpMailer \\Folder(library file)
        -PHPMailerAutoload.php
        -etc.. files

Use this one its also working

<?php
// Pear Mail Library -> https://github.com/pear/Mail
// http://fr.apkhere.com/app/com.netease.mail

require_once ('Mail-master/Mail.php');

$from = '<yourmail@gmail.com>';
$to = '<yourmail@gmail.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'smtp.sendgrid.net',
        'port' => '25',
        'auth' => true,
        'username' => 'smtp usename',
        'password' => 'smtp password'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
} ?>
karthikeyan
  • 172
  • 1
  • 9
  • still i'm not able to send mail......pleasure see my question which i have edited with the current code will provide you better documentation. – tom Sep 18 '17 at 11:17
  • i have updated the code – karthikeyan Sep 18 '17 at 13:26
  • please download the https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.21 link and use this require('phpmailer/PHPMailerAutoload.php'); you can send email dude. – karthikeyan Sep 18 '17 at 13:27
  • First of all you must know the folder structure should be like this - Folder //Folder - Phpmailer // Folder - PHPMailerAutoload.php //File - email.php //File – karthikeyan Sep 18 '17 at 13:33
  • https://i.stack.imgur.com/4F1EF.png plzz check this link....i think sever is unable to find the file sendemail.php – tom Sep 19 '17 at 06:41
  • I use apache server this coding is working fine i didn't use tomcat server – karthikeyan Sep 19 '17 at 08:03
  • change the folder sturcuture and try again or use apache server its perfect for php – karthikeyan Sep 19 '17 at 08:05
  • https://i.stack.imgur.com/QgnPM.png see this one .....now my entire code got print on next page .......i think my php code is not executing ......i don't know why....so, that problem is because of server!! right – tom Sep 19 '17 at 08:26
  • phpMailer is a library put all library files in phpmailer(folder) and use Like this --------- require('phpmailer/PHPMailerAutoload.php'); not like this ------------- require 'PHPMailerAutoload.php'; place your sendemail.php out side the phpmailer folder and once again try this otherwise your server is a problem. – karthikeyan Sep 19 '17 at 09:13
  • Will you please use my code add your smtp user name and password just check it – karthikeyan Sep 19 '17 at 09:28
  • okay! let me try your code.........after that i will tell you the results – tom Sep 19 '17 at 09:34
  • i put phpmailer folder in webcontent->web-inf->lib->phpmailer........and run my code......but i got negative results....the hole code got print like the previous one case – tom Sep 19 '17 at 09:48
  • I have attached another code try this pear Library... – karthikeyan Sep 19 '17 at 10:05
  • thanku for your precious time......appreciate you for your help..........but still didn't work....still the same problem ;( – tom Sep 19 '17 at 10:18
  • Its ok use apache or xampp server and try it.... – karthikeyan Sep 19 '17 at 10:20
  • i setup apache httpd server.......and run my project but still i'm unable to send email......same result occur(new page open with php code) – tom Sep 19 '17 at 11:57
  • xampp is perfect......the only problem is server as i setup xampp....my send email code run perfectly and i'm able to send mail...........thanku for your help :) – tom Sep 20 '17 at 05:28
  • Super tom i appreciate your effort... – karthikeyan Sep 20 '17 at 05:35
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
  • 502
  • 4
  • 10