Sending email with PHP from an SMTP server - php

$from = "someonelse#example.com";
$headers = "From:" . $from;
echo mail ("borutflis1#gmail.com" ,"testmailfunction" , "Oj",$headers);
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?

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 <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
You can find more about PHPMailer here: https://github.com/PHPMailer/PHPMailer

<?php
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "YOURMAIL#gmail.com");
$message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = YourMail#address.com";
$headers = "From: YOURMAIL#gmail.com";
mail("Sending#provider.com", "Testing", $message, $headers);
echo "Check your email now....<BR/>";
?>
or, for more details, read on.

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.

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.
Here are the steps:
Install msmtp
sudo apt-get install msmtp-mta ca-certificates
Create a new configuration file:
sudo nano /etc/msmtprc
...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 <smtp.example.net>
port 587
auth on
user <username#example.net>
password <password>
from <address-to-receive-bounces#example.net>
syslog LOG_MAIL
You need to replace the configuration data represented by everything within "<" and ">" (inclusive, remove these). For host/username/password, use your normal credentials for sending mail through your mail provider.
Tell PHP to use it
sudo nano /etc/php5/apache2/php.ini
Add this single line:
sendmail_path = /usr/bin/msmtp -t
Complete documentation can be found here:
https://marlam.de/msmtp/

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 {
echo 'Message sent!';
}
Reference: PHPMailer examples folder

For another approach, you can take a file like this:
From: Sunday <sunday#gmail.com>
To: Monday <monday#gmail.com>
Subject: Day
Tuesday Wednesday
and send like this:
<?php
$a1 = ['monday#gmail.com'];
$r1 = fopen('a.txt', 'r');
$r2 = curl_init('smtps://smtp.gmail.com');
curl_setopt($r2, CURLOPT_MAIL_RCPT, $a1);
curl_setopt($r2, CURLOPT_NETRC, true);
curl_setopt($r2, CURLOPT_READDATA, $r1);
curl_setopt($r2, CURLOPT_UPLOAD, true);
curl_exec($r2);
https://php.net/function.curl-setopt

I created a simple lightweight SMTP email sender for PHP if anybody needs it. Here is the URL:
https://github.com/Nerdtrix/EZMAIL
It was tested in both environments, production and development.

Related

PHPMailer not working since uploading website

