AMP-Access-Control-Allow-Source-Origin is not equal to the current - php

Alrighty, I've been beating my head against this one for a few days and after extensive Googling (there's a lot of purple links when I search anything regarding this now) I'm pretty much stumped. The form works both from our domain and the AMP one, so maybe I should just get over it and let it be. At the same time though, I want it to be correct so hopefully someone can help me out here.
I have read the AMP CORS documentation a few times and everything seems all good to me.
On form submission, all of our forms return the error below. Visually the URL's appear the same. I've pasted them into a plain text editor and all seems good there too.
As a result of this error our forms technically throw an error message on submit. We have just flipped the messages around for now so an error pops up as a success, which is probably not the greatest as if it should properly succeed it will throw an error message.
We are running the headers out of our htaccess file, code below for that.
<IfModule mod_headers.c>
Header set Access-Control-Allow-Credentials "true"
#Header set Access-Control-Allow-Origin "static.craigmanufacturing.com"
#Header set Access-Control-Allow-Origin "https://www-craigattachments-com.cdn.ampproject.org"
#Header set Access-Control-Allow-Origin "https://www-craigattachments-com.amp.cloudflare.com"
SetEnvIf Origin "https://(static.craigmanufacturing.com|www-craigattachments-com.cdn.ampproject.org|www-craigattachments-com.amp.cloudflare.com|https://cdn.ampproject.org)$" AccessControlAllowOrigin=$0$1
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header set Access-Control-Allow-Credentials "true"
Header set Access-Control-Allow-Source-Origin "https://www.craigattachments.com"
Header set Access-Control-Expose-Headers AMP-Access-Control-Allow-Source-Origin
Header set AMP-Access-Control-Allow-Source-Origin "https://www.craigattachments.com"
</IfModule>
Anyone have any thoughts on why we may be getting this error that I may be missing? You can try out our contact form here: https://www.craigattachments.com/contact-us - all messages go to an inbox that only I have access to so no worries on hitting it.
I will happily provide more details on this if required as I'm sure I may have forgotten to include some crucial detail as my brain is mush about this issue at this point.
Edit: here's the PHP (stripped down a bit of my details and the HTML) behind sending the form as well. Doubled up on headers in here because otherwise it seems to throw a JSON syntax error, even though after testing the error above is coming from the htaccess file. I have also tried stripping the headers from htaccess and including them in the PHP only.
<?php
header("Access-Control-Allow-Headers:Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token");
header("Access-Control-Allow-Methods:POST, GET, OPTIONS");
header("Access-Control-Allow-Origin:".$_SERVER['HTTP_ORIGIN']);
header("Access-Control-Expose-Headers:AMP-Access-Control-Allow-Source-Origin");
//header("AMP-Access-Control-Allow-Source-Origin:https://".$_SERVER['HTTP_HOST']);
//header("AMP-Access-Control-Allow-Srouce-Origin:https://www.craigattachments.com");
header("AMP-Access-Control-Allow-Source-Origin:".$_REQUEST['__amp_source_origin']);
header("Content-Type: application/json");
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
set_include_path("." . PATH_SEPARATOR . ($UserDir = dirname($_SERVER['DOCUMENT_ROOT'])) . "/pear/share/pear" . PATH_SEPARATOR . get_include_path());
require_once "Mail.php";
require_once "Mail/mime.php";
$host = "###";
$username = "###";
$password = "###";
$port = "###";
$to = "###";
$email_from = "###";
$email_name = $_POST['fullname'];
$email_subject = $_POST['subject'];
$email_body = $_POST['message'];
$email_address = $_POST['sender_email'];
?>
<?php
$html_email = '';
$crlf = "\n";
echo json_encode(array($email_subject, $email_address, $html_email, $email_name));
$headers = array ('From' => $email_name . "<" . $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));
$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html_email);
$html_email = $mime->get();
$headers = $mime->headers($headers);
list($user,$domain) = explode('#', $email_address);
$mail = $smtp->send($to, $headers, $html_email);
?>
Edit2: I updated my code to include my commented out other attempts.

This header allows the specified source-origin to read the authorization response. The source-origin is the value specified and verified in the "__amp_source_origin" URL parameter (for example, "https://publisher1.com").
The value needs to be an allowed origin, not a comma-separated list of allowed origins which is what you are providing.

Related

CakePHP : Add “List-Unsubscribe” in email header using Email component

