I am using first time mail function in php i get a code from online and it implemented to my program but its not working. Can any please help me to correct this ?
<?php
$to = "ganesh.sunraise#gmail.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:lokesh.sunraise111#gmail.com ";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}
?>
First, please tell, are you getting "Message sent successfully.." message.
There are many reasons for mail is not working:
Your server does not support send mail.
Please try below method:
<?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);
?>
or use:
PHPmailer
You need to setup the email server locally, or you can use 3-rd party email server to send email from your web server. Added the following lines to your php.ini to send email using gmail.
smtp_server=smtp.gmail.com
smtp_port=465
smtp_ssl=auto
error_logfile=error.log
auth_username=your-gmail-username#gmail.com
auth_password=your-gmail-password
You can use SMTP as the others suggested, and this can be done using the PHPMailer library.
However, there's another suggestion and this is the one that I prefer (because SMTP may contain some problems that you've to search about them and fix them).
The other suggestion is to use a PHP mailling API like SendGrid or Mailgun - (I prefer SendGrid).
Basic SendGrid integration with PHP:
Download SendGrid from here https://github.com/sendgrid/sendgrid-php/releases/download/v6.2.0/sendgrid-php.zip - or follow the installation instructions for another ways for installation.
Unzip the SendGrid-PHP library and upload it to your server.
Use this sample code (edit it for your needs): https://github.com/sendgrid/sendgrid-php#quick-start
Related
I have Postfix and SquirrelMail configured on my server. And user called noreply. So, I am logging to SquirrelMail and sending email to some address. Everything works fine, i am receiving email from noreply#mydomain.com. But when I try to send email from php using mail(),
mail ('test#2ether.net', 'Postfix Test', 'A test email');
server tries to send it from address www-data#mydomain.com. How do I configure it to send emails from noreply#mydomain.com?
I would use PEARMail it is designed to counter such issues, (PHP's "mail()" method is all but useless in today's email meta - too many bots used it ) however the actual answer is this:
$to = 'test#2ether.net';
$subject = 'Postfix Test';
$message = 'A test email';
$headers = 'From: noreply#mydomain.com' . "\r\n" .
'Reply-To: noreply#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
I'm trying to use noreply to send out emails, but it doesn't work - it wont sent anything. I am using this test-file:
<?php
$to = 'myemailhere';
$subject = 'You received an email message!';
$message = 'This is a message in the email body form.';
$headers = 'From: noreply#example.com' . "\r\n" .
'Reply-To: noreply#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
What could be the problem? By the way, I am using rackspacke, if anyone should know about it.
Thanks in advance!
If you're running on a Rackspace cloud server, you will have to configure the server yourself to be able to send mail. The mail() function relies on the OS configuration to handle emailing. If you are on a Rackspace cloud site, you will likely need to contact Rackspace support for help.
Personally I dodge this bullet altogether by using PEAR's SMTP class. This is a full SMTP implementation in PHP and since it does not rely on any outside configuration or module, it is fully portable. It has saved me a LOT of trouble.
http://pear.php.net/package/Mail
http://pear.php.net/package/Net_SMTP
Note: The PEAR site is having some loading issues atm for me. Give it a minute and it should load.
what is exception? use try catch block. Else try this code
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
I am trying to send mail from localhost using PHP. I am using the following code to send the mail :-
<?php
$to = 'o****e#gmail.com';
$subject = 'hey You';
$message = 'Can you identify me :P';
$headers = 'From: at*****t#gmail.com' . "\r\n" .
'Reply-To: at*****t#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
At first, I tried to send the mail to myself(at*****t#gmail.com), it worked fine. However, after that, now if I am changing the $to, its still sending the mail to same ID(mine) with the previous contents(not the updated one).
Is my request getting cached somehow ? Why every mail is being repeatedly sent to me irrespective of change in both contents and $to ?
Go to your php.ini file and change SMTP = localhost to SMTP = aspmx.l.google.com and uncomment sendmail_from and put in your sending gmail address. and set smtp_port = 25
Restart localhost
I am trying to set up a php script that would send an email to selected users, I looked on the internet and found out how but can't run the script cos I get the following error:
Warning: mail() [function.mail]:
"sendmail_from" not set in php.ini or
custom "From:" header missing in
E:\Program
Files\xampp\phpMyAdmin\emailer.php on
line 10
Here is the code of the script I designed following examples:
<?php
$recipient = "<ismael.srcyber#gmail.com>";
$subject = "Flight Status";
$body = "Flight has just landed.";
$host = "ssl://smtp.gmail.com";
$port = "465";
if(mail($recipient, $subject, $body))
{
echo("<p>Message successfully sent</p>");
}
else
{
echo("<p>Message Delivery failed </p>");
}
?>
Thanks in advance.
As the error suggest you must add the default "from" field (who sends the mail) in php.ini:
SMTP = localhost
sendmail_from = me#localhost.com
And then restart apache
Otherwise you can set it dinamically as the fourth parameter as stated in the php manual (look http://php.net/manual/en/function.mail.php)
Note:
When sending mail, the mail must
contain a From header. This can be set
with the additional_headers parameter,
or a default can be set in php.ini.
Failing to do this will result in an
error message similar to Warning:
mail(): "sendmail_from" not set in
php.ini or custom "From:" header
missing. The From header sets also
Return-Path under Windows.
Example:
<?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);
I'm having issues sending emails using the php mail() function. I know the php script I have works because I have an identical copy of it on another web-hosting company and it works there.
I think it has to do with the web-hosting company itself. Do any of you know what I need to do in order to make it work? Is there something I need to tell them to install? I think they're running on Apache.
Thanks,
Amit
For clarification purposes, here is the mail-script.
<?php
$to = 'my#email.com';
$subject = 'Contact from your website';
$message =
'Below are details from the Contact Us Form ' . "\n\n" .
'Name: ' . $_REQUEST['name'] . "\n\n" .
'Telephone Number: ' . $_REQUEST['phone'] . "\n\n" .
'E-mail address: ' . $_REQUEST['email'] . "\n\n" .
'Comments: ' . $_REQUEST['comments'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
//SPAM CHECK
$str = $_REQUEST['spam'];
$strE = $_REQUEST['email'];
if( $str != "10" || $strE == "")
{
echo "<div align='center' style='color:red'>One or more of the form fields were incorrect, you will be redirected to the contact page within 3 seconds.</div>";
?><meta http-equiv="refresh" content="3;URL=http://engineercreativity.com/samples/biz/contact"><!-- EDIT THIS -->
<?php
} else {
mail ($to, $subject, $message, $headers);
?>
<meta http-equiv="refresh" content="0;URL=http://engineercreativity.com/thankyou.html"> <!-- EDIT THIS AS WELL -->
<!--
<div class="text" align="center" style="text-align: center; color: green;">
<br/>
Thank you for contacting us!
<br/>
The message was succesfully sent!
</div>
-->
<?php
}
?>
If it is a dedicated server, make sure you have postFix Mail installed (http://www.postfix.org/)
I faced this error today itself as the SMTP server was not available (i assumed it as there by default, but not)
Are you performing any kind of checks on the mail function? It should return true if it's executing successfully - knowing that would help us cut down on other possible reasons you may not be receiving the mail, such as filters, server or smtp configuration etc. Doing something like:
if (mail($to, $subject, $body, $header)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
Should give you a better idea, and should die outright if the function does not exist for some reason. Php's mail function is incredibly finicky on free web hosts, since it's commonly abused for spam purposes.
Posting full headers also can help legitimate messages pass spam tests.
$headers = "Return-path: <sendingemail#test.com>\n";
$headers .= "Reply-to: <sendingemail#test.com>"."\n";
$headers .= "Content-Type: text/html; charset=windows-1252\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
$headers .= "From: <sendingemail#test.com>\n";
$headers .= "X-Priority: 3\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Organization: My Organization\r\n";
$headers .= "\n\n";
Write a really simple script, like
<?php
mail('your_mail#example.com', 'test subject', 'test msg') or die('no mail()');
echo 'mail sent.';
Execute it, and make sure the mail is not caught by your spam filter (if you can afford it, set up your own domain/DNS server, netcat -l -p 25 is sufficient).
If that doesn't work, contact the support of your web hoster. Do they have an FAQ or any other documentation?
Whatever the solution, check your mail()'s output.
Most common solution
Ask your hosting company if your current web host has SMTP set up to relay mail from your scripts. If they say "no", then they might have another SMTP host for you to use like smtp.example.com, or you'll have to use another SMTP relay (check with your current e-mail provider).
Alternative
The SMTP server you're talking to might not understand what your script is saying. I've seen situations before where my mail script will work with Postfix but not qmail. This is easily solved by using a third party e-mail library: there are tons out there, but my favorite is Flourish's (http://flourishlib.com/docs/fEmail).
mail() function of php, will send your mail to junk only. Instead use SMTP php mailer function.
Why we should use SMTP instead PHP mail():
SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam.
Mail sent with the mail() function is sent with sendmail in most cases. There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers.
This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. You are actually sending from user#serverhostname.tld and not someone#example.com like you had told the mail function to do.
If you use SMTP and view the original the email is actually sent from someone#example.com
You can download SMTP class from:
https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170
http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html