PHP mail function is not working? - php

$to = 'abc#xyz.com';
$subject = 'Feedback';
$finalmessage = "";
$from = 'def#ghi.com';
$finalmessage = $name . $address . $phnum . $email . $feedback;
$finalmessage = wordwrap($finalmessage, 70);
$mail=mail($to,$subject,$finalmessage,"From: $from\n");
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}
Thats the code. At the end it displays "Thank you for using our mail form", but i am not receiving any mail. Any ideas whats going wrong?

if(isset($_POST['Submit'])){
$to="email";
// this is your Email address
$from = $_POST['Email_Address']; // this is the sender's Email address
$first_name = $_POST['Full_Name'];
$tel_num=$_POST['Telephone_Number'];
$msg=$_POST['Your_Message'];
$subject = "Full Name : ".$first_name;
$headers = "From: ".$first_name." <".$from.">\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$message="Full Name : ".$first_name."<br><br>Telephone Number : ".$tel_num."<br><br>Message : ".$msg;
$bericht = nl2br($message);
mail($to,$subject,$message,$headers);
}

You need to configure SMTP settings of your local server in php.ini file as follows:
[mail function]
; For Win32 only.
SMTP = localhost // your smtp server
smtp_port = 25
Or you should give try to Swift Mailer or PHP Mailer

Try using fake sendmail to send emails in a windows enviroment.
http://jesin.tk/using-sendmail-on-windows/

Use php mailer() function
You can use PHPmailer :
http://phpmailer.codeworxtech.com/
Now
use following code -
<?php
require("class.phpmailer.php"); // give proper path of folder if needed
$mail = new PHPMailer();
session_start();
ob_start();
php?>
<your mail body goes here>
<?php
$body=ob_get_contents();
ob_end_clean ();
$to = 'abc#xyz.com';
$subject = 'Feedback';
$finalmessage = "";
$from = 'def#ghi.com';
$finalmessage = $name . $address . $phnum . $email . $feedback;
$finalmessage = wordwrap($finalmessage, 70);
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->IsHTML(true);
$mail=mail($to,$subject,$finalmessage,"From: $from\n");
if($mail){
echo "Thank you for using our mail form";
}else{
echo "Mail sending failed.";
}

You need to configure your mail SMTP in php.ini
It's quite simple, what is your ISP? For example, in comcast, just search in Google "comcast smtp address and port"
Take note of the SMTP address and Port, then go to your php.ini
Search for this configuration:
[mail function]
; For Win32 only.
SMTP = localhost // your smtp server
smtp_port = 25
Change the address with the SMTP and smtp_port. Then you should be able to send emails in your localhost

Related

php mail function not working reply SMTP server response: 503

