Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple PHP Mail function not working on Amazon server EC2 [closed]

Tags:

php

amazon-ec2

Please see this code

<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?> 

what is the problem in this code . Its not working on Amazon Linux server

Thanks

like image 431
kuldeep raj Avatar asked Oct 24 '13 06:10

kuldeep raj


People also ask

Why is my mail PHP not working?

If it's still not working: change the sender ($sender) to a local email (use the same email as used for recipient). Upload the modified php file and retry. Contact your provider if it still does not work. Tell your provider that the standard php "mail()" function returns TRUE, but not mail will be sent.

Does PHP mail function work?

Unless you mean delivering mail to the webmaster, it doesn't make sense for the webmaster to configure things. You're asking the wrong question - there is nothing wrong with the mail() function in PHP - it's not unreliable. The problem is contents and distribution of your mails.

What is PHP mail () function?

The mail() function allows you to send emails directly from a script.


3 Answers

I had the same issue with ec2 for php mail.

Solution worked for me:

  1. install sendmail by command:

     sudo apt-get install sendmail
    
  2. check the service whether its started or not by execuring follwing command

    service sendmail status
    

    Note: Output of above command should be something - 'Active: active (running)'

  3. start the service if it is not running by following command

    service sendmail start
    
  4. After the service is started, send a test mail using following command:

    echo "This is test mail body" | mail -s "Test Mail Subject" "[email protected]"
    

Replace email with your email ID and see if you receive this email, if yes, then your mail setup is fine and now your php email should be working fine.

If not, file to check for errors is /var/spool/mail/.

like image 72
Avnish alok Avatar answered Oct 29 '22 21:10

Avnish alok


I'd had the same issue as you in sending an email using Php's mail(). Installing send mail solved it for me.

sudo apt-get install sendmail
like image 39
Sridhar Avatar answered Oct 29 '22 19:10

Sridhar


Amazon server allow sendmail functionality using localhost. Please uncomment mail configuration in php.ini.

I have already done this and working fine.

like image 38
Harish Singh Avatar answered Oct 29 '22 20:10

Harish Singh