Sending PHP mail from Windows server - email

I have a form on my page. When the user hits the Send button - it's supposed to send an email with the details he entered in the form. Up until recently the form was hosted on a Linux server and I had no problem with it - the mail was sent and received. Recently I had to move to shared Windows server and since the move the mail is not sent. Here's the code that supposed to send the mail:
function send_contact_form($strName, $strEmail, $strPhone, $strMessage)
{
$to = 'mymail#mysite.com';
$subject = 'From the site';
$message = '<html lang="HE">
<head>
<title>
'.$subject.'
</title>
</head>
<body style="text-align:right; direction:rtl; font-family: Arial;">
Name: '.$strName.'<br>Email: '
.$strEmail.'<br>Phone: '.$strPhone
.'<br><br>Message: <br>'.$strMessage.'
</body>
</html>';
$email = $strEmail;
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
$header .= "From: $email\r\nReply-To: $email" . "\r\n";
mail($to, $subject, $message, $header);
}

In a windows environment PHP uses SMTP insted of the Linux binary sendmail (or replacement)
You need to edit php.ini according to this page to be able to send e-mail via the mail() function.

On Linux, PHP uses an application called sendmail. Of course there is no similar applicaion on Windows. As php.ini file says, to be able to work with mail function, you need to setup mail server coordinates. If You don't have mail server, it is not possible to send emails from PHP. Of course You could use some external server like gmail.

Related

Email Sent By Perl (Net::SMTP) Script Dropped Silently by Gmail

I have a Perl script that sends simple HTML emails to users with status updates and a link to more information. I'm creating mail with Net::SMTP and sending mail using smtp-relay.gmail.com. It has been working great for years.
In the last month, the emails stopped appearing - just gone with no errors and not in SPAM. Hours of troubleshooting later, I've narrowed the problem down to Gmail silently dropping emails that contain my specific URL.
http://DOMAIN/cgi-bin/requests/single_request.pl?requestid=111111
I changed one character and POOF! working again.
http://DOMAIN/cgi-bin/requests/single-request.pl?requestid=111111
I know this is a vague question, but what is going on? The workaround is fine, but I sure didn't learn anything about the root cause.
#!c:\strawberry\perl\bin -w
use strict;
use warnings;
use Net::SMTP;
use Net::Config;
# use this function to send email
#
# example:
#
# send_mail("RECIPIENT\#DOMAIN", "SUBJECT HERE", "BODY HERE");
sub send_mail{
my $recipient = shift;
my $subject = shift;
my $body = shift;
# connect to an SMTP server
my $smtp = Net::SMTP->new("smtp-relay.gmail.com", Debug => 0, Timeout => 30, Hello => 'REDACTED') or die "SMTP Connection Failed: smtp-relay.gmail.com";
# sender's address here
$smtp->mail('REDACTED');
# recipient"s address
$smtp->to($recipient);
# Start the mail
$smtp->data();
# Send the header.
$smtp->datasend("MIME-Version: 1.0\n");
$smtp->datasend("Content-Type: text/html; charset=\"UTF-8\" \n");
$smtp->datasend("To: $recipient . \n");
$smtp->datasend("From: REDACTED\n");
$smtp->datasend("Reply-to: REDACTED\n");
$smtp->datasend("Subject: $subject \n");
$smtp->datasend("\n");
# Send the body.
$smtp->datasend($body);
# Finish sending the mail
$smtp->dataend();
# Close the SMTP connection
$smtp->quit();
}
1;

Wamp localhost send email

I have followed this link and here is my php code. My issue is that I have tried so many times with this and didn't receive any emails. Could you please point out what went wrong? Thank you
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/
if($result)
{
$to = $email;
$subject = "Your comfirmation email";
$header = "from: your name <your email>";
$message = "Thank you for registering with us. You can now login to your account";
if(mail($to, $subject,$header,$message))
{
echo 'Your Confirmation Email Has Been Sent To Your Email Address.
Click here to Login';
}
else
{
echo 'Cannot send Confirmation link to your e-mail address.
Click here to Register';
}
}
Do you have a mail server configured on your system?
Windows does not come with a mail server like *nix. The simple answer is to use a library like phpMailer, which allows you to use a gmail /yahoo /etc mail account to send you email.

How to authenticate a local xampp server to send email

I once used int_set to send email from my company local xammp server for testing.
ini_set('SMTP','mail.companysite.com');
ini_set('smtp_port','26');
ini_set('sendmail_from','email#companysite.com');
Now our host has upgraded the server and we have to authenticate email sending. However I have not been able to get ini_set to work with authentication for email. How can this be done?
We are using xampp on windows machine.
I figured this out:
here is my code:
ini_set('SMTP','mail.somewebsite');
ini_set('smtp_port','26');
ini_set('sendmail_from','email#somewebsite');
ini_set('auth_username','email#somewebsite');
ini_set('auth_password','password');
$headers = "From:email#somewebsite\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";
mail("email#gmail.com", "testing authentication", "hello world", $headers);

How can I use PHP Mail() function within PHP-FPM? On Nginx?

