在htmo方法发布的表单提交上使用php发送邮件

时间:2017-09-18 06:52:24

标签: php html email

我是新手,我正在尝试使用php发送邮件,但是当我点击提交按钮新窗口打开时,无法发送邮件,其中包含我的PHP代码的一部分......我想发送电子邮件来自用户电子邮件ID为我的电子邮件ID,内容为表单

这是我第一次使用php而且我陷入了这个

<?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 - &GT;行动= “HTTP:// myIPhere:7070 /项目名称/ sendemail.php”

请帮我解决这个问题

  

我正在使用tomcat服务器9.0

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

  

在我的webcontent中我添加了

     

class.smtp.php

     

class.phpmailer.php

     

class.PHPMailerAutoload.php

3 个答案:

答案 0 :(得分: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;
    }
?> 

这是示例代码..您可以从github下载smtp库函数....

答案 1 :(得分:0)

尝试此代码可以帮助您获取php邮件的基本代码。

你可以下载phpmailer     https://github.com/PHPMailer/PHPMailer/releases/tag/v5.2.21     并配置它。

<?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

使用它也可以正常工作

<?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>');
} ?>

答案 2 :(得分: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;
}

使用此配置并从GitHub下载PHPMailer。 https://github.com/PHPMailer/PHPMailer