i have a big Problem. I'll fetch all mails of a mailbox with php_imap, connecting to pop3 service of an microsoft exchange server.
The Adress of the Mailbox: sample#sample.com
This Mailbox has many aliases: e.G. sample_test#sample.com, sample_test2#sample.com
My problem: When someone writes a mail to sample_test#sample.com the mail is deliverd to the mailbox. So far thats no Problem. When viewing the Header-Source in Microsoft Outlook it displays
To: Sample "sample_test#sample.com"
Still, no problem!
But ...
after receiving this via my php-script and writing the imap_header and imap_body to the file e.G. mail1.eml viewing the source gives me
To: Sample "sample#sample.com"
Here i wish to get
To: Sample "sample_test#sample.com"
My Code:
Heres the Snippet for Understanding how i save this to eml file ...
$mailbox = '{'.$imapHost.':'.$imapPort.'}INBOX';
$imapStream = imap_open($mailbox, $imapUser, $imapPassword, 0, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI'));
$hdr = imap_check($imapStream);
$overview = imap_fetch_overview($imapStream, "1:$mailCount");
$size = count($overview);
for($i=$count-1;$i>=0;$i--) {
-->$headers = imap_fetchheader($imapStream, $i, FT_PREFETCHTEXT);
-->$body = imap_body($imapStream, $i);
-->file_put_contents($fullPath . '/' . $saveemlfile, $headers . "\n" . $body);
}
imap_close($imapStream);
Thanks for your Help!
Related
I'm having problems sending attachments in mail with mailgun.
When I go to logs in mailgun, the mail sent shows :
"attachments": [],
Here's my code :
$location = Storage::get('attachments/'.$this-attachments->file_name);
return $this->markdown('emails.create',["desc" => $this->mail->description])
->subject($this->mail->subject)
->attach($location);
I got raw encoded codes when I return the $location so i tried doing with public_path() but the result was same( "attachments":[], <- in mailgun logs ).
Am i doing it wrong? How do i get the attachment in the mail? The mail is going through but not with attachments.
Thanks for your time.
Try below code with storage_path():
$location = storage_path('attachments/'.$this-attachments->file_name);
return $this->markdown('emails.create',["desc" => $this->mail->description])
->subject($this->mail->subject)
->attach($location);
You wrote
$location = Storage::get('attachments/' . $this-attachments->file_name);
But it should be
$location = Storage::get('attachments/' . $this->attachments->file_name);
You forgot the > in $this->
I'm using xpertmailer to send email direct to the remote SMTP server following an MX lookup. This works really well and works on an old closed source NAS drive running PHP4 and on current PHP5 boxes.
<?php
define('DISPLAY_XPM4_ERRORS', true); // display XPM4 errors
require_once '/path-to/SMTP.php'; // path to 'SMTP.php' file from XPM4 package
$f = 'me#mydomain.net'; // from mail address
$t = 'client#destination.net'; // to mail address
// standard mail message RFC2822
$m = 'From: '.$f."\r\n".
'To: '.$t."\r\n".
'Subject: test'."\r\n".
'Content-Type: text/plain'."\r\n\r\n".
'Text message.';
$h = explode('#', $t); // get client hostname
$c = SMTP::MXconnect($h[1]); // connect to SMTP server (direct) from MX hosts list
$s = SMTP::Send($c, array($t), $m, $f); // send mail
// print result
if ($s) echo 'Sent !';
else print_r($_RESULT);
SMTP::Disconnect($c); // disconnect
?>
I'm now trying to add an attachment to it, but I've no idea how to get an attachment to be included and sent.
Anyone any ideas how I can do this ?
Thanks
Example:
$m = new MAIL;
// attach source
$a = $m->Attach('text message', 'text/plain');
$f = '/path/image.gif';
// attach file '$f', disposition 'inline' and give a name 'photo.gif' with ID value (this ID value can be used in embed HTML images)
$a = $m->Attach(file_get_contents($f), FUNC::mime_type($f), 'photo.gif', null, null, 'inline', MIME::unique());
echo $a ? 'attached' : 'error';
I had a function to read from gmail then mark it as read and it has been working fine.
We recently switched to office 365 and now we can't mark the mails as read any more. I got no error message. Any help will be appreciated.
Related code:
$authhost = "{outlook.office365.com:993/imap/ssl}INBOX";
$email = "blablabla#companyname.com";//dummy text. I swear i dont have typo here
$emailPassword = "NotGonnaTellYa";//dummy text. No typo here.
$mailbox = imap_open($authhost, $email, $emailPassword);
$mails = imap_search($mailbox, "ALL");
foreach ($mails as $mail) {
$mail_headers = imap_headerinfo($mailbox, $mail);
$structure = imap_fetchstructure($mailbox, $mail);
/*
do something magically to process the emails then save it to local..
*/
imap_delete($mailbox, $mail);
}
imap_expunge($mailbox);
imap_close($mailbox);
I have given the folders Owner permission in office 365 mail but failed to find any settings about allow IMAP to delete mails.
I have tried using CL_EXPUNGE to open the mail box but no luck either.
How can I copy email from Gmail to my server's /home/email directory after connecting to a Gmail mailbox using PHP's IMAP functionality?
I want to retrieve every email as a file in MIME format and I want to download the complete MIME file with PHP, not just the body or header of the email. Because of this, imap_fetchbody and imap_fetchheader can't do the job.
Also, it seems imap_mail_copy and imap_mail_move can't do the job because they are designed to copy / move email to mailboxes:
imap_mail_copy: Copy specified messages to a mailbox.
imap_mail_move: Move specified messages to a mailbox.
PHP will download the full MIME message from Gmail or any IMAP server using imap_fetchbody when the $section parameter is set to "". This will have imap_fetchbody retrieve the entire MIME message including headers and all body parts.
Short example
$mime = imap_fetchbody($stream, $email_id, "");
Long example
// Create IMAP Stream
$mailbox = array(
'mailbox' => '{imap.gmail.com:993/imap/ssl}INBOX',
'username' => 'my_gmail_username',
'password' => 'my_gmail_password'
);
$stream = imap_open($mailbox['mailbox'], $mailbox['username'], $mailbox['password'])
or die('Cannot connect to mailbox: ' . imap_last_error());
if (!$stream) {
echo "Cannot connect to mailbox\n";
} else {
// Get last week's messages
$emails = imap_search($stream, 'SINCE '. date('d-M-Y',strtotime("-1 week")));
if (!count($emails)){
echo "No emails found\n";
} else {
foreach($emails as $email_id) {
// Use "" for section to retrieve entire MIME message
$mime = imap_fetchbody($stream, $email_id, "");
file_put_contents("email_{$email_id}.eml", $mime);
}
}
// Close our imap stream.
imap_close($stream);
}
I am working on a massmail script which sends an e-mail to every e-mail id present in a particular database.
But there is some issue.
Like I have following database:
id email link
1 a#bc.com bc.com
2 b#cd.com cd.com
And suppose the mail content is : 'Testing this script'
The scripts sends email to a#bc.com perfectly but second time it sends the email, i.e to b#cd.com the content gets doubled.
I mean the second recipient receives an e-mail like this :
Testing this script
Testing this script
The third recipient receives an e-mail with the content repeat three times and the fourth one receives it with four times and so on.
The script grabs e-mail addresses from the email field in the database and sends e-mail to them.
My Code:
<?
include "header.php";
include "config2.php";
$subject="Massmail";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
mysql_connect($server, $db_user, $db_pass)
or die ("Database CONNECT Error");
$resultquery = mysql_db_query($database, "select * from $table");
while ($query = mysql_fetch_array($resultquery))
{
$mailto=$query[$table_email];
$domain=$query[$table_link];
$domain2 = str_replace(array('http://','HTTP://','Http://'), '',$domain);
$handle = fopen("http://$domain2","r") or die("Unable to open link ( $domain ). <a href='javascript:history.go(-1);'>Go back</a> and please try again ");
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
$contents = str_replace('window.location = "/abc.html"','window.location = ""',$contents);
$contents = mb_convert_encoding($contents, "HTML-ENTITIES", "auto");
}
$i = md5(uniqid(rand(), true)) . '.' . html;
$fh = fopen("/home/host/public_html/content/$i", "w");
fwrite($fh, $contents);
fclose($fh);
$filename = '/files/$i';
$message1 .= "Testing Mail Script Version 2";
mail($mailto, $subject, $message1, $headers, "-f" . 'noreply#domain.com');
echo 'Mail sent to '.$mailto.'<br>';
sleep($seconds);
}
include "footer.php";
?>
I have tried to echo the mail that has to be sent and I get this:
To: a#bc.com
Subject: Massmail
Message:
Testing mail script
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
to: b#cd.com
Subject: Massmail
Message:
Testing mail scriptTesting mail script
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Your help will be greatly appreciated. Thanks in advance
In the code line: $message1 .= "Testing Mail Script Version 2"; the period is a concatenate, so each time you loop it, you duplicate the message another time.
Agreed. Sounds like your loop is not functioning correctly? If you can't post the full code, try to post an abstraction for us to diagnose your issue.
If you cant, I would look into looping based on the number of email address you have, not some arbitrary counter. That might help you out.
Suppose this is your table. With id, email, link, message structured as:
id email message
1 a#test.com hello, how are you doing..
2 b#test.com hey, dude this is me..
3 c#test.com we are sending you this...
Now, let's assume you have 100 records. What I would do is (assuming $result is a mysql array returning all the results from mysql ex SELECT * FROM mail...)
I would try:
for($i=0; $i <= count($result); $i++){
$send = mail($result['to'], $result['subject'],
$result['message'], $result['headers']);
if(!$send){
echo 'e-mail, sending has stopped';
break;
}
else{
echo 'all e-mails are sent successfully'; }
}
Never mind, I fixed it. #Pirion's post helped me. I changed
$message1 .= "Testing Mail Script Version 2";
to
$message = "Testing Mail Script Version 2";
And everything worked perfectly. Thank You So Much Guys For Helping Me Out.