Hi, my clients site is hosted on GoDaddy. I was getting to work on the PHP back-end to some email forms and realize now the GoDaddy doesn’t… err… support the standard PHP mail() function. How could I best amend the following code to… cooperate with GoDaddy hosting?
fsockopen() is something most hosts don’t lock down and allows a generic connection to a server to be made. It takes more work than mail() to put together a complete e-mail but it is worth the effort. fsockopen() isn’t affected by the “fopen URL wrapper” settings in php.ini (i.e. harder to disable).
If fsockopen() is blocked on those ports by GoDaddy’s firewall settings, then you are completely out of luck unless you change hosting providers or upgrade to more expensive hosting that lets you do whatever you want (VPS or dedicated).
?
Appears to take similar arguments
So would I just include your “smtp.php” in my PHP file with include_once()?
I’m not fluent in PHP. I’m just a C++ programmer. (And obviously HTML/CSS, which is easy)
Where would I get this “smtp.php”? Is it in that download link on your page?
SendSMTPEmail() is the low-level call that sends the actual message. SendEmail() does a lot of work to set up an e-mail and then calls SendSMTPEmail() from there. SendEmail() covers up a lot of nasty requirements to send an e-mail and supports file attachments, embedded images, etc.
I’d grab smtp.php from the WebCron Site Backup module ZIP file (Downloads page, WebCron Site Backup is at the bottom of the page. The file is in /client/support/smtp.php - I also recommend getting pop3.php too). You have to build an $options array to get either one to work. There is DNS resolution support available, but you would have to get the hacked up Net_DNS build from Barebones CMS and start define()'ing stuff. You probably don’t really need it though, which is why it isn’t included in WebCron Site Backup and disabled when sending e-mail reports:
$smtpoptions = array(
"headers" => GetEmailUserAgent("Thunderbird"),
"textmessage" => $message,
"server" => $smtpserver,
"port" => $smtpport,
"secure" => $reportinfo["smtpsecure"],
"username" => $reportinfo["username"],
"password" => $reportinfo["password"],
"usedns" => false
);
...
$result = SendEmail($reportinfo["from"], $toaddr, $subject, $smtpoptions);
if (!$result["success"])
{
...I'd make a POP3 connection here and then retry the SMTP stuff - some hosts do POP before SMTP...
}
That’s ripped from /client/modules/wc_backup/index.php. Used to send a report on what’s changed since the last backup.
SendSMTPEmail() is the low-level call that sends the actual message.
That’s all I’m wanting to try right now. I still don’t know how to include that function with my PHP though. I am not fluent in PHP. All I want is simple writing of strings, no images, etc. I won’t worry about the POP3 first unless this doesn’t work.
Ahhh. See I didn’t know how to “include” the other PHP. I was only giving a good guess with the include_once() but now that you put it in code form I get it. So smtp.php is somewhere on the bottom of that page… 'Need to do that today.
Found it. So I only need the smtp.php?
Gonna try it…
Edit: Nope.
Fatal error: Call to undefined function UTF8IsASCII() in D:\Hosting\5437704\html\smtp.php on line 1262
…?
Line 1262 – undefined function
if (!UTF8IsASCII($subject)) $subject = ConvertToRFC1342($subject);
Oops. I always forget the dependencies. You also need ‘utf8.php’ (‘/client/support/utf8.php’ in WebCron Site Backup) and ‘str_basics.php’ (‘/server/support/str_basics.php’ in WebCron):
str_basics.php provides a common set of functions I use everywhere. utf8.php provides Unicode filtering support. smtp.php provides e-mail sending capabilities primarily over SMTP that adheres to a number of different RFCs required to properly send e-mail.
I used to have a command-line tool for sending e-mail and I sort of dropped it because there were better alternatives. It seems a similar tool is now needed in a website context. Ripping files out of a couple different ZIP files is okay for one-time solutions. If I see this as a recurring theme, I may need to put something together again.
Okay, so it’s all the files in the /support directory.
I probably wouldn’t have found out the order to include them.
Um, does the
SendEmail($from, $to, $subject, $smtpoptions);
not take a $message argument???
Edit: Oh I see it’s in the options.
Okay, I have
<?php
require_once "str_basics.php";
require_once "utf8.php";
require_once "smtp.php";
if(isset($_POST['email'])) {
$email_to = "email@domain.com";
$email_subject = "Test Email";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validate expected data exists
if(!isset($_POST['realname']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$realname = $_POST['realname']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$realname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\
\
";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($realname)."\
";
$email_message .= "Email: ".clean_string($email_from)."\
";
$email_message .= "Comments: ".clean_string($comments)."\
";
$smtpoptions = array(
"headers" => GetEmailUserAgent("Thunderbird"),
"textmessage" => $email_message,
"server" => "mail.website.com",
"port" => 25,
"secure" => false,
"username" => "loginusername",
"password" => "*******",
"usedns" => false
);
SendEmail($email_from, $email_to, $email_subject, $smtpoptions);
?>
Sorry, automatic email handling is currently receiving maintenance, please send your message to email@domain.com
<br />
<a href="index.html">back</a>
<?
}
?>
What else do I need to define in the “$smtpoptions” array?
smtp.php does its own e-mail address “validation” (see MakeValidEmailAddress()) but follows all the relevant RFCs and passes several validation test suites. Most regexes are insufficient.
You need to set ‘server’ to point at the SMTP server for your host (see the DNS Manager in your GoDaddy control panel). Set ‘username’ and ‘password’ to a valid login for e-mail. Otherwise, looks fine. You can get into error checking later - SendEmail() returns an array.
Warning: fsockopen() [function.fsockopen]: unable to connect to mail.website.com:25 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in D:\Hosting\5437704\html\smtp.php on line 864
I assume this is from undefined options.
Set ‘username’ and ‘password’ to a valid login for e-mail.
He uses Yahoo mail… so would I just get that information from him or no?
I think you misunderstood part of the code I sent. I gave you a shell to work with - you have to replace the various strings so they are correct. For example, ‘mail.website.com’ is supposed to be replaced with the correct SMTP server. Mail hosts generally provide the information you need. You could use Yahoo’s servers:
With your client’s login. Sharing passwords isn’t something I do nor recommend but if the client wants to give you that information, that’s their prerogative. However, you may be better served by setting up another account, having the ‘from’ address be the new account, the ‘to’ address be the client’s account, and set the ‘replytoaddr’ option be the source e-mail address. Yahoo is likely to block messages when it runs the server IP against reverse DNS and fails to find a match for ‘from’ based on the code you’ve got above. That is, you can’t send e-mail from your web host to mail.yahoo.com and say the address is from ‘me@gmail.com’ when your own domain is ‘somewebsite.com’ - that’s called “spoofing” and such messages either get ignored or dumped to the spam folder.
I don’t know if you can use regular SMTP (port 25, ‘secure’ => false) or if it has to be SSL-enabled (port 465, ‘secure’ => true) but smtp.php supports both modes. Each web host and e-mail provider is so different with their configurations that you are going to have to fiddle around a bit on your own to get it to work.
Keep in mind that all this work might be for nothing depending on how GoDaddy’s firewalls are set up - they could block all outbound SMTP connections and you would be out of luck. However, supposedly, from what I read, WordPress has issues with mail() but not fsockopen() on GoDaddy hosting, so there is a good chance this will succeed.