[nycphp-talk] Emailing from a PHP script
Anthony Wlodarski
aw at sap8.com
Tue Oct 9 14:14:10 EDT 2007
Here is my version of sending items via email, I was creating a site where
you can bounce things to a Verizon or Cingular phone from my server such as
wallpapers. Has some imaging resizing code in there as well but the code
mostly illustrates how to send multimedia files using the right header
information.
<?php
// Borrowed from PHP.net
// Edited and commented by Anthony Wlodarski
function send_mail($emailaddress, $fromaddress, $emailsubject, $body, $file)
{
// create a temp file and resize to 25% of image size if it
is not already smaller than 240 x 320
$tempfile = $file.".tmp.jpg";
if(copy($file, $tempfile))
{
list($width, $height) =
getimagesize($tempfile);
}
else
{
echo "Error creating temp file, try again
later.";
}
// we must see how we want to scale the image if width is >
240 pixels
if($width > 240)
{
$percent = 240/$width;
$new_width = $width * $percent;
$new_height = $height * $percent;
}
else if($height > 320)
{
$percent = 320/$height;
$new_width = $width * $percent;
$new_height = $height * $percent;
}
// create our image resource from the temp file
$image = imagecreatefromjpeg($tempfile);
// create a resized image
$imagetemp = imagecreatetruecolor($new_width, $new_height);;
// resize the image
imagecopyresampled($imagetemp, $image, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
// save the resized file into the temp file and send the
message upon success
if(imagejpeg($imagetemp, $tempfile))
{
// end of line characters
$eol="\r\n";
$mime_boundary=md5(time());
//Common Headers
$headers .= 'From:
MyName<'.$fromaddress.'>'.$eol;
$headers .= 'Reply-To:
MyName<'.$fromaddress.'>'.$eol;
$headers .= 'Return-Path:
MyName<'.$fromaddress.'>'.$eol; // these two to set reply address
$headers .= "Message-ID: <".$now."
TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP
v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype
Headers
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type:
multipart/related; boundary=\"".$mime_boundary."\"".$eol;
$msg = "";
//original php.net code ends here
//Anthony W. edits
if(is_file($tempfile))
{
//File for Attachment
//Find the last occurence of
"/" in the absolute path and then chop
$file_name = substr($tempfile,
(strrpos($tempfile, "/")+1));
$handle=fopen($tempfile, 'rb');
$f_contents=fread($handle,
filesize($tempfile));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data
For Transition using base64_encode();
fclose($handle);
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: image/jpeg;
name=\"".$file_name."\"".$eol;
$msg .= "Content-Transfer-Encoding:
base64".$eol;
$msg .= "Content-Disposition: attachment;
filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of
lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
}
else
{
echo "Error, file does not
exist";
}
// Setup for text OR html
$msg .= "Content-Type:
multipart/alternative".$eol;
// text only messages are sent
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain;
charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding:
8bit".$eol;
$msg .= strip_tags(str_replace("<br>", "\n",
$body)).$eol.$eol;
// end of mime
$msg .= "--".$mime_boundary."--".$eol.$eol;
// finish with two eol's for better security. see Injection.
// send the email
ini_set(sendmail_from,$fromaddress); // the
INI lines are to force the From Address to be used !
mail($emailaddress, $emailsubject, $msg,
$headers);
ini_restore(sendmail_from);
echo "Message sent to ".$emailaddress;
}// end of if(imagecopy...
// delete the temporary file
unlink($tempfile);
}
// store all teh variable posted from the previous html form
// catch the phone number
if($_POST['mobilenum'] == '')
{
echo 'Phone number must be entered please return to the <a
href="./index.php?page=uploadimageform">previous</a> page.';
}
else
{
$mobilenum = $_POST['mobilenum'];
$domobile = true;
}
// simple check for email validation
if($_POST['email'] == '')
{
echo 'Email must be entered please return to the <a
href="./index.php?page=uploadimageform">previous</a> page.';
}
else
{
$email = $_POST['email'];
$doemail = true;
}
// simple check for email validation
if(isset($_POST['file']))
{
$file = $_POST['file'];
$dofile = true;
}
else
{
echo 'File handle has been lost please return to the <a
href="./index.php?page=uploadimageform">previous</a> page.<br/>';
}
// simple check for email validation
if(isset($_POST['carrier']))
{
$carrier = $_POST['carrier'];
$docarrier = true;
}
else
{
echo 'Carrier has not been selected please return to the <a
href="./index.php?page=uploadimageform">previous</a> page.<br/>';
}
// Message Subject
$emailsubject="picture";
// Message Body
$body="";
if($carrier == 'verizon')
{
$mobilenum .= "@vzwpix.com";
}
else if($carrier == 'cingular')
{
$mobilenum .= "@cingularme.com";
}
// call function to resize and send image
send_mail($mobilenum, $email, $emailsubject, $body, $file);
?>
Hope this helps in addition to Paul's infol.
Anthony Wlodarski
Senior Technical Recruiter
Shulman Fleming & Partners
646-285-0500 x230
aw at sap8.com
From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
Behalf Of PaulCheung
Sent: Tuesday, October 09, 2007 8:38 AM
To: NYPHP Talk
Subject: Re: [nycphp-talk] Emailing from a PHP script
It must be my birthday today. As I have managed to solve my two knottiest
problems today.
Here is the solution to my email problem
<?php session_start; ob_start;
ini_set("SMTP", "smtp.isp.com");
ini_set("smtp_port", "25");
ini_set("sendmail_from", admin at isp.com);
$to = "johndoe at hotmail.com <mailto:johndoe at hotmail.com> ";
$subject = "Enter your subject here";
$message = "YES IT WORKED @ LONG LAST
I can put anything I like here";
$from = "FROM: admin at isp.com ";
mail("$to", "$subject", "$message", "$from");
echo "The email has been sent";
?>
I hope this helps others who are wrestling with the same problem
----- Original Message -----
From: PaulCheung <mailto:paulcheung at tiscali.co.uk>
To: NYPHP Talk <mailto:talk at lists.nyphp.org>
Sent: Friday, September 14, 2007 4:34 PM
Subject: Re: [nycphp-talk] Emailing from a PHP script
Thanks for the solution. I've tried it and it did not work for me
changed php.ini to reflect change rebooted my system. Don't really
understand why it is necessary to download QK SMTP
Has all my problems been something to do with XAMPP under windows??
Paul
----- Original Message -----
From: Jason Sia <mailto:jsia18 at yahoo.com>
To: NYPHP Talk <mailto:talk at lists.nyphp.org>
Sent: Friday, September 14, 2007 4:00 PM
Subject: Re: [nycphp-talk] Emailing from a PHP script
ok that will not really work
you should use SMTP=localhost then download QK SMTP Server to make this
work.
----- Original Message ----
From: PaulCheung <paulcheung at tiscali.co.uk>
To: NYPHP Talk <talk at lists.nyphp.org>
Sent: Friday, September 14, 2007 10:37:11 PM
Subject: Re: [nycphp-talk] Emailing from a PHP script
Yes, that is correct.
Has this anything to do with my problem?
----- Original Message -----
From: Jason Sia <mailto:jsia18 at yahoo.com>
To: NYPHP <mailto:talk at lists.nyphp.org> Talk
Sent: Friday, September 14, 2007 3:24 PM
Subject: Re: [nycphp-talk] Emailing from a PHP script
you are just using XAMPP for windows right?
----- Original Message ----
From: Ken Robinson <kenrbnsn at rbnsn.com>
To: NYPHP Talk <talk at lists.nyphp.org>
Sent: Friday, September 14, 2007 9:32:07 PM
Subject: Re: [nycphp-talk] Emailing from a PHP script
At 09:09 AM 9/14/2007, PaulCheung wrote:
>Can anybody see what I am doing wrong??
>
>I have set php.ini (as per the manual) to my email settings
>======================================
>[mail function]
>; For Win32 only.
>SMTP = smtp.tiscali.co.uk
>smtp_port = 25
>
>; For Win32 only.
>sendmail_from = paulcheung at tiscali.co.uk
>========================================================
>I run the script and the following message appears
Did you restart your web server (Apache?) after making the changes to
the PHP.INI file? This has to be done for the changes to take affect.
Ken
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php
Send instant messages to your online friends http://uk.messenger.yahoo.com
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php
Send instant messages to your online friends http://uk.messenger.yahoo.com
_____
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php
_____
_______________________________________________
New York PHP Community Talk Mailing List
http://lists.nyphp.org/mailman/listinfo/talk
NYPHPCon 2006 Presentations Online
http://www.nyphpcon.com
Show Your Participation in New York PHP
http://www.nyphp.org/show_participation.php
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20071009/3f540e76/attachment.html>
More information about the talk
mailing list