How to mark the emails as spam and not spam in PHP? - php

I am working on my PHP code to mark my emails as spam and not spam. I need some help with the code, because I am not sure which variable I should use to send the report to spamassassin to mark my emails as spam and not spam.
I am using the script from this link:
https://samprasanna.blogspot.com/2017/06/php-api-to-spamassassin-spamd-protocol.html
Here is the code:
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
require_once('Mail.php');
require_once('Mail/IMAPv2.php');
require('Spamassassin/Client.php');
require('Spamassassin/Client/Result.php');
// Connect to the server:
$username = 'username';
$password = 'tester;
$params = array(
    "hostname" => "localhost",
    "port" => "783",
    "user" => "user");
$sa = new Spamassassin\Client($params);
$inbox = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX';
$inbox = imap_open($inbox, $username, $password) or die("Can't connect: " . imap_last_error());
$email_number = '121';
$message = imap_fetchbody($inbox, $email_number, "", FT_UID);
$header = htmlspecialchars(imap_fetchbody($inbox, $email_number, "", FT_UID|FT_PEEK));
?>
It works fine, but I need some help with this:
$sa->report($message)
Report message as spam, both local and remote
And this:
$sa->revoke($message)
Revokes a message previously reported as spam
When I send the report to spamassassin, which variable do I have to use $message or $header to report the email as spam and not spam??
Do I have to use $message or $header when send report to spamassassin??
I guess I would have to use the $header variable when I report the emails as spam and not spam but I am not really sure.
Any advice would be much appreciated.

Related

How to open all emails in a utf8 encode?

Below is my code that I open "Zoho" to get unread emails.
The problem is that the encoding from emails coming from Gmail and Outlook are different.
Gmail is utf-8 and Outlook (live) is iso-8869-7.
How to handle this?
$hostname = '{imappro.zoho.com:993/imap/ssl}INBOX';
$username = 'email#email.com';
$password = 'password';
/* try to connect */
$inbox = imap_open($hostname,$username ,$password) or die('Cannot connect to Zoho: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'UNSEEN');

Gmail keeps sending emails from local machine

I developed a site with PHPMailer and used gmail smtp to send emails from my local machine.
My code:
`require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/classes/system.class.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/helpers/send_email.helper.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/modules/settings/classes/settings.class.php';
$sClass = new System();
$settingClass = new Settings();
$template = file_get_contents('../html/frontend/email_contact_form.html');
$site_email = $settingClass->get_setting_value('email_from_address');
$template_data['name'] = $_POST['name'];
$template_data['phone'] = $_POST['phone'];
$template_data['email'] = $_POST['email'];
$template_data['message'] = $_POST['message'];
$template = $sClass->merge_data($template, $template_data);
$result = send_email($_POST['email'], $_POST['name'], $_POST['email'], $_POST['name'], $site_email, 'Ruth Rudin', $_POST['subject'], $template);
echo '<p>Your message has been sent.</p>`
On the live server, I do not use gmail smtp.
My client keeps getting emails from root#localhost.
Could these be emails I tested and got stuck in the queue?
If so, where can I see this queue and delete these emails?
Very confused here.
Thank you

gmail not responding while connecting through imap?

I wrote a simple 3 line code for connecting to gmail through imap-php.
i am using 000webhost for running it.
but when i try to open it, it keeps on loading and after some times shows "Problem loading page" or "No data recieved", as if gmail servers are not responding!!
Any suggestions whats the problem?
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";
$username = 'abc#gmail.com';
$password = "abc123";
$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: ' . imap_last_error());
$server_name = "imap.gmail.com";
$account_name = "email#mydomain.com";
$account_pwd = "my_account_password";
$inbox = imap_open("{".$server_name.":993/imap/ssl/novalidate-cert}INBOX",
$account_name, $account_pwd) OR error_imap_mail("Unable To Open Imap
Connection","Imap Failure",imap_errors());
function error_imap_mail($subject,$messagetext,$errors) {
global $report_email;
$mailfrom = "support#mydomain.com";
$mailsubject = "$subject";
$mailbody = "$messagetext\n\n";
if (count($errors) >0) {
if (substr($errors[0],0,8) <> "[IN-USE]") {
array_reverse($errors);
foreach ($errors as $key=>$value) {
$mailbody .= "\nErr No.".$key." Error:".$value;
}
foreach ($report_email as $value) {
mail($value, $mailsubject, $mailbody, "From: $mailfrom");
}
}#if (substr($err_array[0],0,8) <> "[IN-USE]") {
}#if (count($errors) >0) {
exit;
}
Get your script to send you the errors if it fails to connect.
Have you turned IMAP on in the account setting in Gmail?
Try this:
$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$username = 'abc#gmail.com';
$password = "abc123";
$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: ' . imap_errors());
Also make sure you enable IMAP for you account via GMail settings.

Sending an email verification after registering

I want, that users who register on my site, have to activate their account first, before using it. The problem is, that I don't get any email to my email test account.
Before I start posting a code, could the problem be, that I'm working currently on a local machine with xampp?
Otherwise, here is the code snippet
$random = substr(number_format(time() * rand(),0,'',''),0,10);
$insertMailVerify = $this->db->prepare("INSERT INTO mailverify (mailAddress, token, datetime) VALUES (:mailAddress, :token, :date)");
$insertMailVerify->execute(array(':mailAddress'=>$emailAddress,
':token'=>$random,
':date'=>$date));
$to = $emailAddress;
$subject = "Activating your Account";
$body = "Hi, in order to activate your account please visit http://localhost/FinalYear/activation.php?email=".$emailAddress." and fill in the verification code $random";
if(mail($to, $subject, $body))
{
echo ("<p>Message success</p>");
}
else {
echo ("<p>Message fail</p>");
}
Just in case you wonder where i take $emailAddress from: This is just a code snippet, i already let the software echo the email address, and it's correct. It even goes in the "Message success" if case, but I still can't get any email. What could be the problem?
After submit the form you can use a code or link and send it to the user email id
$message = "Your Activation Code is ".$code."";
$to=$email;
$subject="Activation Code For Talkerscode.com";
$from = 'your email';
$body='Your Activation Code is '.$code.' Please Click On This link Verify.php?id='.$db_id.'&code='.$code.'to activate your account.';
$headers = "From:".$from;
mail($to,$subject,$body,$headers);
echo "An Activation Code Is Sent To You Check You Emails";
Code from TalkersCode.com for complete tutorial visit http://talkerscode.com/webtricks/account-verification-system-through-email-using-php.php
in local host i think the best way is using phpmailer and gmail account
here the tutorial : http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
It seems your mail server SMTP is not configured correctly....
check the port and IP of SMTP server address.
Try to change you PHP.ini file like this and tell me if it works.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465 //if 465 is not working then try 25
If this is not working then there are a lot of tutorials of how to achieve what you are trying to do.
Here is one link: Send email from localhost
Had the same problem, but on linux.
1) Assuming your mail() function is working properly: the problem could be, that email is coming into spam-box (because these localhost mail systems are often marked as spambots, so email services are protecting emails from unverified host).
2) If mail() is not working, its not configured properly, if you follow some tutorial and configure it, which needs like 5 minutes, you will realise why not to use it:)
The best way for you is to use some free or paid smtp service. Very easy, quick and your emails wont be marked as spam. And install PEAR library to send email. Here is an example.
$from = 'from#domain.com';
$to = 'to#domain.com';
$subject = 'subject';
$body = 'Hello!';
$host = 'smtpserver.com';
$port = '25';
$username = 'yourlogin';
$password = 'yourpass';
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if(PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
To your verification code: instead of telling user to write down the verification code, better generate him the link, that he clicks his account gets activated. Something like
http://localhost/FinalYear/activation.php?email=some#email.com&token=79054025255fb1a26e4bc422aef54eb4
on registration page: generate the activation token like md5($email . '_sometext');
on activation page if(md5($_GET['email'] . '_sometext') == $_GET['token']) { activate.. }
please note, that it is just an example, there are 10 ways how it could be done :)

