如何使用PHP代码发送简单的电子邮件


How to send a simple email using PHP code

我想用一些简单的PHP代码向我的gmail帐户发送一封电子邮件。下面的代码在执行方面是有效的,然而问题甚至被认为是说"发送消息"我没有在我的gmail帐户中收到我的电子邮件。请告知

ini_set('SMTP',"smtp.gmail.com");
    $to ="example@gmail.com"; // this will be replaced with my actual email
    $from ="example@gmail.com"; // this will be replaced with senders email
    $message = $_GET['Message'];
    $subject = "This is a test";
    if(mail($to,$subject,$message,$from))
    {
        echo "Message Sent";
    }
    else
    {
         echo "Message Not Sent";
    }

发送简单电子邮件的步骤

  • 转到谷歌
  • 搜索"PHP邮件"
  • 单击第一个结果
  • 读,读,继续读,等等,读一遍,继续读
  • 享受吧

但说真的:

(示例取自PHP.net)

示例1

发送简单的电子邮件

使用mail()发送简单的电子邮件:

<?php
// The message
$message = "Line 1'r'nLine 2'r'nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "'r'n");
// Send
mail('caffeinated@example.com', 'My Subject', $message);
?>

示例2

发送带有额外邮件头的邮件。

添加基本标头,告诉MUA From和Reply To地址:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "'r'n" .
    'Reply-To: webmaster@example.com' . "'r'n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

使用他的代码行:

$to ="example@gmail.com"; // this will be replaced with my actual email
$from ="example@gmail.com"; // this will be replaced with senders email
$headers = "From: ".$from."'r'n";
$headers .= "Reply-To: ".$from."'r'n";
//$headers .= "CC: susan@example.com'r'n";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
$message = $_GET['Message'];
$subject = "This is a test";
mail($to, $subject, $message, $headers);