Sending email with PHP from an SMTP server
I have trouble sending email in PHP. I get an error: SMTP server response: 530 SMTP authentication is required . I was under the impression that you can send email without SMTP to verify. I know that this mail will propably get filtered out, but that doesn’t matter right now.
[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 = someonelse@example.com
This is the setup in the php.ini file. How should I set up SMTP? Are there any SMTP servers that require no verification or must I setup a server myself?
9 Answers 9
When you are sending an e-mail through a server that requires SMTP Auth, you really 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:
$mail = new PHPMailer(); // Settings $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"; // SMTP account username example $mail->Password = "password"; // SMTP account password example // Content $mail->setFrom('domain@example.com'); $mail->addAddress('receipt@domain.com'); $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body in bold!'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send();
It’s worth noting for those stumbling on this answer that PHPMailer is also built into WordPress and can be configured using the ‘phpmailer_init’ action hook. It’s a convenient way to set up WordPress for SMTP mail or Amazon SES (which supports SMTP connections).
@Luka Yes, it is. According to their license file PHPMailer uses the LGPL 2.1 license, which allows commercial usage.
Do I need to do anything special to use this code? Where do I put this? Can I call it with an HTML5 form with a POST request? How do I send an Email once I created this PHPMailer object?
This is the best answer for godaddy php mail() function issues — 2017 — shouldn’t have to download PHPMailer or some other 3rd party resource — thanks
For Unix users, mail() is actually using Sendmail command to send email. Instead of modifying the application, you can change the environment. msmtp is an SMTP client with Sendmail compatible CLI syntax which means it can be used in place of Sendmail. It only requires a small change to your php.ini.
sendmail_path = "/usr/bin/msmtp -C /path/to/your/config -t"
Then even the lowly mail() function can work with SMTP goodness. It is super useful if you’re trying to connect an existing application to mail services like sendgrid or mandrill without modifying the application.
MSMTP is also available for Windows. The obvious downloads have version 1.4. The version I found somewhere is 1.6.2. Don’t know if there is a 1.8.6 for Windows.
The problem is that PHP mail() function has a very limited functionality. There are several ways to send mail from PHP.
- mail() uses SMTP server on your system. There are at least two servers you can use on Windows: hMailServer and xmail. I spent several hours configuring and getting them up. First one is simpler in my opinion. Right now, hMailServer is working on Windows 7 x64.
- mail() uses SMTP server on remote or virtual machine with Linux. Of course, real mail service like Gmail doesn’t allow direct connection without any credentials or keys. You can set up virtual machine or use one located in your LAN. Most linux distros have mail server out of the box. Configure it and have fun. I use default exim4 on Debian 7 that listens its LAN interface.
- Mailing libraries use direct connections. Libs are easier to set up. I used SwiftMailer and it perfectly sends mail from Gmail account. I think that PHPMailer is pretty good too.
No matter what choice is your, I recommend you use some abstraction layer. You can use PHP library on your development machine running Windows and simply mail() function on production machine with Linux. Abstraction layer allows you to interchange mail drivers depending on system which your application is running on. Create abstract MyMailer class or interface with abstract send() method. Inherit two classes MyPhpMailer and MySwiftMailer . Implement send() method in appropriate ways.
There are some SMTP servers that work without authentication, but if the server requires authentication, there is no way to circumvent that.
PHP’s built-in mail functions are very limited — specifying the SMTP server is possible in WIndows only. On *nix, mail() will use the OS’s binaries.
If you want to send E-Mail to an arbitrary SMTP server on the net, consider using a library like SwiftMailer. That will enable you to use, for example, Google Mail’s outgoing servers.
In cases where you are hosting a WordPress site on Linux and have server access, you can save some headaches by installing msmtp which allows you to send via SMTP from the standard PHP mail() function. msmtp is a simpler alternative to postfix which requires a bit more configuration.
sudo apt-get install msmtp-mta ca-certificates
Create a new configuration file:
. with the following configuration information:
# Set defaults. defaults # Enable or disable TLS/SSL encryption. tls on tls_starttls on tls_trust_file /etc/ssl/certs/ca-certificates.crt # Set up a default account's settings. account default host port 587 auth on user password from syslog LOG_MAIL
You need to replace the configuration data represented by everything within «» (inclusive, remove these). For host/username/password, use your normal credentials for sending mail through your mail provider.
sudo nano /etc/php5/apache2/php.ini
sendmail_path = /usr/bin/msmtp -t
Complete documentation can be found here:
This is a very nice solution, thank you. For CentOS don’t forget to allow sending e-mail on web layer for selinux by setsebool -P httpd_can_sendmail 1
I know this is an old question but it’s still active and all the answers I saw showed basic authentication, which is deprecated. Here is an example showing how to send via Google’s Gmail servers using PHPMailer with XOAUTH2 authentication:
//Import PHPMailer classes into the global namespace use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\OAuth; //Alias the League Google OAuth2 provider class use League\OAuth2\Client\Provider\Google; //SMTP needs accurate times, and the PHP time zone MUST be set //This should be done in your php.ini, but this is how to do it if you don't have access to that date_default_timezone_set('Etc/UTC'); //Load dependencies from composer //If this causes an error, run 'composer install' require '../vendor/autoload.php'; //Create a new PHPMailer instance $mail = new PHPMailer(); //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging //SMTP::DEBUG_OFF = off (for production use) //SMTP::DEBUG_CLIENT = client messages //SMTP::DEBUG_SERVER = client and server messages $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; //Set the SMTP port number: // - 465 for SMTP with implicit TLS, a.k.a. RFC8314 SMTPS or // - 587 for SMTP+STARTTLS $mail->Port = 465; //Set the encryption mechanism to use: // - SMTPS (implicit TLS on port 465) or // - STARTTLS (explicit TLS on port 587) $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Set AuthType to use XOAUTH2 $mail->AuthType = 'XOAUTH2'; //Fill in authentication details here //Either the gmail account owner, or the user that gave consent $email = 'someone@gmail.com'; $clientId = 'RANDOMCHARS-----duv1n2.apps.googleusercontent.com'; $clientSecret = 'RANDOMCHARS-----lGyjPcRtvP'; //Obtained by configuring and running get_oauth_token.php //after setting up an app in Google Developer Console. $refreshToken = 'RANDOMCHARS-----DWxgOvPT003r-yFUV49TQYag7_Aod7y0'; //Create a new OAuth2 provider instance $provider = new Google( [ 'clientId' => $clientId, 'clientSecret' => $clientSecret, ] ); //Pass the OAuth provider instance to PHPMailer $mail->setOAuth( new OAuth( [ 'provider' => $provider, 'clientId' => $clientId, 'clientSecret' => $clientSecret, 'refreshToken' => $refreshToken, 'userName' => $email, ] ) ); //Set who the message is to be sent from //For gmail, this generally needs to be the same as the user you logged in as $mail->setFrom($email, 'First Last'); //Set who the message is to be sent to $mail->addAddress('someone@gmail.com', 'John Doe'); //Set the subject line $mail->Subject = 'PHPMailer GMail XOAUTH2 SMTP test'; //Read an HTML message body from an external file, convert referenced images to embedded, //convert HTML into a basic plain-text alternative body $mail->CharSet = PHPMailer::CHARSET_UTF8; $mail->msgHTML(file_get_contents('contentsutf8.html'), __DIR__); //Replace the plain text body with one created manually $mail->AltBody = 'This is a plain-text message body'; //Attach an image file $mail->addAttachment('images/phpmailer_mini.png'); //send the message, check for errors if (!$mail->send()) < echo 'Mailer Error: ' . $mail->ErrorInfo; > else
How to send email from localhost using PHP on Linux
I know that to send e-mail from localhost on Windows, you need to change SMTP server in php.ini however this is valid only on Windows:
[mail function] ; For Win32 only. ; http://php.net/smtp SMTP = localhost ; http://php.net/smtp-port smtp_port = 25
In linux by default their is no configuration required to send mail. What is the output of php mail function ?. Try mail command on terminal and check mail is correctly configured linux.about.com/od/commands/l/blcmdl1_Mail.htm
@nidhin mail($to,$subject,$message,$headers); gives nothing, no error but if $send = mail($to,$subject,$message,$headers); and echo $send; it shows nothing again so it means it’s FALSE
i guess this one will help you much better way to send email by using your gmail credentials. kvcodes.com/2016/03/send-e-mail-localhost-ubuntu-php
7 Answers 7
I would suggest installing ssmtp rather than installing a full mail server like postfix. If this is just a local test environment, you probably don’t need a full MTA. ssmtp is very easy to setup—you just supply your smtp credentials for a remote server. There’s a tutorial here.
This worked for me on Linux Mint 17 for sending emails from the localhost:
sudo apt-get install sendmail
If you’re running Debian and variants thereof (*buntu, etc.), you can install a mail server by running sudo tasksel install mail-server , which should set you up with basic email capabilities. You can test this by running in command line echo ‘body’ | sendmail recipient@example.net , or as others have mentioned, mail($to, $subj, $msg) in PHP.
When i was installing I have chosen by mistake no configuration, how i can change configuration? And maybe you can offer which one should I take
well I found out how to change setting, but my mail was delivered ONLY in 2 days, maybe I made wrong settings, can you help me?
To send mail from localhost (WAMP, XAMP, or LAMP) you can use PHPMailer package (Download PHPMailer from here).
First you have to edit the «php.ini» To find this file display the phpinfo by using following code from the WAMP server. Create one php file and add this content.
There search for «Loaded Configuration File» That will be the path to your php.ini.
In this file remove the ;(semi colon) given to extension=php_openssl.dll.
After downloading PHPMailerX.X.X package
Extract->Copy the full folder into your project folder.
In test folder there is one file called testemail.php.
Change the parameter as your need. (Example given below).
Then in the browser type 127.0.0.1/PHPMailer/test/testemail.php.
Then it will show successful message if email sent, else it will give error message. Example:
//add these codes if not written $mail->IsSMTP(); $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; //You have to change these parameters to your requirements. //…….code…. $mail->Username = “abc@gmail.com”; // GMAIL username $mail->Password = “abcdefgh”; // GMAIL password //……..code….. There are many other functions to attach file etc.. For that refer doc file. $mail->AddAddress(“destination@gmail.com”,”Nick name”); //…….code…..