I am trying to use PHPMailer but I'm having problem. This is my test code:
<?php
require './PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 4;
$mail->Debugoutput = 'html';
$mail->Host = "localhost";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = "donotreply#myservermail";
$mail->Password = "mypassword";
$mail->setFrom('donotreply#myservermail', 'Mail test');
$mail->addReplyTo('myemail#live.com', 'First Last');
$mail->addAddress('donotreply#myservermail', 'Test mail');
$mail->Subject = 'PHPMailer SMTP test';
$mail->msg = 'Hello there this is a test . ';
$mail->Body = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
And here is the result from browser, and I see no error there:
Connection: opening to localhost:25, timeout=300, options=array ()
Connection: opened
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "220 localhost ESMTP"
SMTP -> get_lines(): $data is "220 localhost ESMTP"
SERVER -> CLIENT: 220 localhost ESMTP
CLIENT -> SERVER: EHLO MY_SERVER_NAME
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250-localhost"
SMTP -> get_lines(): $data is "250-localhost"
SMTP -> get_lines(): $data was "250-localhost"
SMTP -> get_lines(): $str is "250-SIZE 20480000"
SMTP -> get_lines(): $data is "250-localhost250-SIZE 20480000"
SMTP -> get_lines(): $data was "250-localhost250-SIZE 20480000"
SMTP -> get_lines(): $str is "250-AUTH LOGIN"
SMTP -> get_lines(): $data is "250-localhost250-SIZE 20480000250-AUTH LOGIN"
SMTP -> get_lines(): $data was "250-localhost250-SIZE 20480000250-AUTH LOGIN"
SMTP -> get_lines(): $str is "250 HELP"
SMTP -> get_lines(): $data is "250-localhost250-SIZE 20480000250-AUTH LOGIN250 HELP"
SERVER -> CLIENT: 250-localhost250-SIZE 20480000250-AUTH LOGIN250 HELP
Auth method requested: UNKNOWN
Auth methods available on the server: LOGIN
Auth method selected: LOGIN
CLIENT -> SERVER: AUTH LOGIN
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 SOME_ENCRYPTED_STRING"
SMTP -> get_lines(): $data is "334 SOME_ENCRYPTED_STRING"
SERVER -> CLIENT: 334 SOME_ENCRYPTED_STRING
CLIENT -> SERVER: SOME_ENCRYPTED_STRING
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "334 SOME_ENCRYPTED_STRING"
SMTP -> get_lines(): $data is "334 SOME_ENCRYPTED_STRING"
SERVER -> CLIENT: 334 SOME_ENCRYPTED_STRING
CLIENT -> SERVER: SOME_ENCRYPTED_STRING
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "235 authenticated."
SMTP -> get_lines(): $data is "235 authenticated."
SERVER -> CLIENT: 235 authenticated.
CLIENT -> SERVER: MAIL FROM:<donotreply#MY_SERVER_MAIL>
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 OK"
SMTP -> get_lines(): $data is "250 OK"
SERVER -> CLIENT: 250 OK
CLIENT -> SERVER: RCPT TO:<MY_EMAIL#yahoo.com>
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 OK"
SMTP -> get_lines(): $data is "250 OK"
SERVER -> CLIENT: 250 OK
CLIENT -> SERVER: DATA
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "354 OK, send."
SMTP -> get_lines(): $data is "354 OK, send."
SERVER -> CLIENT: 354 OK, send.
CLIENT -> SERVER: Date: Thu, 14 May 2015 10:40:00 +0200
CLIENT -> SERVER: To: MY_EMAIL yahoo <MY_EMAIL#yahoo.com>
CLIENT -> SERVER: From: MAIL_SERVER test <donotreply#MY_SERVER_MAIL>
CLIENT -> SERVER: Reply-To: MY_EMAIL Live <MY_EMAIL#live.com>
CLIENT -> SERVER: Subject: PHPMailer SMTP test
CLIENT -> SERVER: Message-ID: <SOME_ENCRYPED_STRING#MY_SERVER_NAME>
CLIENT -> SERVER: X-Priority: 3
CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.10 (https://github.com/PHPMailer/PHPMailer/)
CLIENT -> SERVER: MIME-Version: 1.0
CLIENT -> SERVER: Content-Type: text/plain; charset=iso-8859-1
CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
CLIENT -> SERVER:
CLIENT -> SERVER: This is a plain-text message body
CLIENT -> SERVER:
CLIENT -> SERVER: .
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250 Queued (0.000 seconds)"
SMTP -> get_lines(): $data is "250 Queued (0.000 seconds)"
SERVER -> CLIENT: 250 Queued (0.000 seconds)
CLIENT -> SERVER: QUIT
SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "221 goodbye"
SMTP -> get_lines(): $data is "221 goodbye"
SERVER -> CLIENT: 221 goodbye
Connection: closed
Message sent!
I've tried this test with different emails, on yahoo, gmail, etc, but still the same. I also checked carefully the spam for all my emails, for couple hours.
Any idea?
It is succeeding - in delivering to localhost, as you're asking it to. What happens after that is entirely outside PHPMailer's control. You need to look at your local mail server's log to see what happens after that, but it looks like it's not getting any further than that, so you need to look at your mail server's config.
Related
I have the following code which works fine with an AWS instance with Ubuntu but not with Fedora.
#!/bin/perl
use Net::FTP;
use Net::SMTP;
use warnings;
use POSIX qw(strftime);
my $now_string = localtime;
my $date = strftime "%d/%d/%Y", localtime;
my $checkdate = strftime "%d/%b/%Y", localtime;
my $username = "apikey";
my $passwd = "REDACTED";
my $from = 'Reports <reports#redacted.com';
my $smtphost = 'smtp.sendgrid.net';
my $smtp = Net::SMTP->new($smtphost, Port => 587, Debug => 1);
$smtp->auth($username,$passwd) || die "Authentication failed\n";
The output on the Fedora instance is:
Net::SMTP>>> Net::SMTP(3.11)
Net::SMTP>>> Net::Cmd(3.11)
Net::SMTP>>> Exporter(5.74)
Net::SMTP>>> IO::Socket::IP(0.39)
Net::SMTP>>> IO::Socket(1.40)
Net::SMTP>>> IO::Handle(1.40)
Net::SMTP=GLOB(0x564c8ac970b0)<<< 220 SG ESMTP service ready at ismtpd0027p1las1.sendgrid.net
Net::SMTP=GLOB(0x564c8ac970b0)>>> EHLO localhost.localdomain
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-smtp.sendgrid.net
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-8BITMIME
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-PIPELINING
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-SIZE 31457280
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-STARTTLS
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250-AUTH PLAIN LOGIN
Net::SMTP=GLOB(0x564c8ac970b0)<<< 250 AUTH=PLAIN LOGIN
Authentication failed
The code is exactly the same on both instances but with the Fedora instance Authentication consistently fails.
Manually connecting to sendgrid via the command line also works.
Thank you in advance!
I am sending a mail using the following code in Perl
#!/usr/bin/perl
$to = 'abcd#gmail.com';
$from = 'webmaster#yourdomain.com';
$subject = 'Test Email';
$message = 'This is test email sent by Perl Script';
open(MAIL, "|/usr/sbin/sendmail -t");
# Email Header
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
close(MAIL);
print "Email Sent Successfully\n";
Now how do i get the name of the sender while i am seeing the name. For eg. when i keep $from as abcd#xyz.com it displays name as abc while i want to give it my name like James Lawson.?
Following is the sample code:
$req = "122";
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Connection: close\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
if(!$errno)
{
var_dump($fp);
}
else
{
echo "ERROR: $errno, $errstr";
}
When I connect to the sandbox it produces me a connection timed out error:
ERROR: 110, Connection timed out
So I debugged the problem and found out it was something to do with the SSL and verified the existence and accessibility of openssl in the server.
Tested the openssl and connectivity on the server with the following
"openssl s_client -connect www.sandbox.paypal.com:443"
I get
"Socket: Connection Timed out"
I checked out iptables and there is no blocking for port 443
So I tried again to check the case for paypal.com, like the following:
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
It worked immediately.
So I tried in my other server and I was able to connect to sandbox.paypal.com but not on the server where I need to demo and test payments.
My hosting provider is clueless and I am pulling my hair out here.
I appreciate any help on this.
Yes, ofcourse my first question on Stackoverflow :)
The static $_ might needs to be flushed. It is tried to be done by $|=1;.
DESIRED OUTPUT:
CLIENT
Connected to the Server.
sent to server : SAS4
sent to server : 50
sent to server : SAS_ACTION LOGIN
sent to server : LOGIN bss
sent to server : PASSWORD cleint
sent to server : $END$
Message received from Server : SAS4
Message received from Server : 61
Message received from Server : SAS_ACTION LOGIN_ACK
Message received from Server : ACK_STATUS 0
Message received from Server : ACK_MESSAGE Logged In
Message received from Server : $END$
SERVER
Waiting for the Client.
Connected from : 127.0.0.1, Port : 1862
Message received from Client : SAS4
Message received from Client : 50
Message received from Client : SAS_ACTION LOGIN
Message received from Client : LOGIN bss
Message received from Client : PASSWORD cleint
Message received from Client : $END$
server to client : SAS4
server to client : 61
server to client : SAS_ACTION LOGIN_ACK
server to client : ACK_STATUS 0
server to client : ACK_MESSAGE Logged In
server to client : $END$
CODE :
CLIENT
use strict;
use warnings;
use IO::Socket::INET;
my $socket = new IO::Socket::INET (PeerHost => '127.0.0.1', PeerPort => '1055', Proto => 'tcp', Reuse => 1) or die "$!\n";
print "Connected to the Server.\n";
send_login();
$|=1;
receive_loginack();
$socket->close();
sub send_login
{
my $login_txt = "Login.txt";
open LOGIN, '<', $login_txt or die "Cannot open $login_txt $!\n";
my #login = <LOGIN>;
close LOGIN;
my $logfile = "logfile.txt";
open LOG, '>>', $logfile or die "Cannot open $logfile ($!)\n";
foreach my $login (#login)
{
print $socket $login;
print LOG "CLIENT : $login";
print "sent to server : $login";
#last if ($login eq "\$END\$\n");
}
close LOG;
}
sub receive_loginack
{
while (<$socket>)
{
print"Message received from Server : $_";
}
}
SERVER
use strict;
use warnings;
use IO::Socket::INET;
my $socket = new IO::Socket::INET (LocalHost => '127.0.0.1', LocalPort => '1055', Proto => 'tcp', Listen => 1, Reuse => 1) or die "Oops: $! \n";
print "Waiting for the Client.\n";
server_loop();
$socket->close();
sub server_loop
{
OUTER:
while (my $clientsocket = $socket->accept())
{
print "connected from : ", $clientsocket->peerhost();
print ", port : ", $clientsocket->peerport(), "\n";
INNER:
while (<$clientsocket>) #
{ #
print"Message received from Client : $_"; #
last INNER if ($_ eq "\$END\$\n"); # RECEIVING
#last OUTER if ($_ eq "\$QUIT\$\n"); #
print $clientsocket $_; #
} #
my $login_ack = "login_ack.txt";
open LOGINACK, '<', $login_ack or die "Cannot open login acknowledgment file $login_ack ($!)\n";
my #loginack = <LOGINACK>;
close LOGINACK;
my $logfile = "logfile.txt";
open LOG, ">>", $logfile or die "cannot open $logfile ($!)\n";
foreach my $loginack (#loginack)
{
$|=1;
print $clientsocket $loginack;
print LOG "SERVER : $loginack";
print "server to client : $loginack";
#last if ($loginack eq "\$END\$\n");
}
close LOG;
close $clientsocket;
}
}
The server's input loop contains:
print $clientsocket $_;
So everything the client sends to the server is being sent back to the client, before the login_ack.txt file. So the client prints it as a message received from the server.
I have setup centOs 6 on rackspace server, and installed Apache PHP & other modules.
I also installed sendmail to use mail() function from PHP, it working, but i am not able to set my own header in mail().
$to = "myemail#gmail.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$from = " Team <my#odomain.com>";
$headers = "From: $from\r\n";
$headers = "MIME-Version: 1.0\n" ;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}
But i getting spam emails with header "Apache apache#server". Header is not setting.
I also tried "-f emailaddress", but not working.
What should i do? I had also try some sendmail configuration but still not solved.
Ritesh
Replace \r\n with \n in the line $headers = "From: $from\r\n";