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("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!

Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63

2 Answers2

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.

Poomrokc The 3years
  • 1,099
  • 2
  • 10
  • 23
  • 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! – Chinmay Dabke Apr 07 '14 at 13:36
  • 1
    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? :-) – Pelle Apr 07 '14 at 13:43
  • 1
    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 – Poomrokc The 3years Apr 07 '14 at 13:44
  • 1
    See here for a very simple minimal example! http://phpmailer.worxware.com/index.php?pg=tutorial#2 – Pelle Apr 07 '14 at 13:46
  • Thanks! I'll try it and let you know! :-) – Chinmay Dabke Apr 07 '14 at 13:57
  • @DroidFan you could see my edit in a few moments. I will show you my sending email code – Poomrokc The 3years Apr 07 '14 at 14:11
  • I tried it but I'm getting two errors. 1) class `PHPMailerAutoload.php` could not be found. Solution : I changed it to `class.phpmailer.php` and hopefullly its working. 2) The variable `send_using_gmail` was not defined. So I removed the if statement in which it was used. Now the problem is that its still not working :(. It shows the message "Unable to process your request now, please try again later". Please help me solve this. thanks! – Chinmay Dabke Apr 07 '14 at 15:03
  • https://github.com/PHPMailer/PHPMailer Look , you can see PHPMailerAutoload.php. Have you download it yet? Looks like you get the incomplete phpmailer. and don't remove the send_using_gmail variable. The only problem is you don't have **PHPMailerAutoload.php** Download it and save in the same directory as your php. https://github.com/PHPMailer/PHPMailer/blob/master/PHPMailerAutoload.php the file link – Poomrokc The 3years Apr 07 '14 at 15:08
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().
Community
  • 1
  • 1
Pelle
  • 6,423
  • 4
  • 33
  • 50