Can't send email with PHP Mail - php

I'm trying to send an email using php. I've the following only it doesn't seem to output anything to my browser nor send my email, can anybody see what i'm doing wrong?
$to = "liam#mysite.co.uk";
$subject = "Alien Sighting";
$message = foreach ($_POST as $key=>$value) {
$$key = $value;
}
$from = "Sighting#site.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";

Can you give us $_POST content please ?
Does your PHP configuration display all errors messages? Make sure to use error_reporting (E_ALL) in your dev environnement to find any typo / error. That should help to get an output to your browser.

You have to be sure to setup your SMTP server to send your email if you are running the script on your computer otherwise you can get a test server so you don't have to setup the SMTP it will send automatically

Are u using Rmail.php? Do u uploaded your code to a webserver or is it on your local PC?
I am always using Rmail.php to send mails.
$mail = new Rmail();
$mail->setFrom(YOUR ADDRESS);
$mail->setSubject(YOUR SUBJECT);
$mail->setHTML($textSchueler);
$mail->send(array($address));

You'll also want to check and make sure your server is configured to send email at all. While most shared hosting accounts will be configured for you if you are setting up your own server or developing locally PHP may not have the any way to send the messages. Try checking to see if the mail() function returns true indicating the message was successfully sent and then look in the mail logs on the server to verify transmission.

In this kind of situation where PHP does not output anything to your browser (even it should), most likely the script fails before your echo statement.
Make sure you have not disabled error reporting. See PHP runtime configuration error_reporting and display_errors. You can change error reporting behaviour also runtime with error_reporting() function in the code. Enabling error reporting helps you to figure out what the problem might be.
Even you had disabled the error reporting on the browser, check out your webserver logs. They should have details what's the problem (syntax error in the script, failed mail delivery by mail function call... there are many possibilities).

Related

phpmailer but without host

Previously I have the mail() function in PHP to work with my site. Now we got a domain for the site ( nginx server ) and now the mail() just does not want to behave, always returns false. I have the PHP ini set up for mail() with the correct domain as
So...I turned to PHPMailer and it's still not working....
I get this error....
Could not instantiate mail function.
so my question is what is going wrong? I do not have a SMTP host... i want to use the local server as the mailing server as it was working before and I believe I have the right set up for that.
Was installed with composer.
This is my code:
require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/PHPMailer.php");
require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/SMTP.php");
require_once($_SERVER['DOCUMENT_ROOT']."/vendor/phpmailer/phpmailer/src/Exception.php");
$emailer = new PHPMailer\PHPMailer\PHPMailer();
$emailer->IsMail();
$emailer->SetFrom("noreply#my.domain");
$emailer->FromName = "My Domain";
$emailer->AddAddress($admin_data['email']);
$emailer->isHTML(false);
$emailer->Subject = "test";
$emailer->Body = " test "
if(!$emailer->send()){
give_error("Send Email Error: " . $emailer->ErrorInfo);
return false;
}
When looking to get under the hood of why your PHPMailer-driven email is failing to send, be sure to turn on PHPMailer's "SMTPDebug" option.
To do this, set the SMTPDebug property to "3" after instantiating your PHPMailer object. In your code, it would look like:
//Echo errors, server responses, client message, data, commands & connection status
$emailer->SMTPDebug = 3;
This will echo out all errors, server responses, and client messages that the email might be spilling out as it's trying to complete its sending. With this, you should be able to get a better handle on what might be causing your email to mis-fire or not send at all.
Even if you're try-catching errors currently, it won't capture the full breadth and depth of information without this option turned on.
Please remember, however, to turn this option off before pushing to production. Setting the SMTPDebug property back to 0 will ensure you're not leaking errors, or important server information to the end-user.
//Will disable all debugging.
$emailer->SMTPDebug = 0
You can find more information about the SMTPDebug property within the PHPMailer documentation, which is likely named "PHPMailer-master/docs/" within your codebase.
Follow the links to the troubleshooting guide. In order for the mail() function to work, you need a local mail server installed - I recommend postfix. It’s nothing to do with your code.
That said, while using a local mail server is a good choice, it’s both faster and safer to use SMTP to localhost than to use the mail function. See the PHPMailer docs for why that is.

PHP can send email from command line but not browser

I have a form set up at my homepage. When someone submits the form, it redirects them to a php script that sends me the form. The only issue is, the form won't send me anything when I use the browser. I have a script running on the same server that when I execute from the command line, sends an email immediately. When I go to the page in a browser, it gives a confirmation message, but won't send anything. What's happening? PHP is using MSMTP to send email if that helps. I believe apache is running as root (I know that's bad). How do I check?
Here's the code if anyone wants it:
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "mailbot#prototypexenon.tech";
$to = "<MYEMAIL>";
$subject = "PHP Mail Test script";
$message = "This is a test to check the PHP Mail functionality";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "Test email sent";
?>
EDIT: Apparently new-server-owner was smart enough to not let apache run as root. All fixed now. Thanks #EdHeal
PHP running via the command line and PHP running via a web server can use totally different php.ini files. Run phpinfo() and see which php.ini file is being used in each case. Probably one of them doesn't have SMTP properly configured.
Apparently new-server-owner was smart enough to not let apache run as root. All fixed now. Thanks #EdHeal

PHP Mail 550 Error [duplicate]

