mail() function not working - How to resolve it? - php

mail() function is not working on my server. I have used basic mail() code to know whether its the problem of script. Still it didn't send email. Somebody advised me to change the setting on my server to enable mail() function or something like that.
How can I do that? How can I know that my server allows mail() or it runs mail() properly?
Any advice?

If you using phpmailer Library, you cant use mail(). Because it has predefined function. You can check that by visiting PhpMailer Example Page.
PhpMailer use $mail->Send() instead of mail()
Sample code of PhpMailer
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

If you are on a shared server fisrt i advise you to contact with them if they are responsible for it. if it was sending emails before it could be something you could cause and you may need to change your code.
you did not provide any sample code but here is mine, try it:
$to= "$confoemail";
$subject="Your Contact Request at somewebsite.Com";
$message= "the message to send";
$headers = 'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n".
'From: justin#webmasteroutlet.com' . "\r\n" . //that code here //perfectly works, search if the code is built -in of php.
'Reply-To: justin#webmasteroutlet.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to,$subject, $message, $headers);

What operating system are you running?
On ubuntu, you might try to install a mail server (postfix or sendmail).
apt-get install postfix

Test this on your hosting, if it doesn't work your hosting has mail disabled. which most free services do block. message me for a free small hosting for ur tests.
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];
$name = "somename"; $email="test#test.com"; $phone="1111111111" $subject="test"; $message="the message";
$to = 'info#fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'Reply-To: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
$phone."</br>Subject: ".$subject.
"</br>Message: ".$message;
//echo $themessage;
mail($to, $subject, $themessage, $headers);

Related

SMTP doesn't send mail to gmail and yahoo but has no error [duplicate]

I'm quite stuck with a problem sending mail from a PHP script.
Some data:
Shared hosting, no SSH access, only hosting provider panel
PHP version 5.2.5
Last year I built a site which had no problems sending mail with the same hosting
Let's say the domain is “domain.com” and my private address is “myaddress#mydomain.com” for anonimity's sake in the following code.
Here's the code:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$to = "myaddress#mydomain.com";
$subject = "Hi";
$body = "Test 1\nTest 2\nTest 3";
$headers = 'From: info#domain.com' . "\r\n" .
'errors-to: myaddress#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $body, $headers)) {
echo("Message successfully sent");
} else {
echo("Message sending failed");
}
require('class.phpmailer.php');
$message = "Hello world";
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->AddAddress("myaddress#mydomain.com", "Agos");
$mail->SetFrom("info#domain.com","My Site");
$mail->Subject = "Test Message";
$mail->Body = $message;
$mail->Send();
?>
And here is what I get:
Message sending failed
Could not instantiate mail function.
Which is baffling to say the least. Is there anything I can do to get at least some more meaningful errors? Why is code from the class showing up in my file?
It looks like the class.phpmailer.php file is corrupt. I would download the latest version and try again.
I've always used phpMailer's SMTP feature:
$mail->IsSMTP();
$mail->Host = "localhost";
And if you need debug info:
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only

PHP mail not send emails

I am facing an unexpected issue with a PHP mailfunction. The script sending email to all email address but not to my domain.
Suppose I send email to nitinsoni#gmail.com it was received but when I send email to nitin#mydomain.com it was not received.
I am using GoDaddy web hosting and PHP mail function. SMTP is also not working on GoDaddy server.
PHP code is as follows:
<?php
$to = 'nitin#mydomain.com';
//$to = 'nitinsonitest#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: nitinsonitest#gmail.com' . "\r\n" .
'Reply-To: nitinsonitest#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$r = mail($to, $subject, $message, $headers);
if($r) {
echo 'mail sent';
}else {
echo 'not sent';
}
die;
?>
And SMTP email via PHPMailer is not working as well:
<?php
echo "<pre>";
//die('ada');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->Host = "relay-hosting.secureserver.net"; // your SMTP Server
$mail->IsSMTP();
$mail->PORT = 465;
$mail->SMTPDebug=true;
$mail->SMTPAuth = true; // Auth Type
$mail->SMTPSecure = "ssl";
$mail->Username = "user#gmail.com";
$mail->Password = "password";
$mail->Sender = "user#gmail.com";
$mail->From = "user#gmail.com";
$mail->AddReplyTo("user#gmail.com");
$mail->FromName = "user ";
$mail->AddAddress("recepient#gmail.com");
$mail->IsHTML(true);
$mail->Subject = "Test subject";
$mail->Body='Test Subject';
$mail->WordWrap = 50;
if($mail->Send())
{
echo"<script>alert('The Form has been posted ,Thank you');</script>";
}
else
{
echo 'mail error';
}
Suppose I send email to nitinsoni#gmail.com it was received but when
I send email to nitin#mydomain.com it was not received.
I am using GoDaddy web hosting and PHP mail function. SMTP is also
not working on GoDaddy server.
I doubt this has anything to do with the coding when using mail or PHPMailer. The thing is just because you send a mail, it doesn’t mean that the receiving end thinks the mail is valid. And chances are the receiving end—even if it is your domain—has decided a random e-mail sent off of a random server is simply SPAM.
I have posted a more detailed answer here, but when it comes to SPAM it basically boils down to this: Do you have an SPF (Sender Policy Framework) record setup for your domain? Do you also have a PTR (reverse DNS) record set for that domain?
If you do not have an SPF or PTR record, the chance of your message simply being flagged as SPAM is quite high.
If you are serious about sending mails off of your server, you need to at least get your SPF record & PTR record set.

