Why mail() not sending email? - php

Why the below code not sending mail to me? What is the error?
<?php
if(isset($_POST['name'])){
$msg="Name: ".$_POST['name']."\n Email: ".$_POST['email']."\n Address: ".$_POST['city']."\n Phone: ".$_POST['phone'];
mail('sganake#gmail.com', 'New Trial Request', $msg);
echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';
} ?>
No error I got. Just I have not received my email at inbox. This is running on IIS Server.

Try this
$headers = 'From: from#address.com' . "\r\n";
$validate = mail('sganake#gmail.com', 'New Trial Request', $msg, $headers);
if($validate)
{
echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';
}
else
{
echo '<h2 align="center" style="color:red">Something went wrong.</h2>';
}
If you get 'Something went wrong' it means problem is in your mail server and not in PHP code.

I suggest you to use PEAR::Mail package. You can send emails via SMTP then.
require_once "Mail.php";
$from = "your#gmail.com";
$to = "sganake#gmail.com";
$subject = "New Trial Request";
$body = "Name $name, Address $address ...";
$host = "ssl://smtp.gmail.com";
$port = 465;
$username = "your#gmail.com";
$password = "password";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory(
'smtp',
array(
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password
)
);
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message has been sent!</p>");
}

You might have to send Mail Using SMTP Authenticationas which is required by many mail servers.
Check this link for further details.

May be your php configuration is not complete, see in C://xampp/php/php.ini in:
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
This is for activate email. May be your setting is:
;sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Although I didnt change anything in your code. please try this
<?php
if(isset($_POST['name'])){
$to = 'sganake#gmail.com';
$subject = 'the subject';
$message = 'hello';
$msg='Name:'.$_POST{name}. "\r\n".
'Email: '.$_POST{email}. "\r\n".
'Address: '.$_POST{city}. "\r\n".
'Phone: '.$_POST{phone}. "\r\n";
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sent = mail($to, $subject, $message, $headers);
var_dump($sent) // just to debug
echo '<h2 align="center" style="color:green">Thank you for your message.</h2>';
} ?>

Related

