Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail() function is not working in namecheap server

Tags:

php

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 = "[email protected]";
$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: <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\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='[email protected]' 
$from='[email protected]'

But i am changing the mail $to OR $from into gmail server like [email protected] 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."

like image 436
Ramalingam Perumal Avatar asked Jan 11 '16 06:01

Ramalingam Perumal


People also ask

Why is my PHP email not working?

It could be, for example, because you're missing necessary parameters, have typos in your recipient's email address, or need to set up an SMTP relay. Out of all the possible options as to why your PHP mail() isn't sending email, we found that most issues stem from Postfix being configured incorrectly.

How do you check PHP mail () is working?

to check if it is sending mail as intended; <? php $email = "[email protected]"; $subject = "Email Test"; $message = "this is a mail testing email function on server"; $sendMail = mail($email, $subject, $message); if($sendMail) { echo "Email Sent Successfully"; } else { echo "Mail Failed"; } ?>

How do I enable SMTP on namecheap?

Log into your WordPress admin dashboard and go to Plugins >> Add New >> search for the WP Mail SMTP plugin and click Install Now: 2. Once installed, click Activate.


2 Answers

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 = '[email protected]';                 // 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('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Rama Lingam');     // Add a recipient
$mail->addAddress('[email protected]');               // Name is optional
$mail->addReplyTo('[email protected]', 'Information');
$mail->addCC('[email protected]');
$mail->addBCC('[email protected]');

$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';
}
like image 135
Ramalingam Perumal Avatar answered Nov 14 '22 23:11

Ramalingam Perumal


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.

like image 20
AGradePHP Avatar answered Nov 14 '22 23:11

AGradePHP