accessing my gmail inbox via php code

how i can access my gmail account through my php code? I need to get the subject and the from address to from my gmail account.And then i need to mark the accessed as read on gmail
Should i use gmail pop3 clint?is that any framework that i can use for accessing gmail pop3
server.
I would just use the PHP imap functions and do something like this:
<?php
$mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "USERNAME#googlemail.com", "PASSWORD");
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>
This connects to imap.googlemail.com (googlemail's imap server), sets $subject to the subject of the first message and $from to the from address of the first message. Then, it marks this message as read. (It's untested, but it should work :S)
This works for me.
<?php
$yourEmail = "you#gmail.com";
$yourEmailPassword = "your password";
$mailbox = imap_open("{imap.gmail.com:993/ssl}INBOX", $yourEmail, $yourEmailPassword);
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>
You can use IMAP from PHP.
<?php
$mbox = imap_open("{imap.example.org:143}", "username", "password")
or die("can't connect: " . imap_last_error());
$status = imap_setflag_full($mbox, "2,5", "\\Seen \\Flagged");
echo gettype($status) . "\n";
echo $status . "\n";
imap_close($mbox);
?>
Another nice IMAP example is available at http://davidwalsh.name/gmail-php-imap
Zend Framework has the Zend_Mail API for reading mail as well. It makes it easy to switch protocols if need be (POP3, IMAP, Mbox, and Maildir). Only the IMAP and Maildir storage classes support setting flags at this time.
http://framework.zend.com/manual/en/zend.mail.read.html
Read messages example from the Zend Framework docs:
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'localhost',
'user' => 'test',
'password' => 'test'));
echo $mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
echo "Mail from '{$message->from}': {$message->subject}\n";
}

Categories