Jump to content

Help with sending Mail from PHP Using SMTP Authentication


Recommended Posts

Hi I really would appreciate if some phpfreak can help me with this please :'(.

 

I need to my contact page send emails so that when recipient receives it, it comes with the correct domain  (same as the web address) it is coming from rather than the server name.

 

At the moment they are coming as    abcd20@elessar.nocdirect.com

 

and would like them come as abcd20@myowndomain.com  (lets say my password for that email is 'zzzaaa' and the domain was  myowndomain.com )

 

Here is my email contact php script which works fine and hopefully would not have to change too much.

 

My Contact form is posting to 'send.php' . On top of my contacts page there is a js script for 'jquery.js' and some jQuery stuff for validation..

 

Have seen many scripts but could not adapt to my existing send.php  script. Can you please help?

 

------ Content of 'send.php' -----------------------------------

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('name','email','phone','message');
$required = array('name','email','phone','message');

$your_email = "abcd20@myowndomain.com";
$email_subject = "New Message: ".$_POST['subject'];
$email_content = "New message:\n";

foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'subject' && $key != 'company') {
	  if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(@mail($your_email,$email_subject,$email_content)) {
	echo 'Message sent!'; 
} else {
	echo 'ERROR!';
}
}
?>

--------------------------------------------------------------------------------

 

 

 

Add additional parameter on the mail function to specify the email sender. Something like this.

 

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com';

mail($to, $subject, $message, $headers);
?>

Okay, try this one. Let me know if it works for you.  ;D

 

<?php

if(!$_POST) exit;

$email = $_POST['email'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email ))
{
$error.="Invalid email address entered";
$errors=1;
}

if($errors==1) echo $error;
else
{
$values = array ('name','email','phone','message');
$required = array('name','email','phone','message');

$from_email = 'abcd20@myowndomain.com';
$your_email = "abcd20@myowndomain.com";
$email_subject = "New Message: ".$_POST['subject'];
$headers = "From: {$from_email}\r\nReply-To: {$from_email}";

$email_content = "New message:\n";

foreach($values as $key => $value)
{
	if(in_array($value,$required))
	{
		if ($key != 'subject' && $key != 'company')
		{
			if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
		}

		$email_content .= $value.': '.$_POST[$value]."\n";
	}
}

if(@mail($your_email,$email_subject,$email_content, $headers))
{
	echo 'Message sent!'; 
}
else
{
	echo 'ERROR!';
}
}
?>

As far as i am aware, mail doesn't support SMTP Authentication, your better off going with PEAR Mail package or a fsocket  system like phpMailer

 

 

Of course you can just change your reply address, but that will only send from a non-authenticated account

Hey dolrichfortich!

 

Are some genius or something? because it has worked and you must be! and until now no one has come of with such a simple solution to my problem.

 

Many many thanks mate :-)

 

First go and it worked. ..I am happy :-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.