smtp configuration for php mail - php

I am sending mails from my website by using php mail function. But now it is not working and I contacted our hosting team then they told me to use smtp as they did some changes in server. I don't know how to do it. Current code (with php mail function) is as follows, can anyone help me about the changes which I have to do with this.
<?php
$mail_To="someone#gmail.com";
$headers = "";
$headers .= "From: livetv#muscle-tube.com\n";
$headers .= "Reply-To: livetv#muscle-tube.com\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Mailer: php";
$mail_Subject = " Live TV key";
$mail_Body = "<p>Muscle-tube</p>";
mail($mail_To, $mail_Subject, $mail_Body,$headers);
?>

PHP's mail() function does not have support for SMTP. You're going to need to use something like the PEAR Mail package.
Here is a sample SMTP mail script:
<?php
require_once("Mail.php");
$from = "Your Name <email#blahblah.com>";
$to = "Their Name <otheremail#whatever.com>";
$subject = "Subject";
$body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit...";
$host = "mailserver.blahblah.com";
$username = "smtp_username";
$password = "smtp_password";
$headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if ( PEAR::isError($mail) ) {
echo("<p>Error sending mail:<br/>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message sent.</p>");
}
?>

Note that PHP mail settings come from your php.ini file. The default looks more or less 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 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 =
By editing your php.ini file you should be able to fix the problem without changing your PHP scripts. Also, you can test a connection with the telnet tool and the HELO, MAIL FROM, RCPT TO, DATA, QUIT commands if you directly connect to an SMTP server. With sendmail, you don't even need that, sendmail should know what it's doing (although in your case it probably wasn't and the sendmail settings probably needed a little help.)
Update: in most cases, telnet is not installed anymore because it's considered dangerous (i.e. it gives you a clear text connection which is generally fine on your local network, but not so much to remote computers). Instead, we have nc which is very similar for testing things such as SMTP but doesn't really allow for remote shell connections. That being said, more and more SMTP is going to use encryption as well so the best tool to test is still sendmail.

Since some of the answers give here relate to setting up SMTP in general (and not just for #shinod particular issue where it had been working and stopped), I thought it would be helpful if I updated the answer because this is a lot simpler to do now than it used to be :-)
In PHP 4 the PEAR Mail package is typically already installed, and this really simple tutorial shows you the few lines of code that you need to add to your php file http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
Most hosting companies list the SMTP settings that you'll need. I use JustHost, and they list theirs at https://my.justhost.com/cgi/help/26 (under Outgoing Mail Server)

But now it is not working and I contacted our hosting team then they told me to use smtp
Newsflash - it was using SMTP before. They've not provided you with the information you need to solve the problem - or you've not relayed it accurately here.
Its possible that they've disabled the local MTA on the webserver, in which case you'll need to connect the SMTP port on a remote machine. There are lots of toolkits which will do the heavy lifting for you. Personally I like phpmailer because it adds other functionality.
Certainly if they've taken away a facility which was there before and your paying for a service then your provider should be giving you better support than that (there are also lots of programs to drop in in place of a full MTA which would do the job).
C.

php's email() function hands the email over to a underlying mail transfer agent which is usually postfix on linux systems
so the preferred method on linux is to configure your postfix to use a relayhost, which is done by a line of
relayhost = smtp.example.com
in /etc/postfix/main.cf
however in the OP's scenario I somehow suspect that it's a job that his hosting team should have done

Related

PHP mail() function returns true, but doesn't send mail

I know that this question was asked before.
When I call the PHP mail() function, it returns true.
I checked my php.ini (I'v running CentOS):
SMTP = localhost
smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t -i
mail.add_x_header = On
I read in a forum that I have to install sendmail. So I installed it. Now sites with a mail() function doesn't load anymore. So I removed sendmail, and the mail() function returns true again, but doesn't send the mail.
Any idea?
To send an email you need a SMTP server (local or remote). Actually your mail function just passes the mail to your SMTP server and is this one which really send your email.
In your php.ini appears this line
sendmail_path = /usr/sbin/sendmail -t -i
You should be aware if you use that configuration parameter (from manual):
If set, smtp, smtp_port and sendmail_from are ignored and the
specified command is executed.
But the most important thing here is you just uninstall sendmail so you can expect your mail goes nowhere. I know sendmail was giving you some problems, possibly configuration problems, but now your php.ini configuration is wrong.
How to solve it?
Start removing the sendmail_path parameter from the php.ini.
Install a simple to configure SMTP server like postfix.
Verify postfix is listening at port 22:
netstat -lnt
Try to send a mail from your php mail() function
Verify your mail has been sent correctly (check your /var/log/mail.log or /var/log/mail/mail.log files)
You also can verify the mail is not in the postfix queue:
postqueue -f
i had a similar problem to this; both mail() and wp_mail() functions were returning TRUE, but no email was being sent to my xyz#yahoo.com Email account.
It turns out that Yahoo was blocking these emails as spam. I did not have captcha implemented on my form, and therefore many spam emails were being sent to yahoo which is why they blocked the emails. Emails were sent successfully, but Yahoo was marking them as spam.
make sure this is not the problem in your case.
Please check your DNS,
I get the same problem when test on localhost, but working on real host.
The problem is fake DNS, your email was blocked by google.
If you test with other email service (example: yahoo mail), you will receive as a spam mail.
I found it after check mail log
I had a similar issue. Sendmail was in use. Note these things:
Real sender is where sendmail installed (VPS often).
From field in php mail script (supposed to match sender)
MX records for the domain name (tells what sender to trust)
Sendmail logs could tell a lot (/var/mail in my case). They indeed explain why true is returned despite message is not delivered.
In my case from value in php script was incorrect as well as MX records that didn't trust sender.
I want to mention another possible error is hostname from this guide that could be changed like
hostnamectl set-hostname domain.name
I had issues setting smtp, so i ended up using gmail. You can send mail with PEAR MAIL as instructed here:
$from = "NoReply <noreply#random.com>";
$to = "someone <someone#somemail.com>";
$subject = "my subject";
$body = "my body";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "gmailaccount#gmail.com";
$password = "gmailpass";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
If you use gmail, your $from will be replaced by the email address used to send it.

How to configure PHP to send e-mail?

I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:
$to = "myweb#gmail.com";
$subject = "Test mail";
$message = "My message";
$from = "webp#gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
When I try running the program this is what I get:
Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set().
Please tell me what address to include in the $from variable. Do I need a smtp server for this? How do I send mails using a localhost? Please tell me what exactly to edit in the php.ini file
I am new to all this.. Please help me..
Use PHPMailer instead: https://github.com/PHPMailer/PHPMailer
How to use it:
require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'me.sender#gmail.com';
$mail->Password = '123!##';
$mail->SetFrom('me.sender#gmail.com', $name);
$mail->AddReplyTo('no-reply#mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('abc1#gmail.com', 'title1');
$mail->AddAddress('abc2#gmail.com', 'title2'); /* ... */
$mail->AddAttachment($fileName);
$mail->send();
You need to have a smtp service setup in your local machine in order to send emails. There are many available freely just search on google.
If you own a server or VPS upload the script and it will work fine.
You won't be able to send a message through other people mail servers. Check with your host provider how to send emails. Try to send an email from your server without PHP, you can use any email client like Outook. Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.
Here's the link that gives me the answer and we use gmail:
Install the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip
Modify the php.ini file to use it (commented out the other lines):
mail function
For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 25
For Win32 only.
sendmail_from = <e-mail username>#gmail.com
For Unix only.
You may supply arguments as well (default: sendmail -t -i).
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
(ignore the "Unix only" bit, since we actually are using sendmail)
You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:
sendmail
smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>#gmail.com
configure your php.ini like this
SMTP = smtp.gmail.com
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = smtp.gmail.com
; smtp_port = 465
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster#localhost
To fix this, you must review your PHP.INI, and the mail services setup you have in your server.
But my best advice for you is to forget about the mail() function. It depends on PHP.INI settings, it's configuration is different depending on the platform (Linux or Windows), and it can't handle SMTP authentication, which is a big trouble in current days. Too much headache.
Use "PHP Mailer" instead (https://github.com/PHPMailer/PHPMailer), it's a PHP class available for free, and it can handle almost any SMTP server, internal or external, with or without authentication, it works exactly the same way on Linux and Windows, and it won't depend on PHP.INI settings. It comes with many examples, it's very powerful and easy to use.
This will not work on a local host, but uploaded on a server, this code should do the trick. Just make sure to enter your own email address for the $to line.
<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'your.email#address.com';
$subject = "New Message on YourWebsite.com";
$body = '<html>
<body>
<h2>Title</h2>
<br>
<p>Name:<br>'.$name.'</p>
<p>Email:<br>'.$email.'</p>
</body>
</html>';
//headers
$headers = "From: ".$name." <".$email.">\r\n";
$headers = "Reply-To: ".$email."\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=utf-8";
//send
$send = mail($to, $subject, $body, $headers);
if ($send) {
echo '<br>';
echo "Success. Thanks for Your Message.";
} else {
echo 'Error.';
}
}
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Your Name"><br>
<input type="text" name="email" placeholder="Your Email"><br>
<button type="submit">Subscribe</button>
</form>
</body>
</html>
Usually a good place to start when you run into problems is the manual. The page on configuring email explains that there's a big difference between the PHP mail command running on MSWindows and on every other operating system; it's a good idea when posting a question to provide relevant information on how that part of your system is configured and what operating system it is running on.
Your PHP is configured to talk to an SMTP server - the default for an MSWindows machine, but either you have no MTA installed or it's blocking connections. While for a commercial website running your own MTA robably comes quite high on the list of things to do, it is not a trivial exercise - you really need to know what you're doing to get one configured and running securely. It would make a lot more sense in your case to use a service configured and managed by someone else.
Since you'll be connecting to a remote MTA using a gmail address, then you should probably use Gmail's server; you will need SMTP authenticaton and probably SSL support - neither of which are supported by the mail() function in PHP. There's a simple example here using swiftmailer with gmail or here's an example using phpmailer

PHP Mail() not working on remote server

I am developing an application and have been testing the mail() function in PHP. The following works just fine on my local machine to send emails to myself, but as soon as I try to send it from the testing environment to my local machine, it silently fails.
I will still get the "Mail Sent" message, but no message is sent. I turned on the mail logging in the php.ini file, but even that doesn't seem to be populated after I refresh the page.
Again, the .php files and php.ini files are identical in both environments. Port 25 has been opened on the testing environment, and we are using a Microsoft Exchange server.
<?php
$to = "user#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "user#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
SMTP area of the php.ini file:
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = exhange.server.org
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = user#example.com
First of all, even when mail fails, the echo "Mail Sent." will be shown. The php function mail() will return true on success and false on failure. Put it in an if and you can check if the mail has been sent:
if(mail($to,$subject,$message,$headers)) echo "Mail Sent.";
Should be working to check if the email was sent or not.
Regarding your problem that it is not working, I am not quite sure and I might be wrong, but some servers as of my experience want the \r\n behind each headerline.
$headers = "From:" . $from . "\r\n";
But as already said, I might be wrong and related to the examples on here, it is not necessary when using one headerline - http://php.net/manual/en/function.mail.php
When I am testing the mail function, I do not put any header information into the mail function, just $to, $subject, $message. You might give it a try. I really hate using the php mail function by myown, I always use a PHP mailer class.
Sorry if I couldn't answer to your real problem, that the email can not be sent. I hope you
Check in your PHP distro for PEAR and Mail.php. On the cmd line, "php -i" to find your resources. I believe PEAR and Mail.php is fairly common for distros over 5.2. I'm on a Mac and Linux server and prefer PEAR mail over the PHP mail function. Windows should be similar. Here is an example of sending multiple emails using PEAR Mail.
/** PEAR::MAIL
* PEAR::Mail only opens one mail socket for multiple emails sent
*/
require_once('/opt/local/lib/php/Mail.php');
$body = $_POST['message'];
//using sendmail on backend
$params['sendmail_path'] = '/usr/sbin/sendmail';
//using factory method
$mail_object =& Mail::factory('sendmail',$params);
//loop through selected users to send
for ($i=0;$i<count($recipients);$i++){
if (!empty($recipients[$i]['email'])&&($recipients[$i]['alt_email'])){
//concatinate email and alt_email
$address = $recipients[$i]['email'].",".$recipients[$i]['alt_email'];
}
else {
//only one user address
$address = $recipients[$i]['email'];
}
//send the mail instance
$mail_object->send($address,$headers,$body);
if (PEAR::isError($mail_object)) {print($mail_object->getMessage());}
} //close the for loop
Some time your hosting service providers are block outgoing SMTP Authentication. Please confirm with you hosting providers.

send email in php using xampp server?

i want to send email using php through xampp server.here is my code
<?php
$to = 'atchibabu#solbaacken.com';
$subject = 'My Email';
$msg = "please find details";
// Make sure to escape quotes
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Site Name <atchibabu516#gmail.com.com>' . "\r\n";
if(mail($to, $subject, $msg, $headers))
{
echo "success";
}
else
{
echo "fail";
}
?>
when i execute i get "success" message but i didn't get any mail i am waiting still one hour
i don't know why it's happening. some suggestions send email using smtp so can any one guide how could i use the smtp.i am using mac xampp so any one guide me i could i install smtp in mac xammp.
thanks for advance.
Few things to note:
Your from has 2 .com so, probability of your message going in SPAM is almost 90%.
Check your phpinfo(); output. What does sendmail_path show? Do you have that software installed? Ideally it is /usr/sbin/sendmail -t -i and the software is sendmail in ubuntu machines.
Also, in your phpinfo(), check the SMTP port. Also there is a high possibility of getting blocked by a firewall or similar software, checked it already?
You should configure your email on localhost
here is the step by step way to configure your smtp
Open the “php.ini“. You should know where it is located because it depends upon the particular server you’re running.
Search for the attribute called “SMTP” in the php.ini file.Generally you can find the line “SMTP=localhost“. change the localhost to the smtp server name of your ISP. And, there is another attribute called “smtp_port” which should be set to 25.I’ve set the following values in my php.ini file.
SMTP = smtp.wlink.com.np
smtp_port = 25
Restart the apache server so that PHP modules and attributes will be reloaded.
Now try to send mail
Reff link is here
helpfull links
http://expertester.wordpress.com/2010/07/07/how-to-send-email-from-xampp-php/
Edit your SMTP. If you want to test it in your localhost, try installing Mozilla Thunderbird for localhost email and create new account using Mercury from Xampp.

How can i send an Email using PHP at windows Azure?

How can i send an Email using PHP at windows Azure?
i am using simple mail function:
$to .= 'email-Id';
$subject = " Test Subject";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: '.$to.'' . "\r\n";
$headers .= 'From: '.$name. '<'.$email.'>' . "\r\n";
echo $message='email text here';
#mail($to, $subject, $message, $headers);
To send emails using PHP you have a few options:
Option 1: Use SMTP
You'll need to modify your php.ini configuration file (http://php.net/manual/en/ref.mail.php) and set the SMTP value to an external SMTP server you can use. SMTP servers are not part of the Windows Azure features at the moment.
[mail function]
SMTP = mail.mycompany.com
Option 2: Use sendmail
You'll need to modify your php.ini configuration file (http://php.net/manual/en/ref.mail.php) and set the sendmail_path value to the sendmail executable.
[mail function]
sendmail_path = "C:\wamp\sendmail\sendmail.exe -t"
Since sendmail doesn't exist in Windows, you'll need to use the fake sendmail for windows: http://glob.com.au/sendmail/
Option 3: Use a mail/smtp service
You could use a service like SendGrid to send your emails (they have an offer for Azure users: http://sendgrid.com/azure.html). They'll take care of sending out the email, you'll just need to call the REST api:
$sendgrid = new SendGrid('username', 'password');
$mail = new SendGridMail();
$mail->addTo('foo#bar.com')->
setFrom('me#bar.com')->
setSubject('Subject goes here')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>');
$sendgrid->smtp->send($mail);
I had never done PHP, but the following guide was step by step and incredibly easy to get working.
http://www.windowsazure.com/en-us/Documentation/Articles/store-sendgrid-php-how-to-send-email/
Hope it helps someone.
email-Id ?? what is this?
I'm guessing it is the email address of the recipient.
Your headers do not require the To: as the to address is specified in the first parameter.
Unless you know the recipient's name and want him to see email was sent to: Some Name not just you do not need that.
Also you have an error in it: missing <> before and after the email address.
P.S. Emails sent through PHP's mail() function have one of the highest rates of ending up in SPAM, especially if you do not have Domain Keys and SPF set in your DNS for this.
If using Cpanel please refer to Email Authentication section of your Email group in Cpanel.
I was having the same trouble , but this solution works perfectly for me .
just follow these steps :
Just enable 2 step verification on your G-mail account.
Go to app password and then select app = other and then type AzureWebsite and generate a password , and keep the password.
replace the
$mail->Password = 'new password';
4.I hope this will work for you too .
Updated information # Nov-2017:
Full Blog post:
https://blogs.msdn.microsoft.com/mast/2017/11/15/enhanced-azure-security-for-sending-emails-november-2017-update/
Recommended Method of Sending E-mail
"Microsoft recommends that Azure customers employ authenticated SMTP relay services (typically connected via TCP port 587 or 443, but often support other ports too)....."

Categories