Since I have uploaded my website to a server I cannot use PHPMailer to send smtp mails. It worked before on my local server (XAMPP). I am using GMAIL so I also changed all the required gmail settings (i.a. I created another OAuth2 token). Nevertheless, it just won't work...
The PHP code I am using (and which is also working offline) is the following:
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//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/phpmailer/phpmailer/PHPMailerAutoload.php';
//Load dependencies from composer
//If this causes an error, run 'composer install'
require 'vendor/autoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailerOAuth;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Set AuthType
$mail->AuthType = 'XOAUTH2';
//User Email to use for SMTP authentication - Use the same Email used in Google Developer Console
$mail->oauthUserEmail = "mygmail#gmail.com";
//Obtained From Google Developer Console
$mail->oauthClientId = "IWONTTELLU.apps.googleusercontent.com";
//Obtained From Google Developer Console
$mail->oauthClientSecret = "IWONTTELLU";
//Obtained By running get_oauth_token.php after setting up APP in Google Developer Console.
//Set Redirect URI in Developer Console as [https/http]://<yourdomain>/<folder>/get_oauth_token.php
// eg: http://localhost/phpmail/get_oauth_token.php
$mail->oauthRefreshToken = "1/IWONTTELLU";
//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('mygmail#gmail.com', 'BLA');
//Set who the message is to be sent to
$mail->addAddress('my#privatemail.com', 'Hi');
//Set the subject line
$mail->Subject = 'PHPMailer GMail 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->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//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 {
echo "Message sent!";
}
?>
When I try to use it online, it first of all takes very long until I get an error (tried to use gethostbyname('smtp.gmail.com'), too; long story short: didn't work, too) and secondly, the code I get is Error: Failed to connect to server: Connection timed out (110) & SMTP connect() failed. The service I am using is provided by 1and1 (1und1/1&1). I've been trying to fix this for more than 4 hours now and can't find out what is wrong. Less secure apps can access and I also created a token and "created" a refresh token as in https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2 . The Troubleshooting guide (https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting) couldn't really help me as well although I think it has problems with authenticating... I have also tried to use ssl (of course I also changed the port etc.) which didn't work either.
Is port 587 still supported by gmail? You might want to you SSL port 465. This article might also be useful https://www.wpsitecare.com/gmail-smtp-settings/ or perhaps 1&1 is being blocked by gmail? See this post http://docs.mailpoet.com/article/61-sending-with-gmail-doesnt-work

php mail() function not working to send an email [duplicate]

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.

Send mail with php and gmail smtp? [duplicate]

$from = "someonelse#example.com";
$headers = "From:" . $from;
echo mail ("borutflis1#gmail.com" ,"testmailfunction" , "Oj",$headers);
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?
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 <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
You can find more about PHPMailer here: https://github.com/PHPMailer/PHPMailer
<?php
ini_set("SMTP", "aspmx.l.google.com");
ini_set("sendmail_from", "YOURMAIL#gmail.com");
$message = "The mail message was sent with the following mail setting:\r\nSMTP = aspmx.l.google.com\r\nsmtp_port = 25\r\nsendmail_from = YourMail#address.com";
$headers = "From: YOURMAIL#gmail.com";
mail("Sending#provider.com", "Testing", $message, $headers);
echo "Check your email now....<BR/>";
?>
or, for more details, read on.
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.
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.
Here are the steps:
Install msmtp
sudo apt-get install msmtp-mta ca-certificates
Create a new configuration file:
sudo nano /etc/msmtprc
...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 <smtp.example.net>
port 587
auth on
user <username#example.net>
password <password>
from <address-to-receive-bounces#example.net>
syslog LOG_MAIL
You need to replace the configuration data represented by everything within "<" and ">" (inclusive, remove these). For host/username/password, use your normal credentials for sending mail through your mail provider.
Tell PHP to use it
sudo nano /etc/php5/apache2/php.ini
Add this single line:
sendmail_path = /usr/bin/msmtp -t
Complete documentation can be found here:
https://marlam.de/msmtp/
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 {
echo 'Message sent!';
}
Reference: PHPMailer examples folder
For another approach, you can take a file like this:
From: Sunday <sunday#gmail.com>
To: Monday <monday#gmail.com>
Subject: Day
Tuesday Wednesday
and send like this:
<?php
$a1 = ['monday#gmail.com'];
$r1 = fopen('a.txt', 'r');
$r2 = curl_init('smtps://smtp.gmail.com');
curl_setopt($r2, CURLOPT_MAIL_RCPT, $a1);
curl_setopt($r2, CURLOPT_NETRC, true);
curl_setopt($r2, CURLOPT_READDATA, $r1);
curl_setopt($r2, CURLOPT_UPLOAD, true);
curl_exec($r2);
https://php.net/function.curl-setopt
I created a simple lightweight SMTP email sender for PHP if anybody needs it. Here is the URL:
https://github.com/Nerdtrix/EZMAIL
It was tested in both environments, production and development.

Help with PHP mail() function [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php mail() function on localhost
I'm trying to do some localhost testing for password recovery on my site, but when I try to send an email, I get the following 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()
Here are the relevant settings in my php.ini file.
; 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 = you#yourdomain
I am not sure what to set these to for localhost testing. I realize that I need to set SMTP to whatever my provider's mail server is, but I work in a shared office building so I don't know how to even find out who provides the internet here.
Thanks in advance.
PHP's mail() function doesn't implement SMTP protocol directly. Instead it relies on sendmail() MTA (SMTP server), or a replacement like postfix or mstmp.
It works fine on Unix as long as MTA installed.
On Windows (from PHP.net manual):
The Windows implementation of mail() differs in many ways from the
Unix implementation. First, it doesn't use a local binary for
composing messages but only operates on direct sockets which means a
MTA is needed listening on a network socket (which can either on the
localhost or a remote machine).
So - the moral of the story - you need to install mail server.
However - if it's for test purpose only - simply get a PHP library that actually implements SMTP protocol and use your regular gmail email address to send emails:
Instead of using PHP's mail() use one of these:
PHPmailer
SwiftMailer
Zend\Mail
These PHP libraries actually implement SMTP protocol so one can easily send emails from any platform, without email server installed on the same machine:
PHPMAILER example:
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$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; // set the SMTP port for the GMAIL server
$mail->Username = "some_email#gmail.com"; // GMAIL username
$mail->Password = "pass111"; // GMAIL password
$mail->SetFrom('some_email#gmail.com', 'My name is slim shady');
$mail->AddReplyTo("some_email#gmail.com","My name is slim shady");
$mail->Subject = "Hey, check out http://www.site.com";
$mail->AltBody = "Hey, check out this new post on www.site.com"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "some_email#gmail.com";
$mail->AddAddress($address, "My name is slim shady");
PHP's mail requires a local mailserver to run.
Edit: As the PHP documentation site for mail() reveils, you may use a Mail package from PEAR.
For testing purposes, I'd recommend setting up a fake mail server like Dumbster.
http://quintanasoft.com/dumbster/

PHP mail function not working

I have written a basic script for the mail functionality.
I am trying to run this script through WAMP server.
<?php
phpinfo();
$to = "mss#xyz.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "mohan.s#xyz.com";
$headers = "From: $from";
$res= mail($to,$subject,$message,$headers);
echo " $res Mail Sent.";
?>
I have set the SMTP, sendmail_from in the php.ini file .
It gives me the following error
Warning: mail() [function.mail]: Failed to connect to mailserver at
"mucse409.eu.xyz.com" port 25, verify your "SMTP" and "smtp_port"
setting in php.ini or use ini_set() in C:\wamp\www\email.php on line 9
Mail Sent.
I am able to ping the SMTP address from my machine. Please guide me.
Can you also send mail from this machine to this smtp server using some mail client like ms outlook or mozilla thunderbird?
I had a problem once that my provider block traffic directed at smtp ports outside due to virus infection, and I couldn't send mail because of this, but I could ping server and port.
Could be blocked by a firewall or some such.
See if you can open port 25 with telnet (If you don't have software for this, you can download putty)
Following this tutorial I was able to send mail link text.
Send email using Gmail and PHPMailer The new automatic update
generator is ready, it has been a long time since OCRALight has been
finished and little bit of this and that has been polished on the
update generation.
The process is fairly complex, it involves reverse-engineering,
data-mining, packaging, distribution and a lot o fighting with our
crappy Windows server that is between me and the final Linux
liberation.
Every step in the road has been automatized, one by one, every problem
has been solved and polished, now the final piece is in his place, the
automatic email generation. Now the updates will be made and send
everyday, even weekends and vacations.
If you are interested in the technical aspect keep reading:
How it has been done:
First of all, you need to have PHP with OpenSSL support, for Windows
you’ll need to Install PHP and carefully select OpenSSL in the
components list, if you already have PHP installed, don’t worry a
re-install will keep your configuration, and you’ll be able to select
OpenSSL.
Then download PHPMailer, and extract it near your main php
file.
You will need to have a Gmail account(obviously) I recommend you to
make a new one just for this, mainly because the configuration need to
be very precise, and you wouldn’t be able to use it freely without
loosing functionality or risking to break the configuration.
Configure your Gmail account to use POP mail, but not IMAP, ONLY POP,
just POP.
And now the code:
<?php
require(”PHPMailer/class.phpmailer.php”);
$update_emails = array(
‘Juan Perez’ => ‘Juan_Perez#jalisco.gob.mx’,
‘Francisco Garcia’ => ‘fgarcia#hotmail.com’,
‘Diana la del Tunel’ => ‘diana#gmail.com’
);
echo “\nSending Update Email\n”;
$mail = new PHPMailer(); // Instantiate your new class
$mail->IsSMTP(); // set mailer to use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Host = “smtp.gmail.com”; // specify main and backup server
$mail->SMTPSecure= ’ssl’; // Used instead of TLS when only POP mail is selected
$mail->Port = 465; // Used instead of 587 when only POP mail is selected
$mail->Username = “youremail#gmail.com”; // SMTP username, you could use your google apps address too.
$mail->Password = “yaourextremelynotlamepassword”; // SMTP password
$mail->From = “youremail#gmail.com”; //Aparently must be the same as the UserName
$mail->FromName = “Your name”;
$mail->Subject = ‘The subject’;
$mail->Body = “The body of your message”;
foreach ($update_emails as $name => $email) {
$mail->AddBcc($email, $name);
}
if(!$mail->Send())
{
echo “There was an error sending the message:” . $mail->ErrorInfo;
exit;
}
echo “Done…\n”;
?>
In this code I send the email to a group of people, thus I use the
“Bcc:” field instead of the “To:” one, to add a “To:” you would use
AddAddress($email, $name).
A possible upgrade would be to use a MySQL database to store the
addresses, and provide a web interface to add and remove
them. for the moment, this is enough.
Soo remember:
PHP with OpenSSL; PHPMailer; Create a Gmail Account; Activate POP Host:
smtp.gmail.com; SMTPAuth=true; SMTPSEcure=ssl; Port: 465; User with Domain;
Password; $Mail->send();

Categories