Send Email Function

coreyavis 1 Tallied Votes 830 Views Share

This is a function I created to facilitate the sending of PHP emails. Just give it the name and email of who your sending it to, and your name and email. A subject and a message. These options are required. The type, cc and bcc options are optional. By default, the email will be sent as text, so if your sending an HTML email, make sure to set the type to 'html'.

function send_email($to_name = null, $to_email = null, $from_name = null, $from_email = null, $subject = '', $message = '', $type = 'text', $cc = null, $bcc = null) {
	if (!$to_name || !$to_email || !$from_name || !$from_email) return false;
	$header = "";
	switch($type) {
		case 'html':
			$header .= "MIME-Version: 1.0\r\n";
			$header .= "Content-type: text/html; charset=iso-8859-1\r\n";
		break;
		case 'text':
		default:
			$message = wordwrap($message, 70, "\r\n");
		break;
	}
	$header .= "To: $to_name <$to_email>\r\n";
	$header .= "From: $from_name <$from_email>\r\n";
	if ($cc) {
		$header .= "Cc: $cc\r\n";
	}
	if ($bcc) {
		$header .= "Bcc: $bcc\r\n";
	}
	$header .= "Date: ".date(r)."\r\n";
	$header .= "Reply-To: $from_name <$from_email>\r\n";
	$header .= "X-Mailer: PHP/".phpversion();
	return @mail($to_name . '<' . $to_email . '>', $subject, $message, $header);
 }
broj1 356 Humble servant Featured Poster

Nice little function for sending mail if you do not want to be bothered with the details about coding the neccesary mail commands. I haven't tested it but it looks clean and tidy.

faizan naqvi 0 Newbie Poster

pzzz tell me about function ??? what is definition ???

sandimcp 0 Newbie Poster

Hello Faizan Naqvi. It's not clear what you are asking. If you want to know the name of the function or how to use the function command, it might help to review some basics at http://w3schools.com/php/php_functions.asp

waheed_z89 0 Newbie Poster

looking nice...

gabrielcastillo 40 Web Developer

I may be wrong but in line 2 I would think the && would be better the ||.

If to_name is set and to_email is not set, the result would still be true. Meaning one or the other has to be true.. With the && operator, to_name and to_email are required to be true...

Like i said I could be wrong. Just wondering.

coreyavis 13 Dev Poster

Line 2 is supposed to have || because you are basically saying that if any of the variables are blank, return false. If you change it to && all the variables would have to be blank for it to return false. And if any of them are blank, the function can't complete.

gabrielcastillo 40 Web Developer

Im sorry I believe you are incorrect.. If you leave out $to_name its going to return true.

You want to_name, to_email, from_name, from_email required in order to send emails. So you need to have &&.

Basically you want to_name && to_email && from_name && from_email required in order to send email.

pritaeas 2,194 ¯\_(ツ)_/¯ Moderator Featured Poster

Check again, every variable is preceded by a ! (not). So if any variable is empty then the function is exited.

gabrielcastillo 40 Web Developer

Doh!!

coreyavis 13 Dev Poster

I'm considering changing the charset value in my send email function to UTF-8 instead of iso-8859-1. Any opinions? UTF-8 sounds like a good idea unless I want to turn all symbols into HTML entities before sending any emails.

carllagerfeld 0 Newbie Poster

That's a nice function. I thought maybe what it's missing is a bit of security checking but why make it bigger? You could have that in other functions before before calling yours. I run this thing below (among others) on user entered emails. I return a general "There was a problem.." if it returns false..

function isEmailOK($email) 
{
    //Remove all characters except letters, digits and !#$%&'*+-/=?^_`{|}~@.[].
    $email = filter_var($email, FILTER_SANITIZE_EMAIL);

    //checks if it's a properly formed email
    if (filter_var($email, FILTER_VALIDATE_EMAIL)) 
    {
        return TRUE;
    } 
    else 
    {
        return FALSE;
    }
}
coreyavis 13 Dev Poster

That is a nice little function to use, I may just go ahead and implement it as part of the send_email function. With user entered emails, I also will usually add the following function to the form:

function form_security() {
    /* Spambot security */
    $string = '<input type="hidden" name="honeypot" id="honeypot" value="http://" />';
    $string .= '<input type="hidden" name="humancheck" id="humancheck" value="" />';
    return $string;
}

Then validate those spambot hidden form fields with the following function in the backend:

function check_form_security($honeypot, $humancheck) {
    $error = false;
    if ($honeypot != 'http://') $error = true;
    if ($humancheck != "") $error = true;
    return $error;
}

Spambots always look for form fields that are blank or can be populated with urls, and since spambots can't tell the difference between hidden and regular form fields, they will naturally fill them. When validated on the backend, if the blank form field isn't blank or if the urls form field doesn't just contain 'http://', the form won't be processed in your backend. Provided you don't allow the form to be processed if the check_form_security function returns 'true'.

Airshow 416 WiFi Lounge Lizard Team Colleague

One-part emails are pretty simple. To be really useful, this function would simplify the composition of multi-part emails such that you could compose two or more parts (eg plain text and markup) and the function would do the formatting into multi-part.

Another feature would be a mechanism to send drafts to the screen rather than sending an email. This would take some of the pain out of developing dynamically composed emails. I've done enough of this to know how irritating it is to keep switching between editor, browser and email client. If you can eliminate the need for the email client at least some of the time, that would be good.

coreyavis 13 Dev Poster

I like the idea. That would be more than just a PHP function though. Maybe I'll create that as a PHP class.

Loren_1 0 Newbie Poster

Nice, short and concise.
Just two minor things with Content-type:

text/html should only be used with HTML emails. If it's text you should be using text/plain

charset=iso-8859-1 is ok but the value would work better as a variable for the user to supply that is set UTF-8 by default (as someone mentioned above).

coreyavis 13 Dev Poster

The text/html content-type is only used for HTML emails. I've been working on some newer and better email functions and classes so maybe I'll add the text/plain for text emails, but I don't believe it's a requirement.

Airshow 416 WiFi Lounge Lizard Team Colleague

It's some time since I did any emaill stuff but IIRC, modern email clients are pretty good at detecting the mime type of one-part emails, but only in the absence of a defined type. If you go for my multipart idea, then it is essential to define the correct mime type for each part (eg text/plain followed by text/html).

Airshow 416 WiFi Lounge Lizard Team Colleague

If I find time, I'll see if I can dig out my multipart code. It was written for a particular purpose but I think the heart of it was pretty well generic.

Airshow 416 WiFi Lounge Lizard Team Colleague

Mmm, I found it but it's an old phpBB2 emailer class, which I modded back in 2007. I can't really post the whole thing here but you may like to be aware of "Content-type: multipart/alternative; ...;".

As far as I can tell, the definitive standard on this and related issues is RFQ 2046. It's a good bedtime read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.