邮件功能不起作用 - PHP [英] Mail function not working - PHP

查看:149
本文介绍了邮件功能不起作用 - PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习PHP,我正在尝试使用 mail()函数将测试邮件发送到我的Gmail帐户。



这是我的代码:

  $ message =Hello PHP !; 
邮件(mygmailaccount@gmail.com,PHP测试,$消息);

但它不工作。这是错误的样子:



点击 here 在网页上查看。





我看到很多类似的问题,我也尝试过SO提到的所有解决方案。

我已经修改了我的 php.ini 文件如下所示。

  [邮件功能] 
;仅适用于Win32
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

;仅适用于Win32
; http://php.net/sendmail-from
sendmail_from = mygmailaccount@gmail.com

我在Windows 7(64位)机器上使用WAMP服务器。

请帮我解决这个问题。谢谢!

解决方案

https://github.com/PHPMailer/PHPMailer



这是官方的PHPMailer。



<所有你需要做的是将所有的文件和文件夹上传到一个文件夹与邮寄php文件(你的代码显示的文件)。然后看一下示例文件夹,或者查看示例文件夹,如果你需要smtp.You应该已经弄清楚了,看起来你有很好的编程技能。如果你有问题,评论通知我。



PS。 mail()函数有时不可靠。我经常遇到这个问题,直到我为自己的系统使用phpmailer。



编辑:完全像这样。 send.php是我要写的跟踪代码的文件。



接下来, send.php代码!!

 <?php 
require'PHPMailerAutoload.php';
$ sendto =destinationemail; //输入您自己的
$ sendfrom =yourmaskedmail; //输入您自己的
$ topic =test;
$ passage =test;
$ mail = new PHPMailer(true);

//使用gmail发送邮件
if($ send_using_gmail){
$ mail-> IsSMTP(); //告诉类使用SMTP
$ mail-> SMTPAuth = true; //启用SMTP验证
$ mail-> SMTPSecure =ssl; //将前缀设置为服务器
$ mail-> Host =smtp.gmail.com; //将GMAIL设置为SMTP服务器
$ mail-> Port = 465; //设置GMAIL服务器的SMTP端口
$ mail-> Username =* hidden *; // GMAIL username输入您自己的
$ mail-> Password =* hidden; // GMAIL password输入您自己的
}

//典型的邮件数据
$ mail-> AddAddress($ sendto,$ sendto);
$ mail-> SetFrom($ sendfrom,$ sendfrom);
$ mail-> Subject = $ topic;
$ mail-> Body = $ passage;

try {
$ mail-> Send();
echoSuccess!该电子邮件已发送至$ sendto。 以。
} catch(异常$ e){
//某事坏了
echo现在无法处理您的请求,请稍后重试;
}
?>

更改邮件,邮件和邮件的内容。另外,输入您的Gmail用户名和密码。之后,运行php,等待你的电子邮件来。我刚刚在8分钟前测试它,它的工作。


I am new to PHP and just started learning it. I am trying to send a test mail to my gmail account using the mail() function.

Here is my code:

$message = "Hello PHP!";
mail("mygmailaccount@gmail.com", "PHP Test", $message);

But its not working. This is what the error looks like:

Click here to view it in a webpage.


I have seen lots of similar questions and I have also tried all the solutions mentioned on SO.
I have made changes to my php.ini file as shown below.

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = mygmailaccount@gmail.com

I am using WAMP server on a Windows 7 (64-bit) machine.
Please help me solve this. Thanks!

解决方案

https://github.com/PHPMailer/PHPMailer

This is the official PHPMailer.

all you have to do is upload all the files and folders to a folder with the mailing php file(the file which have the code shown by you). Then look at the example folder or look at the example folders if you need smtp.You should have figured it out , looks like you got good programming skill.If you have problem , comment to informed me.

PS. mail() function sometimes not reliable. I face that problem often until I use phpmailer for my own system.

EDIT: Put it altogether like this. send.php is the file I'm going to write the followling code.

Next , the send.php code!!

    <?php
    require 'PHPMailerAutoload.php';
    $sendto ="destinationemail";//Input your own
    $sendfrom ="yourmaskedmail";//input your own
    $topic ="test";
    $passage ="test";
    $mail = new PHPMailer(true);

    //Send mail using gmail
    if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "*hidden*"; // GMAIL username Input your own
$mail->Password = "*hidden"; // GMAIL password  Input your own
    }

//Typical mail data
$mail->AddAddress($sendto,$sendto);
$mail->SetFrom($sendfrom,$sendfrom);
$mail->Subject =$topic;
$mail->Body =$passage;

try{
$mail->Send();
echo "Success! This email has been sent to ".$sendto. " in the name of ".$sendfrom;
} catch(Exception $e){
//Something went bad
echo "Unable to process your request now, please try again later";
}
?>

Change the mailsender,mailreciever and contents of the mail. Also , input your gmail username and password. After that, run the php, wait for your email to come. I've just test it 8 minutes ago and it worked.

这篇关于邮件功能不起作用 - PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