PHP mail () function not working on namecheap server

I am new to namecheap domain server. I am trying to send simple mail to this namecheap server. He did not send mail and returned an empty value, and not any error.

Here is my sample code.

$to = " raamanmca@gmail.com "; $subject = "HTML email"; $message = "Hello this is testing mail"; // Always set content-type when sending HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers .= 'From: < ramalingam@binaryswan.com >' . "\r\n"; $headers .= 'Cc: myboss@example.com ' . "\r\n"; if(mail($to,$subject,$message,$headers)) { echo "Mail sent..."; } else { echo "Mail not sent"; } 

Suppose I assigned $ to and $ from the mailID in the mail of the namecheap mail server, after which the mail was sent successfully.

Example:

 $to=' test@binaryswan.com ' $from=' hello@binaryswan.com ' 

But I change the $ mail to OR $ from the gmail server, for example test@gmail.com , then it will not send mail and will also return an empty value without errors. How to fix.

From ( Do not receive mail from the PHP mail () method ) Only areas hosted on our servers can be used in the From field. Any domain that is not hosted by us cannot be added to the From field. We had to take this measure to prevent spam from being sent via forums, guest books, and contact form scripts. For your site scripts to work correctly, you must set the From field to the email account created in your cPanel.

This is due to my problem, but I donโ€™t know how to "set the From field to the email address in my cPanel".

+5
source share
1 answer

Darren is right. I am changing the PHP () function to the PHPMailer mail () method. Download the link - https://github.com/PHPMailer/PHPMailer Now the mail has been sent successfully. Thanks to the comments. Here is the response code:

 require 'class.phpmailer.php'; $mail = new PHPMailer; $mail->Host = 'smtp1.example.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = ' user@example.com '; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to $mail->setFrom(' from@example.com ', 'Mailer'); $mail->addAddress(' ramalingam.p@pickzy.com ', 'Rama Lingam'); // Add a recipient $mail->addAddress(' raamanmca@gmail.com '); // Name is optional $mail->addReplyTo(' info@example.com ', 'Information'); $mail->addCC(' cc@example.com '); $mail->addBCC(' bcc@example.com '); $mail->isHTML(true); $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo '<br>Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } 
+3
source

Source: https://habr.com/ru/post/1240313/


All Articles