0

I have developed a website and want to send emails from the website domain I have configured mail function before it worked well then but after few months its not working here is my code

$email_from = 'xxx@domainname.lk';//<== update the email address
$email_subject = "New Visitor";
$email_body = "new visitor".

$to = 'qqq@gmail.com';//<== update the email address
$headers .= "From: $email_from \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
zero323
  • 322,348
  • 103
  • 959
  • 935
  • does it give you an error? – Nelson Owalo Nov 12 '18 at 11:02
  • goto cpanel and click on webmail and open horde or roundcube or squirreMail (click on any one of these) and send email manually. if email send then error in your code otherwise contact your host support.. – Bilal Ahmed Nov 12 '18 at 11:06
  • no @NelsonOwalo code is runnig correctly and bottom line html tags also run successfully – Nayana Lakshitha Nov 12 '18 at 11:07
  • @BilalAhmed email is sending by manually but not sending by code – Nayana Lakshitha Nov 12 '18 at 11:09
  • Talk to your hosting provider, sometimes they block email sending or maybe Their servers got reported as spammers, I suggest to use PHPMailer it gives more control on how to send email including authentication, also allows you to use smtp protocol and send email directly from a real account which include some security details added to the email message headers. – Jonnathan Q Nov 12 '18 at 11:11
  • after `mail()` do a `print_r(error_get_last());` and add what that gives – Rotimi Nov 12 '18 at 11:11
  • 2
    Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Rotimi Nov 12 '18 at 11:13
  • @JonnathanQ i have talked to service providers and they sent me an email with new configured settings but im new to php therefore cant find wht to change here is what they sent – Nayana Lakshitha Nov 12 '18 at 11:17
  • @Akintunde-Rotimi i just added a return value as follow if( $retval == true ) { echo "Message sent successfully..."; }else { echo "Message could not be sent..."; } – Nayana Lakshitha Nov 12 '18 at 11:21
  • and it showing the message sent successfully but not receiving any email – Nayana Lakshitha Nov 12 '18 at 11:22
  • @NayanaLakshitha ok I got it, well, just make sure to include basic headers to avoid be reported as spam take a look at Amir’s answer, also check your spam folder – Jonnathan Q Nov 12 '18 at 11:23
  • @NayanaLakshitha check in your spam folder? – Nelson Owalo Nov 12 '18 at 11:23
  • Finally found it in spam folder and mails are there @JonnathanQ – Nayana Lakshitha Nov 12 '18 at 11:26
  • Finally found it in spam folder and mails are there @NelsonOwalo – Nayana Lakshitha Nov 12 '18 at 11:27

1 Answers1

0

I think the value of $email_body variable should end with semicolon (;) and also $headers should be written like below

$headers = 'From: webmaster@example.com' . "\r\n" .
      'Reply-To: webmaster@example.com' . "\r\n" .
      'X-Mailer: PHP/' . phpversion();
Amir
  • 179
  • 2
  • 14