简体   繁体   中英

Mail function not working - PHP

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. 包含phpmailer的文件夹

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.

Have a look at this page:

http://www.php.net/manual/en/mail.configuration.php

Short version: to make your php.mail function work, you need to figure out how to send an e-mail using your server and make sure your configuration in php.ini is up to date. (For details, see the link above).

  • Does your server run sendmail or another mail agent? Or doesn't it run anything like that at all?
  • If so, make sure the paths are set correctly.
  • If not, use a third party SMTP service. GMail offers it, but the problem is that it requires authentication, which php.mail() doesn't support. If you still want to use it, see this post for alternatives to php.mail() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM