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);
Related
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
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I have a PHP simple code which supposed to send an email, but it seems it does not work since I have never received any mail.
Well I have a free free host and domain with CPanel that I have a Webmail feature there, I can send email through that to wherever I want (Yahoomail, Gmail, ..).
I really do not know how to solve it.
Here is my php code:
$to = 'sample#yahoo.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: sample#sample.com' . "\r\n" . 'Reply-To: sample#sample.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);`
My sendmail_path /usr/local/bin/phpsendmail, Path to sendmail /usr/local/bin/phpsendmail, SMTP localhost, smtp_port 25
Try this ...
$to = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$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'm on a server that requires the errrors-to header to be set when using PHP mail() function. Popular applications such as Wordpress don't specify this header (at least not everywhere) - so the email isn't sent (instead I'm sent a message saying there was an error sending the email because it lacked the header). The server host says this is not something they can change for the system. I do not have access to the .ini file. Is there something I can do about it?
yes, by setting the mail-header you need with the normal mail()-function:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'errors-to: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
In wordpress, you can overload the wp_mail()-function to include that header.