I try to send mail using php mail function. but I getting error like:
PHP Warning: mail(): SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server. in C:\Inetpub\vhosts\qubedns.co.in\httpdocs\Codes\design\rnp\mailsend.php on line 21
Here is my PHP SCRIPT:
<?php
$toEmail = 'bikash336#gmail.com';
$subject = 'hello';
$message = 'Users are able to send emails to local domains and addresses but are unable to send email to any external address.';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <sales#leonwallet.com>' . "\r\n";
$Status = mail($toEmail,$subject,$message,$headers);
if($Status){
echo '1';
}else{
echo '0';
}
?>
Here is my Server configuration: http://qubedns.co.in/codes/php/
What's wrong with me?
I have found the solution with PHPMailer.
Its work for me.
<?php
require('mailserver/PHPMailerAutoload.php');
require('mailserver/class.phpmailer.php');
require('mailserver/class.smtp.php');
require('mailserver/class.pop3.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'ssl';
$mail->SMTPAuth = true;
$mail->IsHTML(true);
$mail->Username = 'sales#leonwallet.com';
$mail->Password = 'password';
$mail->From = 'sales#leonwallet.com';
$mail->FromName = 'Leon Sales Team';
$mail->AddAddress($to);
$mail->AddReplyTo('sales#leonwallet.com', 'Leon Sales Team');
$mail->Subject = $sub;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = $message;
if(!$mail->Send()){
echo "1"; // ERROR
}else{
echo "0"; // SUCCESS
}
?>
Here is a solution. For me he solved the problem
When I changed the environment, the TO parameter was configured with the domain of my test / development project.
I changed the TO email to the production email and the error disappeared.
Here is the solution: http://www.marathon-studios.com/blog/php-mail-503-error/

Php mail not Send sometimes

here is my php code to send email.
<?php
class mailer {
public function send_request_mail($to, $msg) {
$from="abcd#xyz.com";
$headers = 'MIME-Version: 1.0' . "\r\n".'Content-type: text/html; charset=iso-8859-1' . "\r\n".'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion ();
$message = "ip 192.168.0.9:9035";
$subject = "subject";
mail ( $to, $subject, $message, $headers );
}
}
$mail=new mailer();
$mail->send_request_mail("abcd#xyz.com", "msg");
?>
sometimes its works(for some messages).when i try to send an ip address like above,it fails.help me
Hope you are doing Well.
PHP must be configured correctly in the php.ini file with the details of how your system sends email. Open php.ini file available in /etc/ directory and find the section headed [mail function].
Windows users should ensure that two directives are supplied. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.
The configuration for Windows should look something like this:
[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net
; For win32 only
sendmail_from = webmaster#tutorialspoint.com
Linux users simply need to let PHP know the location of their sendmail application. The path and any desired switches should be specified to the sendmail_path directive.
The configuration for Linux should look something like this:
[mail function]
; For Win32 only.
SMTP =
; For win32 only
sendmail_from =
; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i
PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.
mail( to, subject, message, headers, parameters );
Example:
Following example will send an HTML email message to xyz#somedomain.com copying it to afgh#somedomain.com. You can code this program in such a way that it should recieve all content from the user and then it should send an email.
<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
$to = "xyz#somedomain.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#somedomain.com \r\n";
$header = "Cc:afgh#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
</body>
</html>
Hope this will be usefull to you !!! Cheers !!
Waiting for your positive comments !!!
You may want to use something like PHPMailer because there is a good chance your email will end up in spam or junk box when you use mail() function.
Here is a sample code using PHPMailer,
<?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->From = 'abcd#xyz.com';
$mail->FromName = '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->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'ip 192.168.0.9:9035';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

Difference between mail sending using localhost and live server [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I wish to know the difference between mail sending using localhost and live server.
This is my code and someone help to correct this code so that mail along with attachment can be sent successfully.it showed mail sent but I did not receive any.I am not sure where the error or code missing happens.Help me so that I can rectify it.
//This is m1.php file
<?php
$max_no_img=1; // Maximum number of images value to be set here
echo "<form method=post action=m2.php enctype='multipart/form-data'>";
echo "<tr><td>Photo: </td><td><input type='file' name='uploadfile' class='bginput'></td></tr>";
?>
<input type="submit" name="Submit" value="Send">
//This is m2.php file.From m1.php link redirects to here and the mail with attachment to be done here
<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = true;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "mymail#gmail.com"; // SMTP account username
$mail->Password = "password";
if($_POST){
$targetFolder = "image/".$_FILES['uploadfile']['name']; //My location where images stored & retrieved
copy($_FILES['uploadfile']['tmp_name'],$targetFolder);
$htmlbody = " Your Mail Content Here.... You can use html tags here...";
$to = "mymail#gmail.com"; //Recipient Email Address
$subject = "Test email with attachment"; //Email Subject
$headers = "From: mymail#gmail.com\r\nReply-To: mymail#gmail.com";
$random_hash = md5(date('r', time()));
$headers .= "\r\nContent-Type: multipart/mixed;
boundary=\"PHP-mixed-".$random_hash."\"";
// Set your file path here
$attachment = chunk_split(base64_encode(file_get_contents($targetFolder)));
//define the body of the message.
$message = "--PHP-mixed-$random_hash\r\n"."Content-Type: multipart/alternative;
boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"."Content-Type: text/plain;
charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n";
//Insert the html message.
$message .= $htmlbody;
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";
//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"."Content-Type: application/zip;
name=\"$targetFolder\"\r\n"."Content-Transfer-Encoding:
base64\r\n"."Content-Disposition: attachment\r\n\r\n";
$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";
//send the email
$mail = mail( $to, $subject , $message, $headers );
echo $mail ? "Mail sent" : "Mail failed";
}
?>
mail() will return value if your mail sent successfully.
Try this.
$status = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
if($status) {
echo 'Mail sent successfully';
} else {
echo 'Mail sending failed';
}
The reason that you are not receiving the email is likely that you have not configured your localhost machine to run an SMTP server.
To test emails on localhost I use Antix SMTP imposter. It sits on your machine and shows emails that would have been sent had your app been connected to an SMTP server. This has the added bonus of allowing you to see the emails as they would be received without having to worry about sending live emails from your test environment.

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

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);

PHP Contact Form Best Practice

I'm setting up a PHP contact form on a site. I'm using the Swift Mailer library to send the mail, and my domain email is through Google Apps. Is it possible to use Google Apps for company email and use sendmail/SMTP on my VPS to send email from the contact page? The problem I'm having is that I can't dynamically generate the from address, Google's servers force that to be the email address that the email is going through. Thanks in advance.
I use PHPMailer with this function...
function email($to, $subject, $body){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "email#domain.com";
$mail->Password = "password";
$mail->SetFrom("anything#domain.com", "Any Thing");
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
$mail->Send();
unset($mail);
}
After doing some reading, I realized that I didn't need a mail library at all. I'm using PHP's mail() function to accomplish exactly what I wanted, sending form mail through sendmail and having Google Apps handle all domain email. Here's the relevant code that's working for me.
// Define message variables
if(get_magic_quotes_gpc()){
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$body = stripslashes($_POST['body']);
}else{
$name = $_POST['name'];
$email = $_POST['email'];
$body = $_POST['body'];
}
$subject = "Website Contact Form";
$recipient = "web#somesite.com";
$content = "NAME: $name, $email\nCOMMENT: $body\n";
$mailheader = "MIME-Version: 1.0\r\n";
$mailheader .= "From: $email\r\n";
$mailheader .= "Reply-To: $email\r\n";
$mailheader .= "Bcc: another.email#address.com" . "\r\n";
mail($recipient, $subject, $content, $mailheader) or die("Failure");
header("Location:/thankyou.php");
}
This is working perfectly for me. Hope it helps someone else.

Categories