I am usign cakephp 2.x and using Email components to send email.
I am trying to add List-Unsubscribe at headers.
$this->Email->headers = [
'List-Unsubscribe'=>'<mailto:'.$email_from.'?subject=Remove from Mailing List>, <'.$SITEURL.'unsubscribe?em='.$email_to.'>',
'List-Unsubscribe-Post'=>'List-Unsubscribe=One-Click'
];
Now in email source its showing in X-header.
X-List-Unsubscribe: <mailto:clients#example.com?subject=Remove from Mailing List>, <http://example.com/unsubscribe?em=xyz#example.com>
X-List-Unsubscribe-Post: List-Unsubscribe=One-Click
But it's not showing Unsubscribe link with From.
I need List-Unsubscribe in message header to avoid spam email.
When i try to use $this->Email->additionalParams its not showing List-Unsubscribe in email header.
Here is code that i am using
$send_from = $email_from_name . "<" . $email_from . ">";
$this->Email->sendAs = 'both'; // text / html / both
$this->Email->from = $send_from;
$this->Email->replyTo = $email_from;
$this->Email->return = $email_from;
$this->Email->to = $email_to;
$this->Email->delivery = 'smtp';
$this->Email->smtpOptions = ['host'=>$imap_server,'port'=>587,'username'=>$email,'password'=>$password];
$this->Email->subject = $subject;
$this->Email->headers = [
'List-Unsubscribe'=>'<mailto:'.$email_from.'?subject=Remove from Mailing List>, <'.SITEURL.'unsubscribe?em='.$email_to.'>',
'List-Unsubscribe-Post'=>'List-Unsubscribe=One-Click' ];
$this->Email->textMessage = $this->_html_to_text($content);
//$this->Email->delivery = 'debug';
$this->Email->send($content);
The email component doesn't support custom non X-prefixed headers, if you need that you'll have to use CakeEmail, which by default doesn't prefix headers, and requires you to explicitly pass prefixed headers in case required.
Given that the email component is long deprecated, ever since the first release of CakePHP 2 to be specific, now is probably a good time for you to finally drop it.
Quick and dirty example:
App::uses('CakeEmail', 'Network/Email');
// The transport/connection configuration should probably better be moved into a config file
$Email = new CakeEmail(array(
'transport' => 'Smtp',
'host' => $imap_server,
'port' => 587,
'username' => $email,
'password' => $password
));
$Email
->emailFormat('both')
->template('subscribe')
->from($send_from)
->replyTo($email_from)
->returnPath($email_from)
->to($email_to)
->subject($subject)
->addHeaders(array(
'List-Unsubscribe' =>
'<mailto:' . $email_from . '?subject=Remove from Mailing List>, ' .
'<' . $SITEURL . 'unsubscribe?em=' . $email_to . '>',
'List-Unsubscribe-Post' => 'List-Unsubscribe=One-Click'
))
->send($content);
This would require two templates, one for HTML in app/View/Emails/html/subscribe.ctp:
<?php
// $content contains the data passed to the `send()` method, wrapped to max 998 chars per line
echo $content;
and one for text in app/View/Emails/text/subscribe.ctp, requiring you to move the HTML to text conversion into the template. You should probably make it a helper, the following should just illustrate the principle:
<?php
echo _html_to_text($content);
See also
Cookbook > Core Libraries > Utilities > CakeEmail > Configuration
Cookbook > Core Libraries > Utilities > CakeEmail > Sending templated emails

PHP: Sending Email with Cyrillic (Ukrainian text)