I have searched everywhere for this and I really want to resolve this. In the past I just end up using an SMTP service like SendGrid for PHP and a mailing plugin like SwiftMailer. However I want to use PHP.
Basically my setup (I am new to server setup, and this is my personal setup following a tutorial)
Nginx
Rackspace Cloud
PHP 5.3 PHP-FPM
Ubuntu 11.04
My phpinfo() returns this about the Mail entries:
mail.log no value
mail.add_x_header On
mail.force_extra_parameters no value
sendmail_from no value
sendmail_path /usr/sbin/sendmail -t -i
SMTP localhost
smtp_port 25
Can someone help me to as why Mail() will not work - my script is working on all other sites, it is a normal mail command. Do I need to setup logs or enable some PHP port on the server?
My Sample script
<?
# FORMS VARS
// $to = $customers_email;
// $to = $customers_email;
$to = $_GET["customerEmailFromForm"];
$subject = "Thank you for contacting Real-Domain.com";
$message = "
<html>
<head>
</head>
<body>
Thanks, your message was sent and our team will be in touch shortly.
<img src='http://cdn.com/emails/thank_you.jpg' />
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <real-email#real-domain.com>' . "\r\n";
// SEND MAIL
mail($to,$subject,$message,$headers);
?>
Thanks
If found that on a new installation of Ubuntu 14.04 with nginx and PHP-FPM (no apache), neither postfix nor mailutils were installed.
I used:
sudo apt-get install postfix
(as in the recommended answer)
AND
sudo apt-get install mailutils
to get everything working on my server. Both were required. An entry in PHP.ini (as mentioned in the recommended answer) may also have helped, but without both of the other packages, it wouldn't have made a difference.
As there is no value for sendmail_from you need to set one in php.ini:
sendmail_from = "you#example.com"
Or in the headers when you call to mail:
mail($to, $subject, $message, 'From: you#example.com');
The email address should follow RFC 2822 for example:
you#example.com
You <you#example.com>
Failing that, have you actually installed a working email system?
If not, you can install postfix with the following command:
sudo apt-get install postfix
See below for more information on configuring postfix for use with PHP in Ubuntu:
https://serverfault.com/questions/119105/setup-ubuntu-server-to-send-mail

Sendmail configuration changed in XAMPP 1.8.0, can't send local mails anymore

I've just upgraded from XAMPP 1.7.3 to 1.8.0, this included quite a few changes (PHP 5.4 etc) as I went through the reinstallation of my dev-environment.
Anyways, everything works now, except for Sendmail.
Before, you had a configuration in sendmail.ini like this:
#defaults
logfile "C:\XAMPP\sendmail\sendmail.log"
## A freemail service example
account Hotmail
tls on
tls_certcheck off
host smtp.live.com
from [exampleuser]#testmail.loc
auth on
user [exampleuser]#hotmail.com
password [examplepassword]
# Set a default account
account default : Hotmail
Plus some values in php.ini:
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
SMTP = localhost
smtp_port = 25
Now it all looks a lot different (and the old config wouldn't work), an example:
http://pastebin.com/M83bNmJw
A little php mail script:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$to = "someone#hotmail.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
?>
Message delivery failed...
I guess I'm too stupid to change the correct things, it just won't work, plus I barely get an error in my log-files, so I don't even know where to start.
#GMAIL mit XAMPP 1.8.1 und sendmail
[CODE]
[sendmail]
; HOTMAIL
smtp_server=smtp.gmail.com
smtp_port=25
smtp_ssl=tls
tls_certcheck off
error_logfile=error.log
debug_logfile=debug.log
auth_username= xxxx.xxxx#gmail.com
auth_password=xxxxxxx
this settings in php.ini
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = smtp.gmail.com
; smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = xxxx.xxxx#gmail.com
; XAMPP IMPORTANT NOTE (1): If XAMPP is installed in a base directory with spaces (e.g. c:\program filesC:\xampp) fakemail and mailtodisk do not work correctly.
; XAMPP IMPORTANT NOTE (2): In this case please copy the sendmail or mailtodisk folder in your root folder (e.g. C:\sendmail) and use this for sendmail_path.
; XAMPP: Comment out this if you want to work with fakemail for forwarding to your mailbox (sendmail.exe in the sendmail folder)
sendmail_path = "\"C:\sendmail\sendmail.exe\" -t"
; XAMPP: Comment out this if you want to work with mailToDisk, It writes all mails in the C:\xampp\mailoutput folder
;sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"
; 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 = Off
; Log all mail() calls including the full path of the script, line #, to address and headers
mail.log = "C:\xampp\php\logs\php_mail.log"
I see that in 1.8.0, the default will send mail through mailtodisk.exe. You have enabled it in your PHP config file, but have you disabled mailtodisk.exe?
In addition, you'll need to ensure that smtp_server in sendmail.ini is set to localhost.
I just found this solution myself, and all mail sent using PHP works.
My xampp is 1.8.2 with window 8.1
In php.ini
smtp_port = 587
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
mail.add_x_header=Off
In sendmail.ini
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
auth_username=xxaayy#gmail.com
auth_password=kskcmhlrjr
pop3_server=
pop3_username=
pop3_password=
force_sender=xxaayy#gmail.com
force_recipient=
hostname=
To account gmail "auth_password" you need create new password "Your application-specific passwords", check [here][1]
then follow these steps:
The problem is that sendmail has to be run as an administrator. This is the solution to help any one on my situation.
Right click on sendmail.exe
Properties
Compatibility
Change the configuration for all users
Execute as Windows XP SP 3
Execute as adminitrator
test email
$to = "aaaaaaa#domain.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$headers = "From: xxaayy#gmail.com" . "\r\n";
if (mail($to, $subject, $body, $headers)) {
echo ("Message successfully sent!");
} else {
echo ("Message delivery failed...");
}
I've found a working example, it works like a charm now
http://blog.joergboesche.de/xampp-sendmail-php-mailversand-fuer-windows-konfigurieren#xampp_180_sendmail