0

I`M USING THE PHP MAIL FUNCTION TO SEND EMAILS TO MEMBERS IN MY SITE.

sendto="xxxxxxx@yahoo.com";
$to=$sendto;
$from="From:mysite@mysite.com \r\n";
$subject="$fullname contacted mysite";
$content="$message \r\n his/her email address is $email";
if($content){
mail($to, $subject, $content,$from);

echo"<font color=green>Your email was sent successfuly to adminstration of mysite.<br/><a href=index.php>Continue to the site</a></font>";
}

i dont know how to make it like in Tagged,facebook etc when u receive a notification in your inbox eg yahoo,it shows :
from subject
tagged john contacted mysite
facebook john contacted mysite

When i send email by removing @mysite.com and remaining mysite from $from="From:mysite@mysite.com \r\n"; strange thing happens, thus to say mysite@server403.webhostingpad.com.
i hope u`ve understood me.HELP PLEASE.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by mrcniceguy
0

Firstly I would change this part

sendto="xxxxxxx@yahoo.com";
// TO THIS // 
$sendto="xxxxxxx@yahoo.com";

You may have cut that part out with the copy and paste

Personally I use this and it works the way I want it to.

mail($receiver_email, $subject, $msg, 'FROM: MySite.com');

Other member's on this site are pros at this stuff and I hope they correct me if I'm wrong, but it's been my experience that if you leave this part out

$from="From:mysite@mysite.com \r\n";

The message looks like this [email]mysite@server403.webhostingpad.com[/email] when it is received.

What you have there looks like it should be okay.

0

Here is an email class that I made. I have never had problems with it.

It also allows for attachments as well.

Example usage:

$email = new email;
$email->to('test@test.com','test@gmail.com'); //allows for multiple to addresses
//or you can call the to() function again.
//carbon copies are allowed using $email->cc('emailaddress');
//blind carbon copies are allowed using $email->bcc('emailaddress');
$email->from('Name','no-reply@sample.com'); //provide a name and a from address
$email->subject('Subject here'); //add a subject
$email->message('Message text\n\nMore text','text'); //if you want to use html just replace 'text' with 'html'
//attachments are allowed using $email->attach('path/to/file.ext','Name.ext','mime-type');
$email->send(); //sends email(s)

Or you can just use the code as an example so you can what you did wrong.

Attachments
<?php

class email {

	private $email = array();

	private function boundary() {
		if ( !isset( $this->email['boundary'] ) ) {
			$this->email['boundary'] = '==Multipart_Boundary_x' . md5( mt_rand() ) . 'x';
		}
		return $this->email['boundary'];
	}

	private function validate_email( $email ) {;
		return preg_match( '#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#',$email );
	}

	private function filter( $emails ) {
		if ( !is_array( $emails ) ) {
			$emails[] = $emails;
		}
		return array_filter( $emails,array( $this,'validate_email' ) );
	}

	public function to() {
		$args = func_get_args();
		if ( is_array( $args[0] ) ) {
			$args = $args[0];
		}
		$args = $this->filter( $args );
		foreach( $args as $email ) {
			$this->email['to'][] = $email;
		}
	}

	public function from( $name,$email ) {
		$this->validate_email( $email ) or $this->error("Invalid From: email address");
		$this->email['from'] = "From: {$name} <{$email}>\r\n";
	}

	public function cc() {
		$args = func_get_args();
		if ( is_array( $args ) ) {
			$args = $args[0];
		}
		$args = $this->filter( $args );
		$this->email['cc'] = "Cc: " . implode( ',',$args ) . "\r\n";
	}

	public function bcc() {
		$args = func_get_args();
		if ( is_array( $args ) ) {
			$args = $args[0];
		}
		$args = $this->filter( $args );
		$this->email['bbc'] = "Bcc: " . implode( ',',$args ) . "\r\n";
	}

	public function subject( $text ) {
		$this->email['subject'] = $text;
	}

	public function message( $data,$type='text' ) {
		$this->email['message']['data'] = $data;
		switch( $type ) {
			case "html":
				$type = 'text/html';
			break;
			default:
				$type = 'text/plain';
			break;
		}
		$this->email['message']['type'] = $type;
	}

	private function get_file( $file ) {
		if ( file_exists( $file ) && is_readable( $file ) ) {
			$fh = fopen( $file,'rb' );
			$data = chunk_split( base64_encode( fread( $fh,filesize( $file ) ) ) );
			fclose( $fh );
			return $data;
		}
		return false;
	}

	public function attach( $file,$name,$mime ) {
		if ( $data = $this->get_file( $file ) ) {
			$this->email['attachments'][] = "--" . $this->boundary() . "\nContent-Type: {$mime};\n name=\"{$name}\"\nContent-Disposition: attachment;\n filename=\"{$name}\"\nContent-Transfer-Encoding: base64\n\n{$data}\n\n";
		}
	}

	public function send() {
		$required = array('to','subject','message');
		if ( count( ( $diff = array_diff( $required,array_keys( $this->email ) ) ) ) > 0 ) {
			$this->error("Unable to send email(s) because the following functions need to be called: " . implode( ',',$diff ));
		}
		$message = "This is a multi-part message in MIME format.\n\n--" . $this->boundary() . "\nContent-Type: {$this->email['message']['type']}; charset=\"UTF-8\"\nContent-Transfer-Encoding: 7bit\n\n{$this->email['message']['data']}\n\n" . ( isset( $this->email['attachments'] ) ? implode( '',$this->email['attachments'] ) : '' ) . "--" . $this->boundary() . "--\n";
		$headers = '';
		$allowed = array('from','cc','bbc');
		foreach( $allowed as $part ) {
			if ( isset( $this->email[$part] ) ) {
				$headers .= $this->email[$part];
			}
		}
		$headers .= "MIME-Version: 1.0\r\nContent-Type: multipart/mixed;\r\n boundary=\"" . $this->boundary() . "\"";
		foreach( $this->email['to'] as $email ) {
			if ( !mail( $email,$this->email['subject'],$message,$headers ) ) {
				$errors++;
			}
		}
	}

	private function error( $message ) {
		//add error handling here
		trigger_error( $message,E_USER_WARNING );
	}

}

?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.