7

I am new in namecheap domain server. I am trying to send a simple mail on that namecheap server. It wasn't sending mail and returned an empty value 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 assign $to and $from mailID within a namecheap server mail's then mail sent successfully.

Example:

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

But i am changing the mail $to OR $from into gmail server like test@gmail.com then it will not send a mail and is also returning empty value without error. How to fix.

From (Not receiving email from the PHP mail() method) Only domains that are hosted on our servers can be used in 'From' field. Any domain that is not hosted with us cannot be added to 'From' field. We had to take this measure to prevent sending spam using forums, guest books and contact forms scripts. For your site scripts to work properly you should set 'From' field to email account that has been created in your cPanel.

It's related to my issue but i don't know how to "set 'From' field to email account in my cPanel."

Community
  • 1
  • 1
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46

2 Answers2

3

Darren is right. I am changing the PHP mail() function to PHPMailer mail() method.Download link - https://github.com/PHPMailer/PHPMailer Now the mail was sent successfully. Thanks to Comments. Here is answer 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';
}
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46
3

I had the same issue. It turns out you need to make sure you have set the correct from name in the headers.

Go to your namecheap cpanel, go to the "Email Accounts" then click manage and you'll see the details of your default email account. Copy the name of the "Username" field underneath "Mail Client Manual Settings" then paste it in your PHP code inside the "from" header.

Hope it helps anyone.

AGradePHP
  • 63
  • 5
  • according with the official guide I fixed the problem with the following link: https://www.namecheap.com/support/knowledgebase/article.aspx/10038/31/how-to-configure-a-contact-form-with-us/#mailfunction – Francesco Orsi Jun 02 '22 at 19:54