Jump to content

Mail: How to change "From" name?


bugzy

Recommended Posts

Hello!

 

I got this function somewhere in the web. It's working though the problem is I can't change the "From" name and it is always showing "admin"

 

Here's the function

 

function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message) 
{  
//SMTP + SERVER DETAILS  
/* * * * CONFIGURATION START * * * */ 
$smtpServer = "mail.mywebsite.com";  
$port = "26";  
$timeout = "30";  
$username = "admin@mywebsite.com";
$password = "my_password";  
$localhost = "mail.mywebsite.com";  
$newLine = "\r\n";  
/* * * * CONFIGURATION END * * * * */ 

//Connect to the host on the specified port  
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);  
$smtpResponse = fgets($smtpConnect, 515);  
if(empty($smtpConnect))   
{  
$output = "Failed to connect: $smtpResponse";  
return $output;  
}  
else 
{  
$logArray['connection'] = "Connected: $smtpResponse";  
}  

//Request Auth Login  
fputs($smtpConnect,"AUTH LOGIN" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authrequest'] = "$smtpResponse";  

//Send username  
fputs($smtpConnect, base64_encode($username) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authusername'] = "$smtpResponse";  

//Send password  
fputs($smtpConnect, base64_encode($password) . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['authpassword'] = "$smtpResponse";  

//Say Hello to SMTP  
fputs($smtpConnect, "HELO $localhost" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['heloresponse'] = "$smtpResponse";  



//Email From  
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailfromresponse'] = "$smtpResponse";  

//Email To  
fputs($smtpConnect, "RCPT TO: $to" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['mailtoresponse'] = "$smtpResponse";  





//The Email  
fputs($smtpConnect, "DATA" . $newLine);  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data1response'] = "$smtpResponse";  




//Construct Headers  
$headers = "MIME-Version: 1.0" . $newLine;  
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;  
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;  



fputs($smtpConnect, "To:$to\nFrom: $from\nSubject: $subject\n$headers\n\n $message\n.\n");  
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['data2response'] = "$smtpResponse";  

// Say Bye to SMTP  
fputs($smtpConnect,"QUIT" . $newLine);   
$smtpResponse = fgets($smtpConnect, 515);  
$logArray['quitresponse'] = "$smtpResponse";   

//insert var_dump here -- uncomment out the next line for debug info
//var_dump($logArray);
}  

 

 

This is my first time to use a mail in php and I wonder if is it possible to change it here?

 

Anyone?

Link to comment
Share on other sites

Your going through the entire handshake here and that can be avoided if you use a library or something. PHPMailer is a good library. With regards to your specific issue you need to check what your passing as the $namefrom argument.

 

There's nothing I can see after scanning the function that would alter it.

Link to comment
Share on other sites

Your going through the entire handshake here and that can be avoided if you use a library or something. PHPMailer is a good library. With regards to your specific issue you need to check what your passing as the $namefrom argument.

 

There's nothing I can see after scanning the function that would alter it.

 

Thanks CPD. I'll try to look at that PHPmailer though I might focus here for the meantime.

 

 

Yup that $namefrom is kinda missing something on that function and I thought a small modification on the header would do that? or it's more complicated than what I thought?

Link to comment
Share on other sites

You call the function by saying authSendEmail('cpd@domain.com', 'CPD', ...). Whats the argument you pass in replacement of 'CPD' here?

 

$from = "admin@mywebsite.com"; 

$namefrom = "My Website Admin";

 

I'm getting "admin" always which is supposed to be "My Website Admin"

Link to comment
Share on other sites

fputs($smtpConnect, "To:$to\nFrom: $from\nSubject: $subject\n$headers\n\n $message\n.\n"); 

 

Looks like that's the problematic line. Even though $headers is being constructed you're putting From: in front already. You'll notice there is no $fromname there. Remove To/From from that line and append Subject: to the end of the $headers variable then remove the Subject: from that line and have:

 

fputs($smtpConnect, "$headers\n\n $message\n.\n"); 

 

That should clear it up.

Link to comment
Share on other sites

fputs($smtpConnect, "To:$to\nFrom: $from\nSubject: $subject\n$headers\n\n $message\n.\n"); 

 

Looks like that's the problematic line. Even though $headers is being constructed you're putting From: in front already. You'll notice there is no $fromname there. Remove To/From from that line and append Subject: to the end of the $headers variable then remove the Subject: from that line and have:

 

fputs($smtpConnect, "$headers\n\n $message\n.\n"); 

 

That should clear it up.

 

CPD you're right!

 

Now it's working! The problem now is the subject is missing.. I'm getting this "(no subject)" ?

Link to comment
Share on other sites

Did you append the subject to the $headers variable?

 

$headers = "MIME-Version: 1.0" . $newLine;  
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;  
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;  
$headers.= "Subject: ".$subject. $newLine;

 

On a side note, I don't think your using the protocol properly. For authentication I've been reading you should use the EHLO command as opposed to HELO and it should be before the AUTH command. I may be completely wrong because I haven't studied the protocol in depth as there's little point but from what I've read the authentication extension to SMTP uses EHLO instead of HELO and its executed in a different order to what you have. Read up on RFC 4954.

 

Again, I'm not 100% because I haven't studied it enough, only skimmed through.

Link to comment
Share on other sites

Did you append the subject to the $headers variable?

 

$headers = "MIME-Version: 1.0" . $newLine;  
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;  
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;  
$headers.= "Subject: ".$subject. $newLine;

 

On a side note, I don't think your using the protocol properly. For authentication I've been reading you should use the EHLO command as opposed to HELO and it should be before the AUTH command. I may be completely wrong because I haven't studied the protocol in depth as there's little point but from what I've read the authentication extension to SMTP uses EHLO instead of HELO and its executed in a different order to what you have. Read up on RFC 4954.

 

Again, I'm not 100% because I haven't studied it enough, only skimmed through.

 

 

 

CPD thank you very much!

 

I have tried to open the link about that EHLO and HELO but those are already been complicated for me as a beginner :)

 

But just for me to understand what you're saying, what are the possible negative implication of using HELO over EHLO?

 

 

so far using this code, I'm experiencing a very weird issue... there are times that it is working properly and there are time that the e-mail is not sending at all and you need to wait for like 30 minutes for it to work again... kinda strange.

Link to comment
Share on other sites

In the long run if you repeatedly try to authenticate and use the protocol incorrectly from the same IP address you can get your IP address blacklisted.

 

Alright!

 

Thanks for the information. I will definitely look in to that but right now I will be focusing more on the basic side of php.

 

About the issue I'm talking about, I think the problem is with gmail. I think they're blocking some of the e-mail because their security bot thought that it was a spam because I tried to use another gmail account and it works again.

 

Anyway. Thank you very much again CPD!

Link to comment
Share on other sites

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.