I am getting this error despite configuring php.ini to a valid send_from address. I know it is valid because it works when I send it from squirrel mail but when sending mail in php it will just not work. the invalid address presumably refers to the send_from address. so I can't see how it can think it's wrong. here is the php code:
$email="tobiasvogel1#googlemail.com";
$subject = "Your New Password";
$from="admin#dayshare.local";
$message = "Your new password is as follows:
xxxxxxxxxxxxxxxxxxxxxxxxxxx
This email was automatically generated.";
if(!mail($email, $subject,$message,$from)){
echo ("error");
}else echo "success";
and in php.ini:
SMTP = localhost
sendmail_from = admin#dayshare.local
550 Delivery is not allowed to this address
This error means that the sender is trying to send an email to an
address which he is not allowed to send to. This message is generated
after hMailServer has checked the IP range settings. As an example,
the default IP range configuration does not allow external users to
send messages to other external users. This is to prevent people from
using your server to send spam. So if an external user tries to send a
message to another external user, he will get this message.
That is the meaning of the error you are getting. This is from the hMailServer Documentation.
Can you try if the following will work?
<?php
mail('tobiasvogel1#googlemail.com','Test Email','This is a test email.',"From: tobiasvogel1#googlemail.com");
?>
If it doesn't work, then it's probably due to a misconfiguration in your hMailServer and you would need to check your hMailServer Logs.
The 4th parameter of the mail() function is not plain "from". In your code, you are only passing the email address without "From: " - The fourth parameter is for additional mail headers, so you have to format it like this:
mail($email, $subject,$message,"From: admin#dayshare.local\r\nX-Mailer: PHP");
I added another header as an example.
Try this, worked for me:
ini_set("sendmail_from", "info#yourdomain.com");
You need quotes and a semi-colon:
$email="tobiasvogel1#googlemail.com";
here is another solution - WAMP send Mail using SMTP localhost
KEEP IN MIND, everytime, after You change php.ini,
you must restart wamp (! ! !)
p.s. in php.ini, i have used:
SMTP = localhost
smtp_port = 25
sendmail_from = your_user#gmail.com
or if oyu cant edit php.ini, try to insert these lines in your php script.
ini_set("SMTP", "localhost");
ini_set("smtp_port", "25");
ini_set("sendmail_from", "your_user#gmail.com");

php redirect email to file

I am using php5.
Are there some settings or a simple php.ini directive that would redirect all the emails to a folder?
I want on the development machine to have all the emails generated by the system not sent to the actual receiver but put in a folder.
Thanks.
I used to have some code like this (kinda pseudocode):
define ('DEBUG', true);
function send_email($to, $subject, $body) {
if (DEBUG) {
file_put_contents('some_folder/' . $to . date('dmY-His') . '.html', $body);
}else{
// Actual code to send email
}
}
But i agree with others, it's easier/better to setup an development email account to receive those emails.
I don't think you will be able to do something like this. Mails are sent by a mail server so it must be your mail server that writes them to a file instead of sending them.
Why not simply send it to a special development email?
Sample:
define('DEBUG', true);
if(DEBUG)
{
// Override recipient
$recipient = 'development#domain.tld';
}
// Send mail...
No settings that I'm aware of in PHP itself. However, if you're using Postfix on your development server, here's a recipe I cooked up to redirect all outbound email to a single (local) address:
/etc/postfix/main.cf: (add this to the existing file, don't replace everything)
virtual_alias_maps = regexp:/etc/postfix/virtual
/etc/postfix/virtual:
/.*/ duskwuff#localhost
You can configure your mail server to accept SMTP messages as normal, but make it unable to forward them onto another mail server. If your mail server supports it, make it redirect all messages to a postmaster account, or any other address of your choice.
This means that PHP will behave as normal, everything will appear to work straight away with the message, but it just won't go to the 'intended' recipient.
It also means that you can inspect headers (pretty much as they would normally appear), to support debugging.
There are many ways to do this. Basically, you need to define the sendmail command in your php.ini to point to a program or script which will save the mail locally.
One solution is this:
Catch emails with php script
Another is this:
Mail catcher

cannot initiate mail function in joomla

I am getting this error in Joomla while sending the mail.
I am not getting this error every time. But some times its shows me "cannot initiate mail function".
Any solution for this?
That could be any number of things, but a general list of things to check would be first, your Joomla config:
Admin panel > Global Configuration > Server > Mail Settings > Mailer
Make sure that's set to use the PHP mail function. If it is, try making a script called test.php and putting it in the root of your site (where the index.php file is for Joomla). Make that file something like this:
<?php
$to = "you#youremailaddress.com";
if( mail( $to , 'This is a test message.' , 'Is this working?' ) ) {
echo 'Email sent.';
} else {
echo 'Email failed to send.';
}
?>
Make sure you change the $to = line to your email address.
Now go to that script: http://www.yourjoomlasite.com/test.php
You should see the text 'Email sent.' in your browser and then receive an email to that address you entered. If not, then you should contact your hosting provider and ask them to upgrade to the latest PHP version and/or resolve the mail() function issue for you. That is the most raw implementation of sending mail via PHP and if that fails to work then it's got to be an issue with your host.
If you are working in the local server, the mail will not initiate. So host your website in server and try it.
If you have any questions, let me know
If you are using WAMP or similar on your own machine and are getting this message then you need to install a mail server. This is the best solution on Windows.
http://www.toolheap.com/test-mail-server-tool/

Categories