Solved

SMTP Headers for PHP Mailer

Posted on 2012-12-24
11
848 Views
Last Modified: 2012-12-27
Hi experts,

I need full smtp headers for a php mailer. This is what i have and its not working.

$connect = fsockopen($hostname,$port); 
fputs($connect, "EHLO $mydomain\r\n"); 
fputs($connect, "AUTH LOGIN\r\n"); 
fputs($connect, base64_encode($username)."\r\n"); 
fputs($connect, base64_encode($password)."\r\n"); 
fputs($connect, "FROM:$fromname <$fromemail>\r\nReply-To: $replyto\r\n");
fputs($connect, "RCPT TO:$fullname <$email>\r\n"); 
fputs($connect, "DATA\r\n"); 
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: base64\r\n\r\n");
fputs($connect, "To: $email\n");
fputs($connect, "Subject: $subject\n\n"); 
fputs($connect, "$message\r\n"); 
fputs($connect, ".\r\n"); 
fputs($connect, "QUIT\r\n");

Open in new window


Thanks,
0
Comment
Question by:jebbie635
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 19

Accepted Solution

by:
Barthax earned 500 total points
ID: 38718487
Move your FROM line to under the DATA section and then have a first FROM line of:

fputs($connect, "MAIL FROM: \"$fromname\" <$fromemail>\r\n");

$connect = fsockopen($hostname,$port); 
fputs($connect, "EHLO $mydomain\r\n"); 
fputs($connect, "AUTH LOGIN\r\n"); 
fputs($connect, base64_encode($username)."\r\n"); 
fputs($connect, base64_encode($password)."\r\n"); 
fputs($connect, "MAIL FROM: \"$fromname\" <$fromemail>\r\n");
fputs($connect, "RCPT TO:$fullname <$email>\r\n"); 
fputs($connect, "DATA\r\n"); 
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: base64\r\n\r\n");
fputs($connect, "To: $email\n");
fputs($connect, "From: "$fromname" <$fromemail>\r\nReply-To: $replyto\r\n");
fputs($connect, "Subject: $subject\n\n"); 
fputs($connect, "$message\r\n"); 
fputs($connect, ".\r\n"); 
fputs($connect, "QUIT\r\n");

Open in new window

0
 
LVL 19

Expert Comment

by:Barthax
ID: 38718490
Note also that your lines of the e-mail should all end in \r\n and so after the Subject line you should have \r\n\r\n to provide the blank line between the headers and the message body.

SMTP standard: http://tools.ietf.org/html/rfc2821
E-mail format: http://tools.ietf.org/html/rfc2822

$connect = fsockopen($hostname,$port); 
fputs($connect, "EHLO $mydomain\r\n"); 
fputs($connect, "AUTH LOGIN\r\n"); 
fputs($connect, base64_encode($username)."\r\n"); 
fputs($connect, base64_encode($password)."\r\n"); 
fputs($connect, "MAIL FROM: \"$fromname\" <$fromemail>\r\n");
fputs($connect, "RCPT TO:$fullname <$email>\r\n"); 
fputs($connect, "DATA\r\n"); 
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: base64\r\n");
fputs($connect, "To: $email\r\n");
fputs($connect, "From: "$fromname" <$fromemail>\r\nReply-To: $replyto\r\n");
fputs($connect, "Subject: $subject\r\n\r\n"); 
fputs($connect, "$message\r\n"); 
fputs($connect, ".\r\n"); 
fputs($connect, "QUIT\r\n");

Open in new window


(Corrected headers in e-mail.)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38718521
Have you considered using the free and open-source PHPMailer class?  It might be easier.  Just a thought...
https://code.google.com/a/apache-extras.org/p/phpmailer/

And if you find that you're still wrestling with email anomalies and edge cases (as most of us do) you might want to consider a professionally hosted service.  This one has worked well for me.
http://search.constantcontact.com/index.jsp
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jebbie635
ID: 38718531
Is 'TO' required ? Isnt the same with 'RCPT-TO' ?
0
 
LVL 19

Expert Comment

by:Barthax
ID: 38718540
There are two parts to sending an e-mail: the SMTP protocol in which RCPT TO is required and the e-mail itself in which the To: header is sent.  The e-mail itself is sent in the SMTP DATA command.  Everything here constitutes what the the e-mail client sees:
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: base64\r\n");
fputs($connect, "To: $email\r\n");
fputs($connect, "From: "$fromname" <$fromemail>\r\nReply-To: $replyto\r\n");
fputs($connect, "Subject: $subject\r\n\r\n"); 
fputs($connect, "$message\r\n"); 
fputs($connect, ".\r\n"); 

Open in new window


... and also what the SMTP server essentially ignores. :)
0
 

Author Comment

by:jebbie635
ID: 38718601
Omg this is so much reading and cant understand it. Please correct the code, this shouldnt be much for you. I really need it. This is the mailer here:

<?php
error_reporting(E_ALL); 

// MESSAGE DETAILS

$fromemail   = "";
$fromname    = "";
$replyto     = "";
$subject     = "";
$fullname    = "";
$email       = "";
$message     = "";

// SMTP INFO

