I have a problem to send email using php. Here is my code.
<?php
// error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail.php";
$host = "ssl://mail12.heteml.jp";
$username = "username#mail.com";
$password = "mypassword";
$port = "465";
$to = "myname#hotmail.com";
$email_from = "from#mail";
$email_subject = "Subject Line Here:" ;
$email_body = "whatever you like" ;
$email_address = "email-address";
$headers = array ('From' => $email_from, 'To' => $to, 'Subject' => $email_subject, 'Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo "<p>Message successfully sent!</p>";}
?>
This is my code. It works . But all mails are sent in their junk folder. I can't solve this problem. Please help me if you know. Thank you.
Related
Any time i use a variable for $to, the message isn't sent, and when i use "" for the email directly, it is sent. Below is my code...
$snd = "mail#mail.com";
$to = $snd; // this is your Email address
$from = $frome; // this is the sender's Email address
$titl = $title;
$subject = "Quotes From Eagle";
$message = $titl . " " . $quote;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
But when i use the below, it is sent.
$to = "mail#mail.com"; // this is your Email address
$from = $frome; // this is the sender's Email address
$titl = $title;
$subject = "Quotes From Eagle";
$message = $titl . " " . $quote;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
where am I getting this wrong?.
Thanks
I'm having problems with the BUILD method, I want to connect to an HTML-based chat. I wish this class would keep me logged in order to execute the action of sending and receiving messages.
I tried the way down, but it seems that causes a stress on the server while running.
package Shoutbox;
use common::sense;
use WWW::Mechanize;
use WWW::Mechanize::DecodedContent;
use JSON -support_by_pp;
use URI::Escape;
use Moose;
our $url = WWW::Mechanize->new();
$url->get("http://www.forum-invaders.com.br/vb/login.php");
$url->submit_form(
fields => {
vb_login_username => 'login',
vb_login_password => 'senha',
});
has 'msg' => (is => 'rw', isa => 'Str');
sub send_msg {
my $self = shift;
my $message = $self->msg;
my $content = $url->decoded_content;
$content =~ /SECURITYTOKEN = "(.*?)"/g ;
my $token = $1;
if($content =~ /Bem-vindo/gi) {
my $msg = uri_escape($message);
$url->post("http://www.forum-invaders.com.br/vb/vbshout.php",{
message => $msg, securitytoken => $token,
do => "ajax", action => "save", instanceid => "2"});
}
}
sub get_msg{
my $r = $url->get("http://www.forum-invaders.com.br/vb/vbshout.php?type=activeusers&do=ajax&action=fetch&instanceid=2");
my $json = JSON->new->relaxed;
my $s = $json->decode($r->decoded_content);
my $msg = $s->{"shouts"}->{0}->{"message_raw"};
my $user = $s->{"shouts"}->{0}->{"musername"};
my $name;
if ($user =~ />(.+)<\/span/gi) {$name = $1;}
else {$name = $user}
my $now = join(" => ", $name, $msg) . "\n";
return $now;
}
no Moose;
1;
Soon I received an advice to use the BUILD method, so I did so but did not work.
package Shoutbox;
use common::sense;
use WWW::Mechanize;
use WWW::Mechanize::DecodedContent;
use JSON -support_by_pp;
use URI::Escape;
use Moose;
has 'login' => (is => 'rw', isa => 'Str');
has 'password' => (is => 'rw', isa => 'Str');
our $url;
our $token;
sub BUILD{
my $self = shift;
$url = WWW::Mechanize->new();
$url->get("http://www.forum-invaders.com.br/vb/login.php");
$url->submit_form(
fields => {
vb_login_username => $self->login,
vb_login_password => $self->password,
});
my $content = $url->decoded_content;
$content =~ /SECURITYTOKEN = "(.*)"/g;
if ($1 eq "guest"){
print "Login Error\n";
exit;
}
else (print "Login OK!\n";}
$token = $1;
print $token . "\n";
}
has 'msg' => (is => 'rw', isa => 'Str');
sub send_msg {
BUILD;
my $self = shift;
my $message = $self->msg;
my $msg = uri_escape($message);
$url->post("http://www.forum-invaders.com.br/vb/vbshout.php",{
message => $msg, securitytoken => $token,
do => "ajax", action => "save", instanceid => "2"});
}
sub get_msg{
BUILD;
my $r = $url->get("http://www.forum-invaders.com.br/vb/vbshout.php?type=activeusers&do=ajax&action=fetch&instanceid=2");
my $json = JSON->new->relaxed;
my $s = $json->decode($r->decoded_content);
my $msg = $s->{"shouts"}->{0}->{"message_raw"};
my $user = $s->{"shouts"}->{0}->{"musername"};
my $name;
if ($user =~ />(.+)<\/span/gi) {$name = $1;}
else {$name = $user}
my $now = join(" => ", $name, $msg) . "\n";
return $now;
exit;
}
no Moose;
1;
I am try to send mail through smtp server(gmail) from my localhost(wamp).When running the program it will display the Fatal error: Maximum execution time of 30 seconds exceeded .I was change the timeout gain it will produce same error
<?php
$to = "email#gmail.com";
$nameto = "Who To";
$from = "from#fast2host.com";
$namefrom = "Who From";
$subject = "Hello World Again!";
$message = "World, Hello!";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = "smtp.gmail.com";
$port = "25";
$timeout = "30";
$username = "username";
$password = "password";
$localhost = "27.107.106.163";
$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";
}
?>
Please help me anyone for debug the errors
You have too much redundant code, this is what you should do:
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
// set all of the parameters
$to = "Who To <email#gmail.com>";
$from = "Who From <from#fast2host.com>";
$subject = "Hello World Again!";
$message = "World, Hello!";
$text = strip_tags($message);
// create the headers
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'#mydomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
// create the message
$mime = new Mail_mime("\n");
$mime->setTXTBody($text);
$mime->setHTMLBody($message);
// always call these methods in this order
$body = $mime->get();
$headers = $mime->headers($headers);
// create the smtp mail object
$smtp_params = array(
'host' => 'smtp.gmail.com',
'auth' => true,
'username' => 'myUserName',
'password' => 'myPassWord',
); // end $smtp_params
$smtp = Mail::factory('smtp', $smtp_params);
// send the message
$recipients = array($to);
$mail = $smtp->send($recipients, $headers, $body);
?>
PS. The 'auth'=> true; in the smtp_params array flags for an ssl connection.
I have this simple php mail form. It is working and I can use it for making my forms, but I have a problem:
I want to add 2 or 3 attachments to this form. I tried a lot reading about mail at php.net, but I cannot do it by my self.
<?php
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$phone=$_POST['phone'];
$fax=$_POST['fax'];
$mobile=$_POST['mobile'];
$subject=$_POST['subject'];
$website=$_POST['website'];
$message=$_POST['message'];
$fulltext = "
______________________________________________
|
| This Is $name Information:
|______________________________________________
| Name : $name
|______________________________________________
| E-Mail : $email
|______________________________________________
| Address : $address
|______________________________________________
| Phone : $phone
|______________________________________________
| FAX : $fax
|______________________________________________
| Mobile : $mobile
|______________________________________________
| Subject : $subject
|______________________________________________
| Website : $website
|______________________________________________
| Message : $message
|______________________________________________
";
$to = 'support#site.com';
$subject = 'Connect FORM <<';
$headers = 'From: contactform#site.com' . "\r\n" .
$message = $fulltext;
mail($to, $subject, $message, $headers);
echo 'file ersal shod';
?>
I improved code from http://ru.php.net/manual/ru/function.mail.php#105661 :
<?php
function multi_attach_mail($to, $subject, $message, $files, $sendermail){
// email fields: to, from, subject, and so on
$from = "Files attach <".$sendermail.">";
//$subject = date("d.M H:i")." F=".count($files);
$message .= "\n".count($files)." attachments";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
// preparing attachments
for($i=0;$i<count($files);$i++){
if(is_file($files[$i])){
$message .= "--{$mime_boundary}\n";
$fp = #fopen($files[$i],"rb");
$data = #fread($fp,filesize($files[$i]));
#fclose($fp);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: application/octet-stream; name=\"".basename($files[$i])."\"\n" .
"Content-Description: ".basename($files[$i])."\n" .
"Content-Disposition: attachment;\n" . " filename=\"".basename($files[$i])."\"; size=".filesize($files[$i]).";\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
}
}
$message .= "--{$mime_boundary}--";
$returnpath = "-f" . $sendermail;
$ok = #mail($to, $subject, $message, $headers, $returnpath);
if($ok){ return $i; } else { return 0; }
}
multi_attach_mail("to#somebody.dom",
"Subject of mail" ,
"Hello world!!!",
array($_SERVER["DOCUMENT_ROOT"] . "/first.file", // one file in root of your site
$_SERVER["DOCUMENT_ROOT"] . "/second.file" // another one
),
"from#someone");
?>
I am creating a free forwarding email address system and asked a little while ago about this. What I would like to do is forward an email to the persons real email address while keeping the headers intact. Is there any reason why the below code would not work:
#!/usr/bin/php -q
<?php
$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);
$lines = explode("\n", $email);
$to = "";
$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;
$beforecheck = str_ireplace("to:", "", $to);
$checkdatabase = str_ireplace("#virtualparalegal.com", "", $beforecheck);
mysql_connect("localhost", "virtucb3_admin", "Instant11!") or
die("Could not connect to the Database, the Database returned this error: " . mysql_error());
mysql_select_db("virtucb3_wordpress");
$result = mysql_query("SELECT user_nicename, user_email FROM wp_users WHERE `user_nicename` = $checkdatabase");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if ( $row["user_nicename"] == "$checkdatabase") {
$to = ($row["user_email"]);
}
else {
$to = "info#virtualparalegal.com";
}
}
for ($i=0; $i<count($lines); $i++) {
if ($splittingheaders) {
$headers .= $lines[$i]."\n";
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
} else {
$message .= $lines[$i]."\n";
}
if (trim($lines[$i])=="") {
$splittingheaders = false;
}
}
mail($to, $subject, $message, $headers);
?>
Thank you all in advance.