I noticed a couple other posts around that were similar to my problem but found that they weren't specific to my scenario or I am just not understanding things very well.
My situation: I am trying to send mail from a LAMP server using the PHP mail() function. I want to relay the mail to another dedicated mail server.
Problem: It seems I am not able to send mail...sometimes. Sometimes it seems capable of sending mail to accounts outside of the domain but it fails to send any mail to accounts within the domain/network. I have seen logs often complaining about it not being able to authenticate to the domain controller...but it shouldn't have to worry about that should it?
I guess the part I am confused about is does the PHP mail() function automatically create an SMTP message to the server? Or do I have to set the php.ini settings to look at the localhost and then configure sendmail/postfix to send the message to the mail server. Also, why would it bother to authenticate with the domain controller if I only specified that the LAMP server try to connect with the mail server?
Hopefully someone can help me get this sorted out. It's been bothering me for a while now and haven't been able to find a solution.
Thank you,
does the PHP mail() function automatically create an SMTP message to
the server? Or do I have to set the php.ini settings to look at the
localhost and then configure sendmail/postfix to send the message to
the mail server
PHP's mail() function doesn't do that much by itself. On linux, it will typically connect to a local instance of sendmail or similar, using settings defined in php.ini. Check out this section from example php.ini:
[mail function]
; For Win32 only.
; SMTP = localhost
; smtp_port = 25
; For Win32 only.
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
sendmail_path = /usr/sbin/sendmail -t -i
For windows, SMTP server at configured port is used by mail(). For linux, defined sendmail path is used.
Using other words, PHP mail() function on windows connects to a SMTP server, be it local or remote. PHP mail() on Linux cannot do the same. On Linux, the function will only use a local sendmail installation that you need to set up yourself to connect to a SMTP server. Alternative for that kind of configuration is to use PHPMailer, Swiftmailer, Zend_Mail or similar, that provide SMTP functionality by themselves.
why would it bother to authenticate with the domain controller if I
only specified that the LAMP server try to connect with the mail
server?
I'm far from being an expert on this, but as far as I've understood, you need to be authenticated user in your network to access outside resources which mailing would need. A domain controller gives out that kind of permissions.
Start using Swiftmailer (documentation) or PhpMailer, your life will be easier...
Swiftmailer example:
require_once 'lib/swift_required.php';
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself');
$mailer->send($message);
PhpMailer example :
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->AddAddress("whoto#otherdomain.com", "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$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!";
}
I prefer Swiftmailer, but you select you best choice ;-)
Related
I am trying to call the mail function, but whenever I put it in the script, the page does not load.
I have the following code for my php.ini file in XAMPP:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=localhost
; http://php.net/smtp-port
smtp_port=80
auth_username = XX_MYEMAIL_XX
auth_password = XXXXX_MYPASSWORD_XX
I have a 64-bit computer, but an error message said it was missing a sendmail_from, so I gave this variable a value. I have Mercury running from XAMPP, but I don't know if I configured anything that needs to be configured.
I get the following error
mail(): Failed to connect to mailserver at "localhost" port 80, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
I used the following php code:
<?php
$header = "From: varunsingh87#yahoo.com";
$to_email = 'VSpoet49#gmail.com';
$subject = 'Testing PHP Mail';
$message = 'This mail is sent using the PHP mail function';
if (mail($to_email, $subject, $message)) {
echo "<p>Email sent!</p>";
} else {
echo "<p>Email not sent.</p>";
}
?>
Below this is the default html tags.
Update
I removed the sendmail_from and set the smtp_port to 25.
mail(): Bad Message Return Path i
Related
Warning: Bad Message Return Path (PHP)
Failed to connect to mailserver at "localhost" port 25
First, I never heard for mail server listening on port 80.
I have XAMPP installed too, but with configured with "smpt_port=25".
Second, you have "SMTP=localhost" so in order to send email you must have installed mail server on your machine, for example "Mercury" from XAMPP package.
Third, it can be very tricky to properly send email using "mail()" function (authentication, spam detection...) so best solution is to avoid usage of "mail()" function and use some robust library/component/script for that.
It is good advice by Baranix to learn how to use PhpMailer or SwiftMailer (my favourite) and configure them to target real, well configured mail server on real hosting.
Learn how to use PhpMailer and don't mess with this embarrassing mail function.
https://github.com/PHPMailer/PHPMailer/wiki/Tutorial
With this class you will send all messages with and without authorization, with or without tls/ssl and attachments (files, images).
!!! Install smtp server: hmailserver on localhost first !!!
https://www.hmailserver.com/download
And create your domain email mailbox.
Regards
http://php.net/manual/language.operators.errorcontrol.php
Let us learn about the # symbol, but be warned of a possible fake return status.
People typically try this first though
Try Catch Block
i have xampp 7.1 installed on my 2016 macbook pro. I have a php contacts form in my source folder which is rendered successfully by chrome along with my html. However, I need to configure the smtp settings to test some functionality locally from my mac.
I found a php.ini in the xampp etc folder that contains some mail function.
I found other articles which reference phpmailer but since they were a few years old i figured newer versions of xampp for mac may have all the functionality builtin.
Can I modify this php.ini file to use a gmail mailbox as a recipient? How so?
thanks in advance
Here is a copy paste of the mail function :
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me#example.com
; For Unix only. You may supply arguments as well (default: "sendmail
-t -i").
; http://php.net/sendmail-path
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra
parameters
; to the sendmail binary. These parameters will always replace the
value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script
followed by the filename
mail.add_x_header=On
; Log all mail() calls including the full path of the script, line #,
to address and headers
;mail.log =
If what you are trying to do is sending mails from your app server to your own gmail inbox, you don't have to modify these settings; use PHPMailer is an easier way. So let's start step by step.
Allowing Less secure apps -or- configure for XOAUTH2; credits to #Synchro.
Download PHPMailer and extract PHPMailer.php, SMTP.php and Exception.php to your xampp/htdocs folder
Write a sample send mail page send.php: (modify to your real account)
<?php
require_once('SMTP.php');
require_once('PHPMailer.php');
require_once('Exception.php');
use \PHPMailer\PHPMailer\PHPMailer;
use \PHPMailer\PHPMailer\Exception;
$mail=new PHPMailer(true); // Passing `true` enables exceptions
try {
//settings
$mail->SMTPDebug=2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host='smtp.gmail.com';
$mail->SMTPAuth=true; // Enable SMTP authentication
$mail->Username='YourAccount#gmail.com'; // SMTP username
$mail->Password='YourPassword'; // SMTP password
$mail->SMTPSecure='ssl';
$mail->Port=465;
$mail->setFrom('sender#whatever.com', 'optional sender name');
//recipient
$mail->addAddress('YourAccount#gmail.com', 'optional recipient name'); // Add a recipient
//content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject='Here is the subject';
$mail->Body='This is the HTML message body <b>in bold!</b>';
$mail->AltBody='This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
}
catch(Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: '.$mail->ErrorInfo;
}
?>
When all above are ready, issue the url of send.php on your app server to send the example email
Base on this example, you can write a contact form for real use. You might also want to change the location of PHPMailer, then you should modify the path in the example code to point to the new location where they are.
I have installed wamp on windows 8.
Got error:
Warning: mail() [function.mail]: Failed to connect to mailserver at
"localhost" port 25, verify your "SMTP" and "smtp_port" setting in
php.ini or use ini_set() in C:\wamp\www\mail.php on line 9
Here is the simple source code:
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('caffeinated#example.com', 'My Subject', $message);
?>
Which software do i have to install to email through php on windows 8? sendmail, msmtp or ssmtp?
Try this
Configure This Setups
in php.ini
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id#gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
in sendmail.ini:
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=my-gmail-id#gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id#gmail.com
Important: comment following line if there is another sendmail_path in the php.ini : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
Note: Tested and works fine in my Windows 8.1
Possible solution. See this question
For me configuring a mail client on localhost is quite difficult. I also tried quite a few times. Later I moved on to other solutions.
You can use SwiftMailer or PhpMailer with some configuration or you can try this tool with zero configuration.
On a side note, if you are using your windows PC for development and not as a production server, then I suggest that you don't bother setting up a sendmail in windows, just use this handy tool.
Test Mail Server Tool (Its Free)
It will emulate an email server and once any script tries to send an email, it will intercept it and open it for you as an .eml file, which you can open up in any email reader like outlook or mail viewer(again its free).
Now setting this tool up is just a breez, and you will thank me later for all the time that you saved, from not having to manually setup the sendmail, which I must mention is meant to be on a linux machine. ;)
I'd recommend mercury (http://www.pmail.com/downloads_s3_t.htm - Mercury/32 Mail Transport System for Win32 and NetWare Systems v4.74).
This is included in XAMPP, fairly easy to set up and you don't need to configure or (ab)use an email account. You can see the whole smtp transaction in mercury mail log window.
Look here for an excellent answer on how to setup mailing from php: PHP mail form doesn't complete sending e-mail
Use this function tool:
https://github.com/PHPMailer/PHPMailer
Mail() is prette dificult to use and this function allows you to use the email servers STMP function to send emails.
Read documentation here:
https://github.com/PHPMailer/PHPMailer/blob/master/README.md
You need to use email server along with php.
https://www.hmailserver.com/
When you are using an e-mail sender functionality through a server that requires SMTP
Authentication, you must need to specify it. And set the host, username and
password (and maybe the port if it is not the default one - 25).
For example, I usually use PHPMailer with similar settings to this ones:
//ini settings
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "YOURMAIL#gmail.com");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; //Your SMTP account username example
$mail->Password = "password"; //Your SMTP account password example
You can find more about PHPMailer here.
You can video ride for how SMTP configure on wondows here.
I am using localhost WAMP server (Windows) for webhosting. I am getting the following error.
phpmailer ERROR :Could not instantiate mail function.
I have tried many solutions but nothing worked for me.
PHP mailing script
$mailer = new PHPMailer();
$mailer->IsMAIL();
$mailer->CharSet = 'utf-8';
$mailer->AddAddress($formvars['email'],$formvars['name']);
$mailer->Subject = "Your registration with ".$this->sitename;
$mailer->From = $this->GetFromAddress();
$confirmcode = $formvars['confirmcode'];
$confirm_url = $this->GetAbsoluteURLFolder().'/confirmreg.php?code='.$confirmcode;
$mailer->Body ="Hello ".$formvars['name']."\r\n\r\n".
"Thanks for your registration with ".$this->sitename."\r\n".
"Please click the link below to confirm your registration.\r\n".
"$confirm_url\r\n".
"\r\n".
"Regards,\r\n".
"Webmaster\r\n".
$this->sitename;
if(!$mailer->Send())
{
$this->HandleError("Failed sending registration confirmation email.".$mailer->ErrorInfo);
//echo "Mailer Error: " . $mailer->ErrorInfo;
return false;
}
return true;
How do I diagnose this error?
$mailer->IsMAIL(); tells PHPMailer to use the PHP mail() function for emails delivery. It works out of the box on Unix-like OSes but not on Windows because the desktop versions of Windows do not install a SMTP server (it is available on the installation disc).
In order to make it work on Windows you have to change the PHP configuration. Either you edit php.ini (the configuration will apply globally) or you can use function ini_set() to change it only for current execution of the current script.
The best way is to change php.ini. You can find it in the PHP's installation directory or in the Windows's directory. Check the output of PHP function phpinfo() to find out its exact location.
Open php.ini in a text editor (the one you use to write the code is the best) and search for a section that looks like this:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me#example.com
For SMTP, use the name or the IP address of your company's mail server. Or your ISP's mail server if you are at home. Or check the configuration of your email client.
The smtp_port is either 25 for standard SMTP or 587 if the server uses SSL to encrypt its communication.
Also uncomment the sendmail_from setting if it is commented out (remove the ; from the beginning of line) and put your email address there.
Read more about the PHP configuration for sending emails on Windows on the documentation page.
Another option is to install a SMTP server on the computer. You can find one on the Windows installation disc or you can use a third-party solution.
If you choose to not install a SMTP server on the computer you don't even need to modify php.ini. You can change the code of the script to use a SMTP server:
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = "mail.example.com";
$mailer->Port = 25;
Check this PHPmailer example for all the settings and their meaning.
If the SMTP server uses SMTP authentication (the webmail providers do it, your ISP or your company might do it or might not do it) then you have to also add:
$mail->SMTPAuth = true;
$mail->Username = "yourname#example.com";
$mail->Password = "yourpassword";
If you use Gmail's SMTP server, for example, then use your Gmail email address and password. Replace "Gmail" with "Hotmail" or "Yahoo!" or your webmail provider or your company in the sentence above.
Configuring a working mail client from localhost is always a mission and often "unecessary" considering it's just a sandbox. Although some developers want to solve the puzzle (I am one of those), some look for an easier alternative.
I would suggest that you install HMailServer
I have seen lots of people having great luck at a very fast speed using hmail.
Check this post for the step by step: How to send email from localhost WAMP Server to send email Gmail Hotmail or so forth?
I'm using the mail() basic example modified slightly for my user id and I'm getting the error "Mailer Error: Could not instantiate mail function"
if I use the mail function -
mail($to, $subject, $message, $headers);
it works fine, though I'm having trouble sending HTML, which is why I'm trying PHPMailer.
this is the code:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
print ($body ); // to verify that I got the html
$mail->AddReplyTo("reply#example.com","my name");
$mail->SetFrom('from#example.com', 'my name');
$address = "to#example.com";
$mail->AddAddress($address, "her name");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($body);
$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!";
}
?>
Try using SMTP to send email:-
$mail->IsSMTP();
$mail->Host = "smtp.example.com";
// optional
// used only when SMTP requires authentication
$mail->SMTPAuth = true;
$mail->Username = 'smtp_username';
$mail->Password = 'smtp_password';
Your code looks good, did you forget to install PostFix on your server?
sudo apt-get install postfix
It worked for me ;)
Cheers
This worked for me
$mail->SetFrom("from#domain.co","my name", 0); //notice the third parameter
$mail->AddAddress($address, "her name");
should be changed to
$mail->AddAddress($address);
This worked for my case..
You need to make sure that your from address is a valid email account setup on that server.
If you are sending file attachments and your code works for small attachments but fails for large attachments:
If you get the error "Could not instantiate mail function" error when you try to send large emails and your PHP error log contains the message "Cannot send message: Too big" then your mail transfer agent (sendmail, postfix, exim, etc) is refusing to deliver these emails.
The solution is to configure the MTA to allow larger attachments. But this is not always possible. The alternate solution is to use SMTP. You will need access to a SMTP server (and login credentials if your SMTP server requires authentication):
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.example.com"; // set the SMTP server
$mail->Port = 26; // set the SMTP port
$mail->Username = "johndoe#example.com"; // SMTP account username
$mail->Password = "********"; // SMTP account password
PHPMailer defaults to using PHP mail() function which uses settings from php.ini which normally defaults to use sendmail (or something similar). In the above example we override the default behavior.
The PHPMailer help docs on this specific error helped to get me on the right path.
What we found is that php.ini did not have the sendmail_path defined, so I added that with sendmail_path = /usr/sbin/sendmail -t -i;
In my case, it was the attachment size limit that causes the issue. Check and increase the size limit of mail worked for me.
Seems in my case it was just SERVER REJECTION. Please check your mail server log / smtp connection accessibility.
I had this issue, and after doing some debugging, and searching I realized that the SERVER (Godaddy) can have issues.
I recommend you contact your Web hosting Provider and talk to them about Quota Restrictions on the mail function (They do this to prevent people doing spam bots or mass emailing (spam) ).
They may be able to advise you of their limits, and if you're exceeding them. You can also possibly upgrade limit by going to private server.
After talking with GoDaddy for 15 minutes the tech support was able to resolve this within 20 minutes.
This helped me out a lot, and I wanted to share it so if someone else comes across this they can try this method if all fails, or before they try anything else.
An old thread, but it may help someone like me. I resolved the issue by setting up SMTP server value to a legitimate value in PHP.ini
I had this issue as well. My solution was to disable selinux. I tried allowing 2 different http settings in selinux (something like httpd_allow_email and http_can_connect) and that didn't work, so I just disabled it completely and it started working.
I was having this issue while sending files with regional characters in their names like: VęryRęgióńął file - name.pdf.
The solution was to clear filename before attaching it to the email.
Check if sendmail is enabled, mostly if your server is provided by another company.
For what it's worth I had this issue and had to go into cPanel where I saw the error message
"Attention! Please register your email IDs used in non-smtp mails through cpanel plugin. Unregistered email IDs will not be allowed in non-smtp emails sent through scripts. Go to Mail section and find "Registered Mail IDs" plugin in paper_lantern theme."
Registering the emails in cPanel (Register Mail IDs) and waiting 10 mins got mine to work.
Hope that helps someone.
A lot of people often overlook the best/right way to call phpmailer and to put these:
require_once('../class.phpmailer.php');
or, something like this from the composer installation:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once "../../vendor/autoload.php";
on TOP of the page before calling or including anything else. That causes the "Could not instantiate mail function"-error by most folks.
Best solution: put all mail handling in a different file to have it as clean as possible. And always use SMTP.
If that is not working, check your DNS if your allowed to send mail.
My config: IIS + php7.4
I had the same issue as #Salman-A, where small attachments were emailed but large were causing the page to error out.
I have increased file and attachments limits in php.ini, but this has made no difference.
Then I found a configuration in IIS(6.0), and increased file limits in there.
Also here is my mail.php:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require '../_PHPMailer-master/src/Exception.php';
require '../_PHPMailer-master/src/PHPMailer.php';
try{
$email = new PHPMailer(true);
$email->SetFrom('internal#example.com', 'optional name');
$email->isHTML(true);
$email->Subject = 'my subject';
$email->Body = $emailContent;
$email->AddAddress( $eml_to );
$email->AddAttachment( $file_to_attach );
$email->Send();
}catch(phpmailerException $e){echo $e->errorMessage();}
future.
We nee to change the values of 'SMTP' in php.ini file
php.ini file is located into
EasyPHP-DevServer-14.1VC11\binaries\php\php_runningversion\php.ini