$hostname    = "";
$username    = "";
$password    = "";
$port        = 25;
$mydomain    = "";

// SMTP CONNECT

$connect = fsockopen($hostname,$port); 
fputs($connect, "EHLO \'$mydomain\'\r\n"); 
fputs($connect, "AUTH LOGIN\r\n"); 
fputs($connect, base64_encode($username)."\r\n"); 
fputs($connect, base64_encode($password)."\r\n"); 
fputs($connect, "MAIL FROM: \'$fromname\' <$fromemail>\r\n");
fputs($connect, "RCPT TO: \'$fullname\' <$email>\r\n"); 
fputs($connect, "DATA\r\n"); 
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: base64\r\n");
fputs($connect, "To: \'$email\'\r\n");
fputs($connect, "From: \'$fromname\' <$fromemail>\r\nReply-To: $replyto\r\n");
fputs($connect, "Subject: \'$subject\'\r\n\r\n"); 
fputs($connect, "\'$message\'\r\n"); 
fputs($connect, ".\r\n"); 
fputs($connect, "QUIT\r\n"); 

?>

Open in new window

0
 
LVL 19

Assisted Solution

by:Barthax
Barthax earned 500 total points
ID: 38718646
I'm not in a position to test the script, alas.  Key points, however: none of your variables are populated so the message is going no-where but I expect you removed the content for posting here, so feel free to ignore this point if that's true. :)  You can't put e-mail addresses in headers surrounded by ' quotes (To: header under DATA section).  You should put names for e-mail addresses in headers in " quotes (From header under Data section).

I've made the EHLO:, MAIL FROM: and RCPT TO: envelope information (SMTP protocol) into plain information - there's then no requirement for quoting at all for the EHLO and quoting is only required in the other two if you are supplying names (non-e-mail information).

If you are still getting errors with your script, then instead of running the script, open a telnet connection to your destination SMTP server (telnet <server> <port>) and type (literally) your information to the server and see the conversation progress.  You should get a better idea of what is going wrong.

<?php
error_reporting(E_ALL); 

// MESSAGE DETAILS

$fromemail   = "";
$fromname    = "";
$replyto     = "";
$subject     = "";
$fullname    = "";
$email       = "";
$message     = "";

// SMTP INFO

$hostname    = "";
$username    = "";
$password    = "";
$port        = 25;
$mydomain    = "";

// SMTP CONNECT

$connect = fsockopen($hostname,$port); 
fputs($connect, "EHLO $mydomain\r\n"); 
fputs($connect, "AUTH LOGIN\r\n"); 
fputs($connect, base64_encode($username)."\r\n"); 
fputs($connect, base64_encode($password)."\r\n"); 
fputs($connect, "MAIL FROM: $fromemail\r\n");
fputs($connect, "RCPT TO: $email\r\n"); 
fputs($connect, "DATA\r\n"); 
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: base64\r\n");
fputs($connect, "To: $email\r\n");
fputs($connect, "From: \"$fromname\" <$fromemail>\r\nReply-To: $replyto\r\n");
fputs($connect, "Subject: $subject\r\n\r\n"); 
fputs($connect, "$message\r\n"); 
fputs($connect, ".\r\n"); 
fputs($connect, "QUIT\r\n"); 

?>

Open in new window


(Errata: RCPT TO: edited.)
0
 

Author Comment

by:jebbie635
ID: 38718804
Its not sending. What is wrong?
0
 

Author Comment

by:jebbie635
ID: 38719345
I tried telnet and the server is working with this headers, so this code sends data wrong. Anybody?
0
 

Author Closing Comment

by:jebbie635
ID: 38719582
Thanks.

This is working:

<?php
error_reporting(E_ALL);

// MESSAGE DETAILS

$email       = "";
$fromemail   = "";
$fromname    = "";
$replyto     = "";
$subject     = "";
$message     = "";
$mydomain    = "";

// SMTP INFO

$hostname    = "";
$username    = "";
$password    = "";

// SMTP CONNECT

$connect = fsockopen($hostname, 25);
fputs($connect, "EHLO $mydomain\r\n");
fputs($connect, "AUTH LOGIN\r\n");
fputs($connect, base64_encode($username)."\r\n");
fputs($connect, base64_encode($password)."\r\n");
fputs($connect, "MAIL FROM: $fromemail\r\n");
fputs($connect, "RCPT TO: $email\r\n");
fputs($connect, "DATA\r\n");
fputs($connect, "MIME-Version: 1.0\r\n");
fputs($connect, "Content-Type: text/html\r\n");
fputs($connect, "Content-Transfer-Encoding: 8bit\r\n");
fputs($connect, "To: \"$fullname\" <$email>\r\n");
fputs($connect, "From: \"$fromname\" <$fromemail>\r\n");
fputs($connect, "Reply-To: $replyto\r\n");
fputs($connect, "Subject: $subject\r\n\r\n");
fputs($connect, "$message\r\n");
fputs($connect, ".\r\n");
fputs($connect, "QUIT\r\n");

?>
0
 
LVL 19

Expert Comment

by:Barthax
ID: 38723500
Glad you got there in the end. :)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question