1

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("[email protected]", "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 = [email protected]

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

2
  • use phpmailer , google it Commented Apr 7, 2014 at 13:14
  • @PoomrokcThe3years Thanks for your quick reply. I googled it. But can I not make the changes in the PHP.ini file and use the mail function? Commented Apr 7, 2014 at 13:16

2 Answers 2

2

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. Folder containg 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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks! I have uploaded all the files in phpmailer folder to my WAMP folder's www directory. I checked the examples and found them too complicated. Can you please give me a simple example? I don't want to make an email form. I just want to test php mail and send a sample mail to my own gmail account. Thanks! Looking forward to your exmaple!
Just use the "Simple Example" in the readme (or on the github page) and remove all the lines that don't apply to your situation, such as the lines with ReplyTo, CC, attachments etc. You'll see that it's just a matter of telling what SMTP to use, and what message to send to whom. How much easier can it get? :-)
Are you in hurry? I'm using my Ipad and it may not easy to write example , but I will do as much as I could
See here for a very simple minimal example! phpmailer.worxware.com/index.php?pg=tutorial#2
Thanks! I'll try it and let you know! :-)
|
1

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.