用PHP发送电子邮件

时间:2013-08-13 19:19:26

标签: php email sendmail

我几乎尝试了一切。我只想发送电子邮件。

我的第一个代码:

<?php
$to = "yourgmail@gmail.com";
$subject = "HELLO";
$body = "HI!";
try {
    mail($to, $subject, $body);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

它不起作用。没有错误信息,无法正常工作。

我的第二个代码:

function send_mail('mygmail@gmail.com','yourgmail@gmail.com','HELLO','HI!')
{
    $headers = '';
    $headers .= "From: $from\n";
    $headers .= "Reply-to: $from\n";
    $headers .= "Return-Path: $from\n";
    $headers .= "Message-ID: <" . md5(uniqid(time())) . "@" . $_SERVER['SERVER_NAME'] . ">\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Date: " . date('r', time()) . "\n";

    mail($to,$subject,$body,$headers);
}

它也不起作用。

请帮帮我:)。

1 个答案:

答案 0 :(得分:0)

这有用吗?

第一部分:

<?php 

$to = "yourgmail@gmail.com"; 
$subject = "HELLO"; 
$body = "HI!"; 
$from = "mygmail@gmail.com";

send_mail($to,$from,$subject,$body);

?>

第二部分:

<?php

function send_mail($to,$from,$subject,$body) { 

  $headers = ''; 
  $headers .= "From: $from\n"; 
  $headers .= "Reply-to: $from\n"; 
  $headers .= "Return-Path: $from\n"; 
  $headers .= "Message-ID: <" . md5(uniqid(time())) . "@" . $_SERVER['SERVER_NAME'] . ">\n"; 
  $headers .= "MIME-Version: 1.0\n"; $headers .= "Date: " . date('r', time()) . "\n";

  mail($to,$subject,$body,$headers);

}

?>

我不确定你是否可以使用php mail函数通过你的Gmail发送邮件,从未查看过,这应该适用于name@yourdomain.com