Problem with sending Cyrillic Email with PHP.
My side:
Server IIS - Database MsSQL - email server: Exchange 2010 /communication via PHP EWS/
Reciever is UA Goverment owned company with their specific software for receiving emails. It is working with MS Outlook /manually send/.
I tried send it as text /not html/ or i tried PHP Mailer, i also already tried with C# /all are not working with this specific company /on gmail or hotmail it's working fine//.
$ews = new ExchangeWebServices($server, $username, $password);
$msg = new EWSType_MessageType();
$toAddresses = array();
$toAddresses[0] = new EWSType_EmailAddressType();
$toAddresses[0]->EmailAddress =;
$toAddresses[0]->Name =;
$msg->ToRecipients = $toAddresses;
$fromAddress = new EWSType_EmailAddressType();
$fromAddress->EmailAddress =;
$fromAddress->Name =;
$msg->From = new EWSType_SingleRecipientType();
$msg->From->Mailbox = $fromAddress;
$msg->Subject = "Test";
$msg->Body = new EWSType_BodyType();
$msg->Body->BodyType = 'HTML'; //Text HTML
$msg->Body->_ = $UAText;
$msgRequest = new EWSType_CreateItemType();
$msgRequest->Items = new EWSType_NonEmptyArrayOfAllItemsType();
$msgRequest->Items->Message = $msg;
$msgRequest->MessageDisposition = 'SendAndSaveCopy';
$msgRequest->MessageDispositionSpecified = true;
$response = $ews->CreateItem($msgRequest);
var_dump($response);
Thank You,
If its working with an normal Outlook client, however not with your solution my first check would be to compare the header from two example emails (one from outlook and one from your solution). I think that the content-type is set correctly with Outlook however not with your solution.
So you might wish to set the content encoding to UTF-8 in your solution. So I assume the content inside $UAText is some HTML stuff. You might therefore wish to flag that part as UTF-8 via:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
and see how it works.
Additional you might wish to set the encoding directly inside your code via:
$ews = new ExchangeWebServices($host, $user, $password, ExchangeWebServices::VERSION_2007_SP1);
$msg = new EWSType_MessageType();
$msg->MimeContent = new EWSType_MimeContentType();
$msg->MimeContent->_ = base64_encode("Mime-Version: 1.0\r\n"
. "From: michael#contoso.com\r\n"
. "To: amy#contoso.com\r\n"
. "Subject: nothing\r\n"
. "Date: Tue, 15 Feb 2011 22:06:21 -0000\r\n"
. "Message-ID: <{0}>\r\n"
. "X-Experimental: some value\r\n"
. "\r\n"
. "I have nothing further to say.\r\n");
$msg->MimeContent->CharacterSet = 'UTF-8';
Note: Here is a good starting point regarding the content-type encoding option. You also might wish to check the official Microsoft howto here.

Mail Mime image in email body not showing up using MailMime addHtml(...)

Mail_Mime 1.8.9
Mail 1.2.0
php 5.4.12
project = run from my localhost dev computer (LAN).
Hello, I am attempting to add an image to my email I am sending out using MailMime. The image never loads. I'll go through the steps below:
Here I am stripping everything off of the image but the image name and extension (ie:noimage.jpg). I then create a link for non image related purposes, I then create the image tag using the stripped image name and extension.
$url = substr($baseImage, 36);
$domainName = DomainNameUtil::getWebSiteDomainName();
$projectPath = DomainNameUtil::getPathToProject();
$productUrl = $domainName . $projectPath . "/client/app/#/products/" . $product->id;
$productLink = "<a href='$productUrl'>$product->name</a>";
$string = "<img src='$url' /><br />";
Here I am sending the message
$mailConfig = MailConfigurationUtil::getMailConfigurationData();
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$crlf = "\n";
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setHTMLBody($body);
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', array (
'host' => $mailConfig->mailServer,
'port' => $mailConfig->port,
'auth' => true,
'username' => $mailConfig->userName,
'password' => $mailConfig->password
));
$productManager = new ProductManager();
$ordersManager = new OrdersManager();
$orderVirtualItem = $ordersManager->getOrderVirtualItemByBoxId($boxId);
$product = $productManager->getProductById($orderVirtualItem->itemId);
$url = $product->baseImage;
$domain = DomainNameUtil::getWebSiteDomainName();
$path = DomainNameUtil::getPathToProject();
$r = substr($url, 5);
$finalUrl = $domain . $path . $r;
$mime->addHTMLImage(file_get_contents($finalUrl),'image/jpeg',basename("noimage"),false, "blackstone");
$body = $mime->get();
$mail = $smtp->send($to, $headers, $body);
In the example above, I know the image is called noimage.jpeg, so in the addHtmlImage above I simply stated the name. I am under the impression that the name used in addHtmlImage has to be the same name as the image name in the image src tag in the html body.
When I actually send my email I get the email and I get some text saying there is an image, but the image tag in my email body does not load.
Also, looking at it under firebug it has a proxy attached to the image src. Also it references it as PROXYADDRESS#http://noimage.jpg
Anyone have experience in this that can help me understand why the image is not loading?
For reference, I saw someone posting this method online and tried copying them. before this I had even tried putting the whole url in the image src and not adding the image to MIME, and it did not work.
i got pretty similar problem here.
What i got so far: it depends on phpversion (and installed Mail_Mime).
Mail_Mime seems to be "broken" under php 5.4.30
If i run my script under php 5.2.17 all images show up correctly
If i run my script under php 5.4.30 all images do not show up correctly
when i compare both sourcecodes, it seems, Mail_Mime under 5.4.30 gets messy with the order of boundarys and the Content-Type:
Content-Type: multipart/alternative; in 5.2.17
Content-Type: multipart/related; in 5.4.30enter image description here
When specifying a context-id (the last parameter to addHtmlImage()) you must reference the image in the html message correctly: <img src="cid:context-id#local" />. Of course replacing context id with your own context id for each image to show.

