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

时间:2015-12-02 12:59:42

标签: php

我想使用一些简单的PHP代码向我的Gmail帐户发送电子邮件。下面的代码在执行方面起作用,但问题甚至被认为是" Message Sent"我没有在我的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";
    }

2 个答案:

答案 0 :(得分:1)

发送简单电子邮件的步骤

  • 转到Google
  • 搜索" PHP Mail"
  • 点击第一个结果
  • 阅读,阅读,继续阅读,等待,阅读,阅读
  • 享受!

但严肃地说:

(示例来自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);
?>

答案 1 :(得分:0)

使用他的代码行:

$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);