dousong3760 2016-11-13 06:05
浏览 58

在Centos 6.4和Nginx服务器上使用PHP发送电子邮件[重复]

This question already has an answer here:

Please do not mark this as a duplicate issue. I have tried the recommended steps (checking error logs, checking syntax, header info.. etc..) to troubleshoot which the mod has pointed me to and all those steps did not work. I am receiving a PERMISSION issue and I can not seem to figure out why. The error I get is:

"Nov 13 14:19:08 centos64 sendmail[6611]: uADJJ80J006611: SYSERR(nginx): queueup: cannot create queue file ./qfuADJJ80J006611, euid=498, fd=-1, fp=0x0: Permission denied ~"

Original Message:

Would like some help with sending email using PHP on a nginx centOS 6.4 server. I am new to this so want to first verify if my current setup is correct and diagnose what the exact issue is before I start tweaking config files.

Goal: I would like to be able to send all the users in my SQL database email alerts, etc.. So eventually if I have a large user-base I could be sending a lot of emails..

In my php.ini file I see..

sendmail_path = /usr/sbin/sendmail -t -i

And I have used command

/etc/init.d/postfix status

to verify that postfix is installed and is also up to date (yum -y install postfix, or whatever it was)

I was looking at this helpful thread ( http://www.webhostingtalk.com/showthread.php?t=1238442 ) and also looking to follow this guide ( http://www.server-world.info/en/note?os=CentOS_6&p=mail ) but again, didnt want to start messing with stuff until I had guidance.

I tried to send mail using the following code, and simply visited the email.php page on my server and did not get my test email. The test email was attempted to be sent from and to a gmail email address.

<?php $to      = 'GMAIL_EMAIL@gmail.com';
$subject = 'Testing';
$message = 'Hello';
$headers = 'From: GMAIL_EMAIL@gmail.com' . "
" .
'Reply-To: GMAIL_EMAIL@gmail.com' . "
" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

As usual, any and all help is appreciated. Thanks!

SOLUTION:

  1. I used commands

sudo setsebool -P httpd_can_sendmail 1

sudo setsebool -P httpd_can_network_connect 1

And then the messages were sitting in the clientmqueue folder not being sent, so I looked at the /var/log/maillog and saw a message that said.. 'centos64 postfix/smtp[22..7]: connect to gmail-smtp-in.l.google.com[....]:25: Network is unreachable' so I googled the error message and read that I needed to do install of cyrus plain..

yum install cyrus-sasl-plain

Hope this helps someone else out there in the same boat.

</div>
  • 写回答

1条回答 默认 最新

  • douai2499 2016-11-13 17:39
    关注

    I am giving some easy options, which probably cover all possible situations.

    What you were following

    Read an old answer for the way you were following -- PHP mail function not working on Centos server

    Self Hosted with PHP Libraries

    This is Github repo of PHPMailer with example -- https://github.com/PHPMailer/PHPMailer Second option for self hosted email http://swiftmailer.org is quite known. Follow their basic guide and use this snippet :

    <?php
    require_once 'swift/lib/swift_required.php';
    
    $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
      ->setUsername('GMAIL_USERNAME')
      ->setPassword('GMAIL_PASSWORD');
    
    $mailer = Swift_Mailer::newInstance($transport);
    
    $message = Swift_Message::newInstance('Example Subject')
      ->setFrom(array('some@some-mail.com' => 'Example'))
      ->setTo(array('abc@another-mail.com'))
      ->setBody('This is an Example Mail.');
    
    $result = $mailer->send($message);
    ?>
    

    Other ways for self hosted is using Pear Mail.

    Transactional Email Services

    Instead of self hosting mail server, you can use services like SendGrid with https://sendgrid.com/pricing/ to send email over HTTP (we can close mail ports to increase security ). There are many such services. They are exactly like swiftmailer.org you need a PHP library and PHP snippet but they are hosting the email, not your server -- https://github.com/sendgrid/sendgrid-php

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题