DaveRandom;10973043 wrote:We are now starting to get to a point where I can't really tell you what is wrong without seeing the full code/having access to your hosting server - obviously you can't post those here, and shouldn't give them to anyone anyway.
The problems you are having when connecting to localhost are related to the SMTP protocol itself, which can be a tricky beast - I have spent much of my working life shouting at SMTP servers which aren't doing what I expect them to.
Why you can't connect to mail.richardcranney.com I don't know (I can telnet to it on port 25 and get a valid SMTP on the other end) - if the connection is timing out I would guess the DNS is not resolving right from your server, or there is an IP routing problem.
Should 'localhost' and 'mail.richardcranney.com' resolve to the same machine from your hosting server? In other words, is your hosting server the same box as your mail server?
I can email you the full script if you like. My site is hosted with asmallorange.com and they have advised me to use localhost as the mail server.
When I use it on the following, it works:
//$firstname is the first name of target
//$lastname is the last name of target
//$email is the targets email address
//$meeting_date is straight from a DATETIME mysql field and assumes UTC.
//$meeting_name is the name of your meeting
//$meeting_duretion is the duration of your meeting in seconds (3600 = 1 hour)
$firstname = "John";
$lastname = "Smith";
$email = "richcranney@googlemail.com";
$meeting_date = "2011-02-25 13:40:00"; //mysql format
$meeting_name = "Hello";
$meeting_duration = 3600;
//returns true or false
$result = sendIcalEmail($firstname,$lastname,$email,$meeting_date,$meeting_name,$meeting_duration);
//display result
if($result) {
echo "Email sent successfully.";
} else {
echo "A problem occured sending email";
}
function sendIcalEmail ($firstname,$lastname,$email,$meeting_date,$meeting_name,$meeting_duration) {
$from_name = "Richard Cranney";
$from_address = "test@richardcranney.com";
$subject = "Meeting Booking"; //Doubles as email subject and meeting subject in calendar
$meeting_description = "Here is a brief description of my meeting\n\n";
$meeting_location = "My Office"; //Where will your meeting take place
//Convert MYSQL datetime and construct iCal start, end and issue dates
$meetingstamp = strtotime($meeting_date . " UTC");
$dtstart= gmdate("Ymd\THis\Z",$meetingstamp);
$dtend= gmdate("Ymd\THis\Z",$meetingstamp+$meeting_duration);
$todaystamp = gmdate("Ymd\THis\Z");
//Create unique identifier
$cal_uid = date('Ymd').'T'.date('His')."-".rand()."@mydomain.com";
//Create Email Headers
$headers = array (
"From" => "$from_address",
"Reply-To" => "$from_address",
"Subject" => "$subject",
"Return-Path" => "$sender",
"MIME-Version" => "1.0"
);
//Create Email Body (HTML)
$body = "<html>\r\n<body>\r\n<p>Dear $firstname $lastname,</p>\r\n<p>Here is my HTML Email / Used for Meeting Description</p>\r\n</body>\r\n</html>";
//Create ICAL Content (Google rfc 2445 for details and examples of usage)
$ical = "BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
DTSTART:20110110T110000Z
DTEND:20110110T120000Z
DTSTAMP:20110109T010919Z
UID:sjt64vqadlhlb1j8edi18oeelo@google.com
CREATED:20110110T093149Z
DESCRIPTION:
LAST-MODIFIED:20110110T093149Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Yes it works
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR
";
// Define data about the attachment
$fileatt = 'dispatch.ics'; // Path to the file
$fileatt_type = "text/calendar"; // File Type
$attachments = array($fileatt => $fileatt_type);
// write ICAL to file
file_put_contents($fileatt,$ical);
//SEND MAIL
$mailresult = mail_attachments( $email, $subject, $body, $attachments, $headers );
// Delete local file
unlink($fileatt);
// Return result of sending email
return $mailresult;
}
function mail_attachments ($to, $subject, $body, $attachments, $headers = FALSE, $mailparams = FALSE) {
// Get random hash and define first boundary string
$mixedboundary = 'boundary-mixed-'.($random_hash = md5(time()));
// Validate $headers, unset the content-type if it is set and overwrite it with multipart/mixed
if (!$headers) $headers = array(); else foreach ($headers as $header => $data) if (strtolower($header) == 'content-type') unset($headers[$header]);
$headers['Content-Type'] = "multipart/mixed; boundary=\"$mixedboundary\"";
// Build extra headers into a string
$headersarr = array();
foreach ($headers as $header => $data) $headersarr[] = "$header: $data";
$headerstr = implode("\r\n",$headersarr);
// Start building body
$bodystr = "--$mixedboundary\r\n";
if (is_array($body)) {
if (isset($body['html']) && isset($body['text'])) {
// Both text and html have been passed, build extra multipart/alternative section
$bodystr .= "Content-Type: multipart/alternative; boundary=\"".($altboundary = "boundary-alt-$random_hash")."\"\r\n\r\n--$altboundary\r\nContent-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".$body['text']."\r\n\r\n--$altboundary\r\nContent-Type: text/plain; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".$body['html']."\r\n\r\n--$altboundary--\r\n\r\n--$mixedboundary";
} else if (($html = isset($body['html'])) || isset($body['text'])) {
// Only one of text or html have been passed, build as appropriate
$bodystr .= "Content-Type: text/".(($html) ? 'html' : 'plain')."; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n".(($html) ? $body['html'] : $body['text'])."\r\n\r\n--$mixedboundary";
} else return FALSE;
} else if (is_string($body)) {
// $body is a string, assume HTML
$bodystr .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n$body\r\n\r\n--$mixedboundary";
} else return FALSE;
// Loop through attachments, base64 encode them and add them to $bodystr
foreach ($attachments as $attachment => $type) if (is_file($attachment)) $bodystr .= "\r\nContent-Type: $type; name=\"".basename($attachment)."\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment\r\n\r\n".chunk_split(base64_encode(file_get_contents($attachment)))."\r\n--$mixedboundary";
$bodystr .= "--";
// Execute mail() and return the result
//return ($mailparams) ? @mail($to,$subject,$bodystr,$headerstr,$mailparams) : @mail($to,$subject,$bodystr,$headerstr);
// Now try with PEAR
require_once "Mail.php";
$host = "localhost";
$username = "test@richardcranney.com";
$password = "******";
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
return ($mailparams) ? $smtp->send($to, $headers, $bodystr, $mailparams) : $smtp->send($to, $headers, $bodystr);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
}
Post continues on next page....