繁体   English   中英

PHP脚本发送电子邮件不起作用

[英]Php script to sending email doesn't work

我在页面上创建了一个简单表单,现在尝试添加php脚本来发送电子邮件。 不幸的是,它不起作用。 单击该按钮后,我希望用户保持我的立场而无需重定向。

mail_sender.php

<?php 
if(isset($_POST['submit'])){
   $to = "someone@gmail.com"; 
   $from = $_POST['email']; 

   $message = " You received the fallowing message:" . "\n\n" . $_POST['message'];


   mail($to,$message,$from);
   echo "Mail Sent. Thank you, we will contact you shortly.";
}
?>

的HTML

<form action="mail_sender.php" method="post">
    <textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
    <textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
    <input type="submit" id="submit" value="Send">
</form>

首先,您的“提交”按钮中缺少“名称”属性。 和PHP邮件功能是错误的。

它应该是:

$subject = "Your subject";
$headers = "From: $from ";   
mail($to,$subject,$message,$headers);

代替:

mail($to,$message,$from);

PHP的默认mail()函数在大多数情况下不起作用,尤其是对于GMail。 这是因为您的电子邮件需要以一种特殊的方式进行格式化才能被Google的邮件服务器使用。 使用类似PHPMailer的邮件库会更好。

这是使用PHPMailer从GMail帐户发送电子邮件的方法。

    $mail = new PHPMailer();

    // ---------- adjust these lines ---------------------------------------
    $mail->Username = "xxx@gmail.com"; // your GMail user name
    $mail->Password = "passwd";  // your GMail Password
    $mail->AddAddress("yyy@gmail.com"); // recipients email
    $mail->FromName = "Your Name"; // readable name

    $mail->Subject = "Subject";
    $mail->Body    = "Body"; 

    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; // or 587
    $mail->IsSMTP(); // use SMTP
    $mail->SMTPAuth = true; // turn on SMTP authentication
    $mail->From = $mail->Username;

    //----------------------------------------------------------------------

    if(!$mail->Send())
    {
        echo "mail sent";
    }

我尝试了所有操作,现在收到消息SMTP错误:无法连接到服务器,并且SMTP connect()失败

        <form action="mail_sender.php" method="post">
        <textarea id="email" name="email" rows="1" cols="30" placeholder="Type your email"></textarea>
        <textarea id="formContent" name="message" rows="6" cols="30" placeholder="Type your message"></textarea>
        <input type="submit" name="submit" id="submit" value="Send">
    </form>

的PHP

<?php 
require "PHPMailer-master/PHPMailerAutoload.php";

$mail = new PHPMailer();

$to = "someone@gmail.com"; // required
$from = $_POST['email']; 

$comment = 
'Email: '.$from.' <br> />
Message: '.$_POST['message'].' <br> />'
;


$mail->Username = "someone@gmail.com"; // your GMail user name
$mail->Password = "Password";  // your GMail Password
$mail->AddAddress("someone@gmail.com"); // recipients email
$mail->setFrom($_POST['email']);
$mail->Body  = 'This is the HTML message body <b>in bold!</b>';
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->SMTPDebug = 1;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Subject = 'Here is the subject';

//----------------------------------------------------------------------

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
 }    else {
echo 'Message has been sent';
  }
  ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM