从本地服务器发送带有PHP的电子邮件

时间:2015-10-15 12:24:45

标签: php

我已经在我的ubuntu上安装了postfix,并编写了以下代码,当按下按钮时我需要从本地服务器发送电子邮件(我需要从本地服务器发送):

<!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, this is an email from  GUI ';
mail('mm@gmail.com','Sample',$msg); }
?>                 
</html>

它对gmail完全正常,我按下按钮时会收到一封电子邮件。但是,当我将电子邮件地址更改为我的Thunderbird邮件(工作邮件)时,我什么都没得到。  这是我在mail.log中获得的:

postfix / qmgr [1304]:3C93B1E1B47:已删除

但是当我发送到Gmail时,我会收到mail.log

2 个答案:

答案 0 :(得分:3)

您需要使用php mailer才能使其正常工作

您必须拥有具有特权的电子邮件地址。就像你主持电子邮件......

下载phpmailer并将其添加到您的代码中,以便您可以发送邮件.. 这是我使用的一些引用代码...

<?php

require("PHPMailer/class.phpmailer.php");
require("PHPMailer/PHPMailerAutoload.php");

define("MAILHOST",'hostsite  ');
define("MAILSMTPAuth",true);
define("MAILUsername",'hosting mail');
define("MAILPassword",'password');
define("MAILSMTPSecure",'ssl');
define("MAILPort",portno);
define("MAILFrom",'hosting mail');
    $mail = new phpmailer();
    $result = array();

    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = MAILHOST;  // Specify main and backup SMTP servers
    $mail->SMTPAuth = MAILSMTPAuth;                               // Enable SMTP authentication
    $mail->Username = MAILUsername;                 // SMTP username
    $mail->Password = MAILPassword;                           // SMTP password
    $mail->SMTPSecure = MAILSMTPSecure;                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = MAILPort;                                    // TCP port to connect to

    $mail->From = MAILUsername;
    $mail->FromName = 'Name';
    $mail->isHTML(true); // Set email format to HTML

    $mail->Subject = " Inquiry Form";

    $mail->Body = "message";           


    $mail->SetFrom('hosting mail address', 'name');
    $mail->addAddress('recieving mail address', 'Name');     // Add a recipient admin


        }
    exit;
    ?> 

答案 1 :(得分:0)

试试phpmailer,看看它是否适合您。

您可以在此处获取phpmailer:https://www.apachefriends.org/download.html

将文件夹添加到工作目录并在代码中设置路径。

这是我的工作代码

<?php 
    error_reporting(E_ALL);
    require("PHPMailer/class.phpmailer.php"); //path of phpmailer
    $mail = new PHPMailer();
    $mail->IsSMTP(); // set mailer to use SMTP
    $mail->SMTPDebug  = 2; 
    $mail->From = "mygmail@gmail.com";
    $mail->FromName = "Gori";
    $mail->Host = "smtp.gmail.com"; // specif smtp server
    $mail->SMTPSecure= "ssl"; // Used instead of TLS when only POP mail is selected
    $mail->Port = 465; // Used instead of 587 when only POP mail is selected
    $mail->SMTPAuth = true;
    $mail->Username = "mygmail@gmail.com"; // SMTP username
    $mail->Password = "mypw"; // SMTP password
    $mail->AddAddress("another@gmail.com", "Henry"); //replace myname and mypassword to yours
    $mail->AddReplyTo("mygmail@gmail.com", "Gori");
    $mail->WordWrap = 50; // set word wrap
    //$mail->AddAttachment("c:\\temp\\js-bak.sql"); // add attachments
    //$mail->AddAttachment("c:/temp/11-10-00.zip");

    $mail->IsHTML(true); // set email format to HTML
    $mail->Subject = 'test';
    $mail->Body = 'test';

    if($mail->Send()) {echo "Send mail successfully";}
    else {echo "Send mail fail";} 
?>