Sending emails using PHP - php

I am designing a contact form on a website and despite all the tutorials that I followed online to get it to work, it just isn't working.
I am currently using XAMPP for Windows and I know that I need to change my php.ini and sendmail but no matter how much I change my SMTP settings, I just cannot get the contact form to work.
Could someone please give me an up-to-date explanation of how this whole procedure works?
I am planning to put the website online eventually. When this comes, I suppose the php.ini and sendmail file from XAMPP won't matter anymore right? So how does it work on a web server?
Thanks

Don't forget to set a valid smtp in your php.ini because sendmail is not used on Windows.
Use your ISP's smtp.

Your php.ini wont work on webserver.
On webserver most time, the emails are handled by their smtp relays. So do not worry about emails in webserver. For testing you just upload your email script on webserver at any address, and test it it works.
Here I am providing you code which should work on your webserver.
$To = "email address of person whom you want to send email";
$From = "your email address";
$Subject = "subject of email";
$Email = "your message";
$Headers = "MIME-Version: 1.0" . "\r\n";
$Headers .= "Content-type:text/html; charset=iso-8859-1" . "\r\n";
$Headers .= "From: " . $From . "\r\n";
$mail = mail($To, $Subject, $Email, $Headers);
if ($mail) {
echo "email sent";
} else {
echo "email failed";
}

You can also use an remote SMTP server, such as Gmail, to test the email sending locally. This avoids the need to have an email server configured in the current environment.
After you get your code on the server, you should then change the configurations to use the webserver email server.

Related

Why am i able to send email from localhost using Swift Mailer but not with PHP's mail() function?

I am working on a MAMP PRO localhost server. I am very sure emails were being sent some months ago on my localhost from my local applications using php's mail() function. All of a sudden i can't get emails to be sent. I have even moved one of the local websites to a live server on GoDaddy but emails are still not being sent.
The GoDaddy guys swear to that nothing is wrong with their settings and that the issue has to be in my code. Getting back to my localhost MAMP PRO server, i tried all i could to debug, including messing around with the php.ini settings to no avail. I finally reluctantly decided to try an email library, so i went for Swift Mailer. I installed it, wrote the script and now emails are being sent from localhost. I assume it will also work if i put the code on the live server (at GoDaddy).
Here is my mail() code:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: ". "$this->_headerFrom". "\r\n";
$headers .= "Reply-To: "."$email". "\r\n";
$headers .= "Return-Path: "."$email". "\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
$to = "$this->_applicationEmail";
$send = mail($to, $subject, $msg, $headers);
$subject and $msg are all set earlier in the script and they are strings
$send returns true, but no email is sent. I have followed all sorts of
examples here on SO to no avail. It's simple, but mind-boggling all the
same.
I really need to understand why. What is Swift Mailer doing that mail() isn't? I would rather keep my mail() scripts if only i could get them to work. The fact is, i had spent a lot of time writing custom email scripts with several email templates. It would be a heck of a task to rewrite all those mailing scripts to use Swift Mailer. What could make mail() stop working?
MAMP/WAMP need to be set up correctly to send mail.
See http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/.
The same for your GoDaddy server you probably need to do something with SMTP to make sure it is set up correctly.
You might need to do a few tests to see if mail is actually getting sent or just get flat out rejected as spam by the server receiving it as it won't have SPF etc,.

php email is not getting send

I am just wondering if anybody sees any error here, the code executes but I never get the email, any suggestions?
$to = $_POST['to_email'];
$subject = $_POST['subject'];
$message='<p><b>Message:</b> '.str_replace("\n.", "\n..", $_POST['message']).'</p>';
$headers = "From: " . strip_tags($_POST['from_email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['from_email']) . "\r\n".
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
mail() function just uses the sendmail unix/linux command to send mails. If it is wrong configured it won't work
If you have access to your php ini check out the [mail] section
smtp = SMTPSERVER (windows)
smtp_port = 25 (Windows)
sendmail_path= (unix)
This will depend on the setup of PHP.
There are a number of things PHP can do - the most common are queue the email to a local mail server (common on linux, less so on windows) or talk to an external mail relay and queue the mail there (usually requires authentication unless it's an open relay which is rare). It's also possible to write to file.
All that the mail() function guarantees if there are no warnings is that the email has been queued to the location specified in the php.ini file.
The first thing to do is check that mail is reporting success...
$Success = #mail($to, $subject, $message, $headers);
if(!$Success) {
print "Failed to queue email";
}
The next thing you need to do is check your php.ini config to see how email is configured. Make sure it points at a valid mail server with the correct credentials. If you're on linux and using a local mail server like postfix, check that it's running (something like /etc/init.d/postfix status depending on your distro)
As mentioned by #FlorianKasper, SMTP as an option is only available on Windows. If you can clarify exactly what OS you're using, we may be able to help further.

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)....."

Trouble getting simple php mail script to work on OSX

I am trying to get this simple php mail script to send mail to my email addres (mike_minerva#yahoo.com) and I cannot get it to work. I set my sendmail_path in php.ini to the right folder (/etc/sbin/sendmail) but that did not seem to help. What else could I be missing? The script always returns failure.
<?php
$to = "mike_minerva#yahoo.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
if(mail($to,$subject,$message,$headers))
echo "Mail Sent.";
else
echo "failure";
?>
SwiftMailer is a good library for the purpose of authenticating to your SMTP server to send mail.
http://swiftmailer.org/
try to use PEAR MAIL package.
In case anyone else comes to this question via google, another main cause of php mail not working is that the function is blocked on many servers due to the danger of outgoing spam.
There are some good smtp mail classes out there that are very easy to use. I only use mail() for debugging purposes... almost never in a live environment.

Categories