2015-09-14 81 views
1

我想送的时候,“发送电子邮件”按钮被按下电子邮件,但下面的代码将不会发送任何东西到我的邮箱:发送电子邮件与PHP

<!DOCTYPE html> 
<html> 
<form action="email.php" method="post"> 
<input value="Send Email" name="email" type="submit"> 
</form> 
<?php 
    if (isset($_POST['email'])) { 
    $msg = 'hello'; 
    mail('[email protected]', 'Sample', $msg); 
           }        
?> 
</html> 

任何想法如何让它起作用?

+0

你在本地或在您的网站的测试呢? – herriekrekel

+0

从本地主机上,我将它保存为email.php – Jack

+0

有些测试程序可用于测试是否发送电子邮件。之后,这个问题是不是在你的代码,并不得不问你的ISP,或使用中间件服务,如sendgrid;) – DDeme

回答

1

使用下面的PHP代码发送电子邮件

<?php 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($to, $subject, $message, $headers); 
?> 

邮件功能将无法在本地server.You工作需要配置您的本地服务器上的SMTP。看看这个类似的帖子,

php mail setup in xampp

+0

http://stackoverflow.com/questions/ 5335273/how-to-send-an-email-using-php –

+0

谢谢你,所以我猜这是行不通的,因为我在本地服务器上运行它。 – Jack

+1

是的,可能是这种情况...请阅读由娜娜或我在答案中共享的链接 –

0

试试这个

<?php 
     $to = "someemail.com" 
     $subject = "This is subject"; 

     $message = "<b>This is message.</b>"; 
     $message .= "<h1>This is headline.</h1>"; 

     $header = "From:[email protected] \r\n"; 

     $header .= "MIME-Version: 1.0\r\n"; 
     $header .= "Content-type: text/html\r\n"; 

     $retval = mail ($to,$subject,$message,$header); 

     if($retval == true) 
     { 
      echo "Message sent successfully..."; 
     } 
     else 
     { 
      echo "Message could not be sent..."; 
     } 
     ?>