Gmail and mac settings to send mail from a php script? [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I want to send mail from my php script, but unable to find the configuration settings for mac and gmail.
My php script is:
$to ="sender email id";
$subject = "Confirmation";
$header = "";
$message = "";
$sentmail = mail($to,$subject,$message,$header);
if($sentmail)
{
echo "Your Confirmation link Has Been Sent To Your Email Address.<br>";
}
else
{
echo "Cannot send Confirmation link to your e-mail address<br>";
}
I have also try another code on my ubuntu machine using Mail.php, but it shows some authentification error. Here's the code:
include_once "Mail-1.2.0/Mail-1.2.0/Mail.php";
$from = '<sender#gmail.com>';
$to = '<receiver#gmail.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'sender#gmail.com',
'password' => 'xxxxx'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
Provide me some solution so that I can successfully mail from my php script.
Try changing your header on your first script to this:
$header = 'From: put from email address here' . "\r\n" .
'Reply-To: put from email address here' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Changing the FROM in PHP email using SMTP

I have the following code that sends an email properly, but the email is received with a FROM value of root#mysite.com . I would like to have the value of the received email FROM be the value email (that is entered in a contact form) so that the received email can be replied top easily. Any assistance would be appreciated.
Thanx
<?php
if ($_POST["email"]<>'')
{
require_once "Mail.php";
$from = "root#mysite.com";
$to = "info <info#mysite.com>";
$subject = "Contact us info from Website";
$body = "Name: ".$_POST["name"]."\r\n";
$body .= "Email: ".$_POST["email"]."\r\n";
$body .= "Phone: ".$_POST["phone"]."\r\n";
$body .= "Comment: ".nl2br($_POST["comment"])."";
$host = "mail.mysite.com";
$username = "root#mysite.com";
$password = "abcdefghijk";
$headers = array ('From' => $from,'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body, "FROM: $email","-f$replyToEmail");
mail("$toEmail", $subject, $message, "FROM: $fromEmail","-f$replyToEmail");
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
Just set
$from = $_POST['email'];
You do it like this.
$fromEmail= 'From:'.$_POST["email"];
use $fromEmail in your mail function.

how do i configure smtp setting with mail function?

how do i configure smtp setting with mail function smtp is required because of web hosting doesn't allow to sending email without smtp setting due to security reason.
warning error: mail() has been disabled for security reasons
now how can i configure smtp setting with this mail function ()?
<?php
if(isset($_REQUEST['confirm'])){
$your_email = 'non-reply#test.com'; //CHANGE TO YOUR SETTINGS
$domain = $_SERVER["HTTP_HOST"]; //YOUR DOMAIN AND EXTENSION
$to = $email;
$subject = 'Demo Your Order';
$message = 'test1234';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Demo.Pk<'.$your_email.'#'.$domain.'>\r\n" .
//"Reply-To: $from \r\n" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
}
?>
It seems that mail is disable or does not have proper setting in your PHP.ini. I think, you can still send email using SMTP set to some other SMTP server like gmail. Please check below code, it might help you in solving this issue. Below code is using Mail.php frm PEAR
<?php
require_once "Mail.php";
$from = "<me#gmail.com>";
$to = "<you#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHope this helps?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "me#gmail.com"; //<> give errors
$password = "mypassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
You can also use some other Mail libraries like PHPMailer

sending mail from my account. Not webhosts

I got a contact form that succesfully sends emails to me.
heres the extract:
$_POST['message'] = wordwrap($_POST['message'], 70);
mail ('myemail#test.com', $_POST['subject'], $_POST['message'] , $_POST['email']);
echo "<div class='registertext'>Your email was succesfully sent to a member of the administration team. Please wait 24 hours for as to reply and ensure you check your junk mail!<br />To login please click <a href='login.php'>here</a></div>";
The issue I have is, the email gets sent from my host. Not a email I want to specify. How would I overcome this?
You can specify it in the email headers:
$recipient = "recipient#test.com";
$from = "You#yoursite.com";
$replyTo = "You#yoursite.com";
$subject = "Hi!";
$text = "<p>This is a test!<p>";
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: text/html; charset=utf-8\r\n"
."Content-Transfer-Encoding: 8bit\r\n"
."From: =?UTF-8?B?". base64_encode([Your Name]) ."?= <$from>\r\n"
."Reply-To: $replyTo\r\n"
."X-Mailer: PHP/". phpversion();
//send it!
if (mail($recipients, $subject, $text, $headers, "-f $from")){
echo "sent";
} else {
echo "did not send";
};
but there's a good chance it will get caught be SPAM filters. Your best bet in this case would be to use a PHP mailing library that handles SMTP emailing and uses your actual account to send mail (there are several packages that can handle this for you: Pear Mail, and PHP Mailer amongst others.
You can use PEAR mail which will use a SMTP account. Here is some code from my mail form I use
$from = "Name <webmaster#domain.com>";
$to = "Name <address#domain.com>";
$subject = "Subject";
$body = 'A message!';
$host = "ssl://domain.com";
$port = "465";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}

Sending mails in PHP using Yahoo SMTP

How can I send an email via Yahoo!'s SMTP servers in PHP?
You should use something like Swift Mailer or PHPMailer. The following example is for Swift:
$message = Swift_Message::newInstance()
->setSubject('Your subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
->addPart('<q>Here is the message itself</q>', 'text/html')
;
$transport = Swift_SmtpTransport::newInstance('smtp.mail.yahoo.com', 465, 'ssl')
->setUsername('your username')
->setPassword('your password')
;
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
You can use PHP's built-in mail() function to send mails, however it is generally very limited. For instance, I don't think you can use other SMTP servers than the one specified in your php.ini file.
Instead you should take a look at the Mail PEAR package. For example:
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
(I stole this example from http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm :P)
Read this http://php.net/manual/en/function.mail.php
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
PHP mailer will let you use any SMTP server you like, so long as you have the credentials to log on with.

Categories