简体   繁体   中英

Email function not working on linux(vps) server

I am testing email functionality on a linux (vps) server. This is the following code which i am using:

<?php
  // --- CONFIG PARAMETERS --- //
  //
  $email_recipient = "recipient@demo.com";
  $email_sender = "Sender Name";
  $email_return_to = "sender@demo.com";
  $email_content_type = "text/html; charset=us-ascii";
  $email_client = "PHP/" . phpversion();
 //
 // ------------------------- //

// --- DEFINE HEADERS --- //
//
$email_header = "From: " . $email_sender . "\r\n";
$email_header .= "Reply-To: " . $email_return_to . "\r\n";
$email_header .= "Return-Path: " . $email_return_to . "\r\n";
$email_header .= "Content-type: " . $email_content_type . "\r\n";
$email_header .= "X-Mailer: " . $email_client . "\r\n";
//
// ---------------------- //

// --- SUBJECT AND CONTENTS --- //
//
$email_subject = "Test email subject";
$email_contents = "<html>";
$email_contents .= "<h2>Test Email</h2>";
$email_contents .= "<br><b>Sender: " . $email_sender;
$email_contents .= "<br><b>Recipient: " . $email_recipient;
$email_contents .= "</html>";
//
// ---------------------------- //

$email_result = mail($email_recipient, $email_subject, $email_contents, $email_header);
if ($email_result) echo "Email has been sent!";
else echo "Email has failed!";
?>

When i execute the code it takes long time to process and then displays Email has been sent. But the email is never delivered to the recipient. I have checked the send email path in the php.ini file through phpinfo() function it displays :

 sendmail_from  no value    no value
 sendmail_path  /usr/sbin/sendmail -t -i    /usr/sbin/sendmail -t -i

I am unable to trace out the reason for this.

Please help me on this.

Thanks

Pankaj

Use an actual email address as $email_sender; if not working, write 5th parameter in mail() function -fsenderemail@mail.com (whatever sender e-mail is)

Have you tried sending different recipients? Did you check the spambox? I suggest using PHPMailer, it takes care of headers, etc, makes life easier.

make sure your sendmail service is running, sometimes you need start it manually for your VPS. it should be something like:


/etc/init.d/sendmail start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM