Simple PHP Mail function not working on Amazon ser

2019-06-18 05:28发布

Please see this code

<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$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

6条回答
虎瘦雄心在
2楼-- · 2019-06-18 05:49

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" "recipient@email.com"
    

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/.

查看更多
来,给爷笑一个
3楼-- · 2019-06-18 05:52

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

I have already done this and working fine.

查看更多
Summer. ? 凉城
4楼-- · 2019-06-18 05:58

Apparently sending PHP from EC2 is not possible until it gets unlocked by support. To unlock it visit: EC2 - Fresh PHP install - Mail not working

Otherwise, i will just quote @Charles answer:

This won't directly solve your issue (edit: I mean the error message you have now edited out), but Amazon EC2 instances have a really spotty mail reputation. You're probably going to have deliverability issues.

Thankfully Amazon created the Simple Email Service to go along with EC2, with a free level of service for EC2 customers. The API is pretty simple and there are transport adapters for many excellent PHP mailing libraries, like SwiftMailer (transport).

So - no you can't send, but use Simple Email Service, libraries like SwiftMailer, or I would add services like SendGrid

*Source: Another SO question on php mail & EC2

MEANWHILE: I signed up for Simple Email Service, which is somewhat what SendGrid is doing. It's was very simple. First you request smtp user and password. You then either confirm an email for testing, or request production access [I didn't need that so I haven't gone through process]. And one caveat - use ssl:// in front of the host, or it will give you SMTP error: 530 5.7.0 Must issue a STARTTLS command first. error. I was using CakePHP and EC2 when this happened.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-06-18 06:00

Just follow instructions below (Tested for ubuntu 10x EC2)

sudo apt-get install php-pear   // Install php-pear if not installed 

sudo pear install mail  

sudo pear install Net_SMTP

sudo pear install Auth_SASL

sudo pear install mail_mime

sudo apt-get install postfix

sudo service apache2 restart

Enjoy!

查看更多
太酷不给撩
6楼-- · 2019-06-18 06:07

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
查看更多
在下西门庆
7楼-- · 2019-06-18 06:12

are you willing to use other mailing framework? If yes, you can use phpmailer...it works fine for me.

here is the link on their site. http://phpmailer.worxware.com/

查看更多
登录 后发表回答