utf-8 charset not working on contact form with PEAR Mail_mime

I've found and tried various solutions given in other questions about utf-encoding for special characters and other related problems, but without any success.
I have an html contact form that sends information to my mail address with a simple php script using PEAR Mail mime. I can send information containing special characters from my test site on localhost with no problems, but not after I uploaded to my server.
eg message:
Test special characters: é è ç à ô
after being sent from server becomes:
Test special characters: é è ç à ô
I am guessing it's an encoding problem from my web server, but am kinda stuck at how to resolve the problem.
The meta tags in file containing the form are set to:
<meta charset="utf-8">
and the form is specified to accept charset utf-8:
<form name="contact" method="post" action="assets/send_form.php" accept-charset="UTF-8">
I've also tried sending content from php file with:
header("Content-Type: text/html; charset= UTF-8");
as well as creating $headers for the message with 'Content-Type' = 'text/html; charset="UTF-8".
The relevant php code in my script:
//This section creates the email headers
$headerss=array();
$headerss['From']= $from_address;
$headerss['To']= $siteEmail;
$headerss['Subject']= $email_subject;
$headerss['Return-Path']= $contactEmail;
$headerss['Date']= date("r");
$headerss['Content-Type'] = 'text/html; charset="UTF-8"';
// This section creates the smtp inputs
$auth = array('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password);
// create new Mail_mime instance, set utf-8 charset
$mail = new Mail_mime();
$mail -> setHTMLBody($email_message);
//CHECK THIS OUT FOR UTF-8 *****************************
$mimeparams=array();
$mimeparams['text_encoding']="7bit";
$mimeparams['text_charset']="UTF-8";
$mimeparams['html_charset']="UTF-8";
$mimeparams['head_charset']="UTF-8";
$mimeparams['eol']= "\n" ;
$body = $mail->get($mimeparams);
$headers = $mail->headers($headerss);
// This section send the email
$smtp = Mail::factory('smtp', $auth);
$sendmail = $smtp->send($siteEmail, $headers, $body);
Any help will be really appreciated. Thanks.
Try to put before
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I experienced the same problem and my solution was to save the php file in same format, in UTF-8 as I had saved in ANSI format. That solved my problem anyway.

Problem with character encoding on email sent via PHP?

Having some trouble sending properly formatted HTML e-mail from a PHP script. I am running PHP 5.3.0 and Apache 2.2.11 on Windows XP Professional.
The output looks like this:
Agent Summary for Support on Tuesday April 20 2010=20
Ext. Name Time Volume
137 Agent Name 01:27:25 1
138 =09 00:00:00 0
139 =09 00:00:00 0
You see the =20 and =09 in there? If you look at the HTML you also see = signs being turned into =3D. I figure this is a character encoding issue as I read the following at Wikipedia:
ISO-8859-1 and Windows-1252 confusion
It is very common to mislabel text data with the charset label ISO-8859-1, even though the data is really Windows-1252 encoded. In Windows-1252, codes between 0x80 and 0x9F are used for letters and punctuation, whereas they are control codes in ISO-8859-1. Many web browsers and e-mail clients will interpret ISO-8859-1 control codes as Windows-1252 characters in order to accommodate such mislabeling but it is not standard behaviour and care should be taken to avoid generating these characters in ISO-8859-1 labeled content.
This looks like the problem but I don't know how to fix. My code looks like this:
ob_start();
report_queue_summary($yesterday,$yesterday,$first_extension,$last_extension,$queue);
$body_report = ob_get_contents();
ob_end_clean();
$body_footer = "This is an automatically generated e-mail.";
$message = new Mail_mime();
$html = $body_header.$body_report.$body_footer;
$message->setHTMLBody($html);
$body = $message->get();
$extraheaders = array("From"=>"***redacted***","To"=>$recipient, "Subject"=>"Agent Summary for $yesterday [$queue]", "Content-type"=>"text/html; charset=iso-8859-1");
$headers = $message->headers($extraheaders);
# setup e-mail;
$host = "*********";
$port = "26";
$username = "*****";
$password = "*****";
# Send e-mail
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($recipient, $extraheaders, $body);
if (PEAR::isError($mail)) {
echo("" . $mail->getMessage() . "");
} else {
echo("Message successfully sent!");
}
Is the problem that I'm using output buffering?
The problem is that you need the following header:
Content-Transfer-Encoding: quoted-printable

Categories