Mail function not working - PHP - php

I am new to PHP and just started learning it. I am trying to send a test mail to my gmail account using the mail() function.
Here is my code:
$message = "Hello PHP!";
mail("mygmailaccount#gmail.com", "PHP Test", $message);
But its not working. This is what the error looks like:
Click here to view it in a webpage.
I have seen lots of similar questions and I have also tried all the solutions mentioned on SO.
I have made changes to my php.ini file as shown below.
[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 = mygmailaccount#gmail.com
I am using WAMP server on a Windows 7 (64-bit) machine.
Please help me solve this. Thanks!

https://github.com/PHPMailer/PHPMailer
This is the official PHPMailer.
all you have to do is upload all the files and folders to a folder with the mailing php file(the file which have the code shown by you). Then look at the example folder or look at the example folders if you need smtp.You should have figured it out , looks like you got good programming skill.If you have problem , comment to informed me.
PS. mail() function sometimes not reliable. I face that problem often until I use phpmailer for my own system.
EDIT: Put it altogether like this. send.php is the file I'm going to write the followling code.
Next , the send.php code!!
<?php
require 'PHPMailerAutoload.php';
$sendto ="destinationemail";//Input your own
$sendfrom ="yourmaskedmail";//input your own
$topic ="test";
$passage ="test";
$mail = new PHPMailer(true);
//Send mail using gmail
if($send_using_gmail){
$mail->IsSMTP(); // telling the class to use SMTP
$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 = "*hidden*"; // GMAIL username Input your own
$mail->Password = "*hidden"; // GMAIL password Input your own
}
//Typical mail data
$mail->AddAddress($sendto,$sendto);
$mail->SetFrom($sendfrom,$sendfrom);
$mail->Subject =$topic;
$mail->Body =$passage;
try{
$mail->Send();
echo "Success! This email has been sent to ".$sendto. " in the name of ".$sendfrom;
} catch(Exception $e){
//Something went bad
echo "Unable to process your request now, please try again later";
}
?>
Change the mailsender,mailreciever and contents of the mail. Also , input your gmail username and password. After that, run the php, wait for your email to come. I've just test it 8 minutes ago and it worked.

Have a look at this page:
http://www.php.net/manual/en/mail.configuration.php
Short version: to make your php.mail function work, you need to figure out how to send an e-mail using your server and make sure your configuration in php.ini is up to date. (For details, see the link above).
Does your server run sendmail or another mail agent? Or doesn't it run anything like that at all?
If so, make sure the paths are set correctly.
If not, use a third party SMTP service. GMail offers it, but the problem is that it requires authentication, which php.mail() doesn't support. If you still want to use it, see this post for alternatives to php.mail().

Related

phpmailer ERROR :Could not instantiate mail function

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?

How to mail a log file to yourself using PHP from a Windows machine ?

Situation:
I run a PHP script from a windows 7 machine using CLI.
My script will do its thing and then generate 1 log file at the end.
I already got all that part working.
In addition to that, I want to email myself that log file every time the script runs.
Somewhere in the bottom of my script I tried this :
mail('email#gmail.com', 'Report', strip_tags($response). PHP_EOL );
The script run all the way to the bottom, I got my log file to generate, I also got a report on my CLI as well, but I never receive any email.
Result :
I am not sure is because I :
am on a Windows.
need to allow specific php extension
Need to configure more setting in my php.ini.
Can someone help clarify this issue ?
You need a Mail Server which is configured in the PHP.ini to send your mails.
Here is a short Tutorial:
http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx
Note that the IIS6 console still needed for the Mail Server, also if you're hosting on >=IIS7.
Also you need to make sure, your Mail Server is acceptet by the Mail Server you want to send this mail to. This is definitve not a trival task. Gmail and GMX for example never accept it, if you didn't have Reverse DNS correctly configured.
If you don't know how, I highly recommend to have a talk to your System Administrator. Mail Server are very hard to setup correctly. It's a task I work around.
But here are the good news, if you do not want to configure your own mail server. It is very simple with an Hosted Email Adress and SMTP, if your using the Open Source Project PHPMailer:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//More in the Documentation
It has a powerfull SMTP Class, which helps extreme for login into any SMTP Account (Gmail for example).

PHPMailer - SMTP ERROR: Password command failed when send mail from my server

I have used phpmailer() concept to send mail to users from my shared server using php script, but I'm not able to send even though everything is right in my script according to phpmailer code.
My code like this:
$message = " This is testing message from my server";
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->Host = "smtp.gmail.com";
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "moorthi.mrk10#gmail.com"; // My gmail username
$mail->Password = "************"; // My Gmail Password
$mail->SetFrom("moorthi.mrk10#gmail.com");
$mail->Subject = "Test Mail from my Server";
$mail->Body = $message;
$mail->AddAddress($email);
if($mail->Send())
{
print json_encode("SUCCESS");
}
else
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
Note: I have used " GMail " as my SMTP server and SMTPSecure is " ssl " and port is "465" and username & passwords are my GMail username & password
I used VPS shared server and I kept my php script on that server.
I think there is no problem in my php script and I don't know why it doesn't work.
I got the ERROR like this.
2014-02-21 12:30:11 CLIENT -> SERVER: EHLO jkcorporates.com
2014-02-21 12:30:11 CLIENT -> SERVER: AUTH LOGIN
2014-02-21 12:30:11 CLIENT -> SERVER: bW9vcnRoaS5tcmsxMEBnbWFpbC5jb20=
2014-02-21 12:30:11 CLIENT -> SERVER: OTk0MTI0MTE0MA==
2014-02-21 12:30:11 SMTP ERROR: Password command failed: 534-5.7.14
534-5.7.14 i-_eumA> Please log in via your web browser and then try again.
534 5.7.14 54 k76sm17979938yho.18 - gsmtp
2014-02-21 12:30:11 CLIENT -> SERVER: QUIT
" The ERROR is " SMTP connect() failed.
Please give some solution for that.
Remember: I use Shared Server Name 'VPS.mydomain.com' and I want to use GMail as my SMTP server to send mail to users.
A bit late, but perhaps someone will find it useful.
Links that fix the problem (you must be logged into google account):
https://security.google.com/settings/security/activity?hl=en&pli=1
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Some explanation of what happens:
This problem can be caused by either 'less secure' applications trying to use the email account (this is according to google help, not sure how they judge what is secure and what is not) OR if you are trying to login several time in a row OR if you change countries (for example use VPN, move code to different server or actually try to login from different part of the world).
To resolve I had to: (first time)
login to my account via web
view recent attempts to use the account and accept suspicious access: THIS LINK
disable the feature of blocking suspicious apps/technologies: THIS LINK
This worked the first time, but few hours later, probably because I was doing a lot of testing the problem reappeared and was not fixable using the above method. In addition I had to clear the captcha (the funny picture, which asks you to rewrite a word or a sentence when logging into any account nowadays too many times) :
after login to my account I went HERE
Clicked continue
Use this:
https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://security.google.com/settings/security/activity?hl=en&pli=1
this link allow acces to google account
UPDATE 19-05-2017:
These url you must to visit from the IP address that will be send email
Solved the problem - PHPMailer - SMTP ERROR: Password command failed when send mail from my server
require_once('class.phpmailer.php');
include("class.smtp.php");
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$messageField = $_POST['message'];
$phoneField = $_POST['contactno'];
$cityField = $_POST['city'];
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
$body .= $nameField;
try {
//$mail->Host = "mail.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail->Username = "xxxxx#gmail.com"; // GMAIL username
$mail->Password = "********"; // GMAIL password
$mail->AddAddress('sendto#gmail.com', 'abc');
$mail->SetFrom('xxxxxx#gmail.com', 'def');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($body);
$mail->Send();
echo "Message Sent OK</p>\n";
header("location: ../test.html");
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Important:
Go to google Setting and do 'less secure' applications enables. It will work.
It Worked for Me.
As others already suggested, you can enable the "less secure" applications or you can simply switch from ssl to tls:
$mailer->Host = 'tls://smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = "xxx#gmail.com";
$mailer->Password = "***";
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;
When using tls there's no need to grant access for less secure applications, just make sure, IMAP is enabled.
Login to your Gmail account using the web browser.
Click on this link to enable applications to access your account: https://accounts.google.com/b/0/DisplayUnlockCaptcha
Click on Continue button to complete the step.
Now try again to send the email from your PHP script. It should work.
I face the same problem, and think that I do know why this happens.
The gmail account that I use is normally used from India, and the webserver that I use is located in The Netherlands.
Google notifies that there was a login attempt from am unusualy location and requires to login from that location via a web browser.
Furthermore I had to accept suspicious access to the gmail account via https://security.google.com/settings/security/activity
But in the end my problem is not yet solved, because I have to login to gmail from a location in The Netherlands.
I hope this will help you a little! (sorry, I do not read email replies on this email address)
Just in caser anyone ends here like me.
In may case despite having enabled unsecure access to my google account, it refused to send the email throwing an SMTP ERROR: Password command failed: 534-5.7.14.
(Solution found at https://know.mailsbestfriend.com/smtp_error_password_command_failed_5345714-1194946499.shtml)
Steps:
log into your google account
Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha and click continue to enable.
Setup your phpmailer as smtp with ssl:
$mail = new PHPMailer(true);
$mail->CharSet ="utf-8";
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // or 0 for no debuggin at all
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPAuth = true;
$mail->Username = 'yourgmailaccount';
$mail->Password = 'yourpassword';
And the other $mail object properties as needed.
Hope it helps someone!!
You need to use an APP password.
Visit this link to view how to create one.
For those who are still unable to get it working, try the following in addition to the method provided by #CallMeBob.
PHP.ini
Go to C:\xampp\php , edit php.ini file with notepad.
Press CTRL+F on your keyboard, input sendmail_path on the search bar and click Find Next twice.
Right now, you should be at the [mail munction] section.
Remove the semicolon for this line:
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Add a semicolon for this line:
sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
SendMail.ini
Go to C:\xampp\sendmail, edit sendmail.ini file with notepad
Change the following:
smtp_server=smtp.gmail.com
smtp_port=465
auth_username=your-gmail-username#gmail.com
auth_password=your-gmail-password
force_sender=your-gmail-username#gmail.com
Note:
** smtp_port must tally with your those written in your php code.
** Remember to change your-gmail-username and your-gmail-password to whichever account you are using.
**
Hope this helps! :)
If you are G Suit user it can be solved by Administrator
Go to your Admin panel
Type in top search bar «Security» (Select Security with Shield icon)
Open Basic settings
Goto Less secure apps section
Press: Go to settings for less secure apps ››
And now select one of Radio Button
a) Disable access to less secure apps for all users (Recommended)
b) Allow users to manage their access to less secure apps
c) Enforce access to less secure apps for all users (Not Recommended)
Usually It does not working because of a)! And will start working immediately with c) option. b) – option will need more configuration for each user in GSuit
Hope it helps
This error is due to more security features of gmail..
Once this error is generated...Please login to your gmail account..there you can find security alert from GOOGLE..follow the mail...check on click for less secure option..Then try again phpmailer..
Your mail won't be sent online unless you complete the two-step verification for your g-mail account and use that password.
after publishing the code to the server, I am using gmail account to send a mail.
I followed the following steps to solve the issue
https://www.google.com/settings/u/1/security/lesssecureapps
https://security.google.com/settings/security/activity?hl=en&pli=1
if you added the 2 step verification then you need to remove to make it work

Phpmailer error "Could not instantiate mail function"

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

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