send mail using mail() in php

Hello I am learning php where i came to know mail() function i have tried this code
function sendMail()
{
$to = 'Sohil Desai<sohildesaixxxx#gmail.com>';
$from = 'Sohil Desai<sohildesaixxxx#hotmail.com>';
$subject = 'Test Mail';
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion();
$message = 'This mail is sent for testing.';
$mail = mail($to, $subject, $message, $headers);
if (!$mail) {
return 'Error occured sending mail.';
}
return 'Mail successfully sent.';
}
echo sendmail();
I have tested only for gmail, ymail and hotmail.
This function sends mail in a spam for gmail & hotmail and it won't send mail to ymail.
why it happens??
I am using Ubuntu 12.04 & php version 5.3.10.
Can anyone help me?? Thanks to halpers in advance..
Try adding "to" as a header:
$headers = 'MIME-Version: 1.0'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion();
More details can be found at http://www.php.net/manual/en/function.mail.php (example #4).
Verry simple
$to = 'someone#example.com';
$subject = 'the subject';
$message = 'the message';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Hope helps you!
Source: http://w3webtools.com/send-email-using-php/
Instead of using build in crappy email() function, make a use of PHPMailer.
It is easy to use and does most of the hard work for You.
Features:
Probably the world's most popular code for sending email from PHP!
Used by many open-source projects: WordPress, Drupal, 1CRM, SugarCRM,
Yii, Joomla! and many more Integrated SMTP support - send without a local mail server Send emails with multiple TOs, CCs, BCCs and
REPLY-TOs Multipart/alternative emails for mail clients that do not read HTML email Support for UTF-8 content and 8bit, base64, binary,
and quoted-printable encodings SMTP
authentication with LOGIN, PLAIN, NTLM, CRAM-MD5 and Google's
XOAUTH2 mechanisms over SSL and TLS transports Error messages in
47 languages!
DKIM and S/MIME signing support Compatible with PHP
5.0 and later Much more!
All you have to do is to download and include this class to your project and then use it as in example:
A Simple Example
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.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('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$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 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
You'll find plenty more to play with in the examples folder at https://github.com/PHPMailer/PHPMailer.

PHP and SMTP server

My simple code below:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: info#mail.com \r\n";
$headers .= 'Bcc: test#mail.com' . "\r\n";
$subject = "Information";
if (mail($email, $subject, $message, $headers)) {
$mail_status = "success";
} else {
$mail_status = "fail";
}
if ($mail_status == "success") echo '{"status":"success"}';
How can I support SMTP support?
As You mentioned PHPMailer in tags. You can use PHPMailer Class for this process.
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional,
//gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Source: http://phpmailer.worxware.com/index.php?pg=examplebsmtp
SMTP server has to be configured on server, but by PHP, which is a programming language. Ask Server admin to provide you SMTP details.
SMTP information can be set in php.ini or via ini_set()
Example:
ini_set("SMTP","smtp.example.com" );
ini_set('sendmail_from', 'user#example.com');
By the way, if you require SMTP Authentication, mail() does not support it. Try to use other PHP mail library such as SwiftMailer or PHPMailer.

PHP eMailing script

So I wrote a php script that sends a user a temporary password for when the forget their password so they can login and change it. The script works fine, and the email gets sent with all the correct information. The thing i want to change is who it is getting sent by. I want to use google email app for websites to send those emails, rather the emails are getting sent by my webserver. Here's what the sending part of my script looks like:
$email_to = $_POST["email"];
$email_from = "Admin#domain.com";
$email_subject = "Account Information Recovery";
$email_message = "Here is your temporary password:\n\n";
$email_message .= "Password: ".$password."\n";
$email_message .= "\nPlease log into your account and immediately change your password.";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
However when I receive the email, it comes from Admin#webserver. How do I use google's email app to send these emails?
Probably best to use PHPMailer:
$mail = new PHPMailer();
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; //1 for debugging, spits info out
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl'; //needed for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'google_username';
$mail->Password = 'google_password';
$mail->SetFrom($email_from, 'Your Website Name');
$mail->Subject = $email_subject;
$mail->Body = $email_message;
$mail->AddAddress($email_to);
$mail->Send();
Note: This example uses SMTP directly to send the email, which will correct the issue, but if the host has fsockopen disabled it will not work.
I would suggest Swiftmailer. It's got a very nice and well-documented API, and supports all different kinds of transports.
From the docs:
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);

Categories