I'm using PHP's mail() function to send SMS messages. It works fine, except that the message shows as coming from 'myuser#mr2.websitewelcome.com' when I want it to show as 'test#mydomain.com'. Here's the relevant code:
//Set headers and send mail.
$headers = "From: " . "test#mydomain.com" . "\r\n";
$headers .= "Reply-To: ". "test#mydomain.com" . "\r\n";
mail( $from, '', $message, $headers );
I know this question has been asked before, but nothing has helped me so far. I've tried setting the header as '-f test#mydomain.com'. No luck.
Btw, if I send to an email address rather than a phone number, the headers work just fine.
Idk if this has anything to do with it, but my script gets called from an email forwarder that I set up in cpanel like:
Address Forward To
test#mydomain.com |/home/myuser/public_html/handler.php
Also, here are the full headers of an email send to an email address:
Return-Path: <itch3#mr2.websitewelcome.com>
Received: from gateway01.websitewelcome.com (gateway01.websitewelcome.com [67.18.65.19])
by mtain-mk01.r1000.mx.aol.com (Internet Inbound) with ESMTP id 1B28B3800008B
for <myemail#me.com>; Wed, 8 Feb 2012 20:25:56 -0500 (EST)
Received: by gateway01.websitewelcome.com (Postfix, from userid 5007)
id A69B35C21D4BD; Wed, 8 Feb 2012 19:25:55 -0600 (CST)
Received: from mr2.websitewelcome.com (mr2.websitewelcome.com [74.53.229.178])
by gateway01.websitewelcome.com (Postfix) with ESMTP id 9CBAF5C21D49D
for <myemail#me.com>; Wed, 8 Feb 2012 19:25:55 -0600 (CST)
Received: from itch3 by mr2.websitewelcome.com with local (Exim 4.69)
(envelope-from <itch3#mr2.websitewelcome.com>)
id 1RvIln-0003AQ-Cv
for myemail#me.com; Wed, 08 Feb 2012 19:25:55 -0600
To: myemail#me.com
Subject: subject
From: "test#mydomain.com" <test#mydomain.com>
Reply-To: test#mydomain.com
Message-Id: <E1RvIln-0003AQ-Cv#mr2.websitewelcome.com>
Date: Wed, 08 Feb 2012 19:25:55 -0600
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - mr2.websitewelcome.com
X-AntiAbuse: Original Domain - aol.com
X-AntiAbuse: Originator/Caller UID/GID - [2146 32003] / [47 12]
X-AntiAbuse: Sender Address Domain - mr2.websitewelcome.com
X-BWhitelist: no
X-Source: /usr/bin/php
X-Source-Args: /usr/bin/php -q /home/itch3/public_html/handler.php
X-Source-Dir: switchon3.com:/public_html
X-Source-Sender:
X-Source-Auth: itch3
X-Email-Count: 2
X-Source-Cap: aXRjaDM7Y3ZncnViYnM7bXIyLndlYnNpdGV3ZWxjb21lLmNvbQ==
x-aol-global-disposition: G
X-AOL-SCOLL-SCORE: 0:2:199012208:93952408
X-AOL-SCOLL-URL_COUNT: 0
x-aol-sid: 3039ac1d61854f3320a4018d
X-AOL-IP: 67.18.65.19
X-AOL-SPF: domain : mr2.websitewelcome.com SPF : none
Can you use the additional parameters argument to (again) declare the from address?
mail('nobody#example.com', 'the subject', 'the message',
"From: webmaster#example.com\r\n", '-fwebmaster#example.com');
I generally add on the additional parameter when using mail() and it tends to fix most issues I've experienced.
You can set the Return-Path to test#mydomain.com, like so:
mail( $from, '', $message, $headers, '-ftest#mydomain.com');
(note the lack of space after -f)
The variable $from is not set, so it defaults. Your code should be:
//Set headers and send mail.
$headers = "From: " . "test#mydomain.com" . "\r\n";
$headers .= "Reply-To: ". "test#mydomain.com" . "\r\n";
$from = "test#mydomain.com";
mail( $from, '', $message, $headers );
Related
I am sending emails to sms using php. I am using #mms.att.net instead of #txt.att.net as it allows me to control the name who the text message is from. #txt.att.net uses a random number that cant be changed. Everything sends great however I am having issues getting line breaks to display how I want. I have tried multiple combinations such as \r\n \n\r \r \n however am having no luck. They all return the same result except \n\r which double spaces. It doesn't help to send the message in text/html as the phones sms program wont interpret <br> and shows as chars in the body if I was to use it. I have tried. So I am sending in text/plain The result I am getting is as follows
Tim Smith
Time: 6PM
Location: Somewhere, USA
Phone: 111-111-1111
Status: Open
And what I am trying to achieve is for it to look like this with no spaces between each line.
Tim Smith
Time: 6PM
Location: Somewhere, USA
Phone: 111-111-1111
Status: Open
My code for PHP mail() is
$txt = "Test". "\n".
"$firstname $lastname\r\n".
"Time: $JobTime\r\n".
"Location: $City, $State\r\n".
"Phone: $Phone\r\n".
"Status: $JobStatus";
$to = "1111111111#mms.att.net";
$subject = "Test Message";
$txt;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
$headers .= "From: Test<test#example.com>" . "\r\n";
mail($to,$subject,$txt,$headers,"-f test#example.com");
Might there be a different method to achieve what I want?
I'm trying to add a line break into a plain text email and after several attempts I don't seem be able to get it to work. Here's what I've tried so far:
$plain = "";
$plain .= "Roll Back Plan:\r\n\r\n";
$plain .= "Roll Back Plan:\r\n\";
$plain .= "Roll Back Plan:\n\n";
$plain .= "Roll Back Plan:\r\r\";
$plain .= "Roll Back Plan:\r";
$plain .= "Roll Back Plan:\n";
$plain .= "Roll Back Plan:" . PHP_EOL;
And none have worked so far.
I appreciate that's been asked before here.
But I've tried the suggested approaches and still nothing...
Any ideas?
EDIT:
This is for a plain text email.
EDIT2: Added raw email
Received: {{REMOVED}}} by
{{REMOVED}}} with Microsoft SMTP Server (TLS) id 14.1.355.2;
Tue, 3 Nov 2015 10:22:07 +0000
Return-Path: {{REMOVED}}}
Received: from {{REMOVED}}}
X-Env-Sender: {{REMOVED}}}
X-Msg-Ref: {{REMOVED}}}
X-Originating-IP: {{REMOVED}}}
X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor:
VHJ1c3RlZCBJUDogOTQuMjM2LjExOS41ID0+IDUzMTM=\n,received_headers: No
Received headers
X-StarScan-Received:
X-StarScan-Version: 7.19.2; banners=-,-,-
X-VirusChecked: Checked
Received: (qmail 10991 invoked from network); 3 Nov 2015 10:22:06 -0000
Received: {{REMOVED}}}
(94.236.119.5) by server-3.tower-56.messagelabs.com with
DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 3 Nov 2015 10:22:06 -0000
X-MSFBL: d2FnbmVyLm1hdG9zQGM0bC5jby51a0BkdnAtOTQtMjM2LTExOS01QGJnLWxvbi0w
MUA0NzktTUVCLTI0MjoxNzYzOjIyOTA6MzkzMzowOjI5MzU6NzoxMDkzMjkz
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1446546126;
s=m1; d=mktomail.com; i=#mktomail.com;
h=Date:From:To:Subject:MIME-Version:Content-Type;
bh=n1kHPhx5cLCEhc6ci0DYK/AkmvxaGYckMaYngTcfmCM=;
b=Nw+P13RuK/SuT1kwJVw1YsC6vlhZBNhBS6PzBcPJeFE2scVxj65d53qFg+hjlUwZ
OX4iIFpee8RJeQFX/3ev2GiKm4KN0+Q3V0chfvvEl5kT9ZCWObgQEj56L4UWAgPKMw8
YZuX9lw/d1FZH0Al/RjKRD0wjITtpg+fvTxj/nY8=
Date: Tue, 3 Nov 2015 04:22:06 -0600
From: {{REMOVED}}}
Reply-To: {{REMOVED}}}
To: {{REMOVED}}}
Message-ID: {{REMOVED}}}
Subject: {{REMOVED}}}
Content-Type: multipart/alternative;
boundary="----=_Part_24932112_12312464.1446546126219"
X-Binding: bg-lon-01
X-MarketoID: 479-MEB-242:1763:2290:3933:0:2935:7:1093293
X-MktArchive: false
List-Unsubscribe: {{REMOVED}}}
X-Mailfrom: {{REMOVED}}}
X-MktMailDKIM: true
X-MS-Exchange-Organization-AuthSource: {{REMOVED}}}
X-MS-Exchange-Organization-AuthAs: Anonymous
MIME-Version: 1.0
------=_Part_24932112_12312464.1446546126219
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
{{REMOVED}}}
Some text And more text<br>And some even more
{{REMOVED}}}
{{REMOVED}}}
------=_Part_24932112_12312464.1446546126219--
You can compose email body as html :
in that case you can use "</br>" tag to line break
If you wish to compose email as plain text:
use "\r\n" for line break
i use the below code to extract header details from the mail.. i could not get the mail address in from, to and cc as mentioned below..
$header = explode("\n", imap_fetchheader($mbox,$msgno));
echo "<br>";
for ($i=1; $i<count($header); $i++)
{
echo $header[$i] . "<br>";
}
output:
Delivered-To: user1#examplecom
X-WM-Delivered: user1#example.com
Received: from ElcotPC ([127.0.0.1])
(envelope-sender )
by 127.0.0.1 with ESMTP
for ; Wed, 31 Jul 2013 09:14:19 +0530
From: "user1"
To:
Cc:
Subject: testing with attachment
Date: Wed, 31 Jul 2013 09:14:18 +0530
The "from","to", "cc" field are empty without the mail address..
i want the output like this..
Delivered-To: user1#examplecom
X-WM-Delivered: user1#example.com
Received: from ElcotPC ([127.0.0.1])
(envelope-sender )
by 127.0.0.1 with ESMTP
for ; Wed, 31 Jul 2013 09:14:19 +0530
From: "user1" <user1#example.com>
To: <user2#example.com>
Cc: <user1#example.com>
how to get the email address to "from", "to" and "cc" field?
Update:
It's always best to use code that is readily available, so I checked if a imap-parsing function exists already. It does: imap_rfc822_parse_headers. Read the docs for details, and links to all sorts of imap_* functions. Perhaps imap_rfc822_parse_adrlist is exactly what you need?
A basic preg_match_all call could do the job, I think:
if (preg_match_all('/^\s*(From|To|Cc):[^<]*<([^>]+)\>/m',$string, $addresses)
{
$addresses = array_merge($addresses[1], $addresses[2]);
print_r($addresses);
}
Should output:
array (
'From' => 'user1#example.com',
'To' => 'user2#example.com',
'Cc' => 'user1#example.com',
)
I think that's what you were looking for.
The regex explained:
^\s* matches the start of the line, and zero or more whitespace chars
(From|To|Cc) matches (and groups) From, To or Cc
:[^<]*<: Matches (but doesn't group) the colon, and any char, except for the address delimiting <
([^>]+): Mathces (and groups) everything after the <, that isn't >
\>: Can be left out, but matches address-delimiting >
m: multi-line. If left out the leading ^ means start of string, now it means start of line
Notes: This expression doesn't deal with comma separated addresses or multiple addresses, and it might be usefull to call:
filter_var($addresses['From'], FILTER_VALIDATE_EMAIL)
or use array_map to filter $addresses[2] prior to merging...
The Content type is handle as a part of message , how to fix that ? thanks
From - Wed Jun 05 12:29:59 2013
X-Account-Key: account1
X-UIDL: 50933ddb0000053d
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
X-Mozilla-Keys:
Return-Path: <flippingST#DPS_FDBD.localdomain>
Received: from xxxxxxxxx (SMTP1 [xxxxxxx])
by xxxxxx (8.13.8/8.13.8) with ESMTP id r554TvMv018216
for <leochan#pop.singtao.com>; Wed, 5 Jun 2013 12:29:57 +0800
Received: from cip.singtaonewscorp.com (cip.singtaonewscorp.com [202.66.86.162] (may be forged))
by xxxxxxx with Microsoft SMTPSVC(xxxxxxxxx);
for <leo.chan#singtaonewscorp.com>; Wed, 5 Jun 2013 12:29:56 +0800
Date: Wed, 5 Jun 2013 12:29:56 +0800
From: flippingST#DPS_FDBD.localdomain
Message-Id: <201306050429.r554TudQ021830#smtp.singtao.com>
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: AogdALq9rlEuic+b/2dsb2JhbABagzk0gkEBhw+jHgsBkhIdTBd0giMBFQE7AQo8FQEBVgcNEySIEQiPSYxDjgYBAYVBAZxtgj6BBwMEC50ji02DSw
X-IronPort-AV: E=Sophos;i="4.87,804,1363104000";
d="scan'208,217";a="25969537"
Received: from ec2-46-137-207-155.ap-southeast-1.compute.amazonaws.com (HELO DPS_FDBD.localdomain) ([46.137.207.155])
by cip.singtaonewscorp.com with ESMTP; 05 Jun 2013 12:30:48 +0800
Received: by DPS_FDBD.localdomain (Postfix, from userid 500)
id D8FEE4329E; Wed, 5 Jun 2013 00:29:28 -0400 (EDT)
To: leo.chan#singtaonewscorp.com
Subject: =?UTF-8?B?5oql56ug5YaF5a655YiG5Lqr?=
X-PHP-Originating-Script: 500:mail.php
MIME-Version: 1.0
X-EsetId: 40C9373366470C695FCF37616E1C4038
Content-type: text/html; charset=UTF-8
From: leo.chan#singtaonewscorp.com <leo.chan#singtaonewscorp.com>
Message-Id: <20130605042928.D8FEE4329E#DPS_FDBD.localdomain>
Date: Wed, 5 Jun 2013 00:29:28 -0400 (EDT)
<html><head></head><body><b>讯息 :</b>leo.chan#singtaonewscorp.com</br></br>分享连结 :</b>按此观看</br></br><img src = "https://s3-ap-southeast-1.amazonaws.com/demosource/ChangSha/2013/05/24/0/0/A/Content/1/Pg001.png" /></body></html>
__________ Information from ESET NOD32 Antivirus, version of virus signature database 8412 (20130604) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com
The above is the mail source code, notice the
Content type ,From: leo.chan#singtaonewscorp.com <leo.chan#singtaonewscorp.com>
Message-Id: <20130605042928.D8FEE4329E#DPS_FDBD.localdomain>
Date: Wed, 5 Jun 2013 00:29:28 -0400 (EDT)
is part of mail message
Here is the php code, I have set the Content type as header , but it seems it cut off after MIME version 1.0 ? How to fix this? thanks.
<?php
function encodeMIMEString ($enc, $string)
{
return "=?$enc?B?".base64_encode($string)."?=";
}
if (isset($_POST["data"])){
// Get posted data
$mailStr = $_POST["data"];
$mailStr = str_replace ('&page','#page',$mailStr);
$mailStr = str_replace ('&issue','#issue',$mailStr);
$info = explode("&", $mailStr);
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0\r\nContent-type: text/html; charset=UTF-8\r\n";
$headers .= "From: $info[0] <$info[0]>";
// Read XML to place the headings in mail content
$xml = simplexml_load_file('lang'.DIRECTORY_SEPARATOR.$info[4].'.xml')
or die("Error: Cannot create object");
$subject = (string)$xml->mailTitle;
$mailMsgDes = (string)$xml->mailMsgDes;
$mailLink = (string)$xml->mailLink;
$mailLinkView = (string)$xml->mailLinkView;
$message = nl2br(htmlentities(trim($info[2]), ENT_QUOTES, "UTF-8"));
$url = str_replace ('#page','&page',$info[3]);
$url = str_replace ('#issue','&issue',$url);
$mailContent = '<html><head></head><body><b>'.$mailMsgDes.' :</b>'.$message.'</br></br>'.$mailLink.' :</b>'.$mailLinkView.'</br>';
if (isset($info[5])){
$mailContent = $mailContent."</br><img src = \"$info[5]\" />";
}
if (isset($info[6])){
$mailContent = $mailContent."</br><img src = \"$info[6]\" />";
}
$mailContent .= '</body></html>';
if (mail($info[1], encodeMIMEString("UTF-8", $subject), $mailContent, $headers))
echo 'Mail sent';
}
?>
Using \r instead of \r\n fixed the problem.
Possibly due to server settings.
Put your FROM header above the MIME-Version and Content-type definition.. Just swap 'em:
// To send HTML mail, the Content-type header must be set
$headers = "From: $info[0] <$info[0]>\r\n";
$headers .= "MIME-Version: 1.0\r\nContent-type: text/html; charset=UTF-8\r\n\r\n";
If you use the PHP_EOL constant, then you don't need to worry about \r or \n or \r\n, and your code would be more portable across servers.
I have to get any text between:
Final-Recipient: RFC822; !HERE! Action
I need !HERE! from this example. There could be any string.
I tried something like:
$Pattern = '/Final-Recipient: RFC822; (.*) Action/';
But it doesn't work.
upd
Here is the string I'm trying to parse: http://dpaste.com/187638/
Since you said "any string" which may contain spaces, the closest approximate would be
$Pattern = '/Final-Recipient: RFC822; (.*?) Action/s';
# ^ ^
# lazy match instead of greedy match ----' |
# allow . to match newline -----'
Of course it won't match "Final-Recipient: RFC822; Action Action".
Your pattern works fine for me:
$i = 'This is a MIME-encapsulated message --o3ONXoEH01blah3:35:33 +0400 (MSD) Final-Recipient: RFC822; !HERE! Action: failed Status: 4.4.7 Lblahru> From: *
#*.ru';
$pattern = '/Final-Recipient: RFC822; (.*) Action/';
$matches = Array();
preg_match($pattern, $i, $matches);
print_r($matches);
Output:
Array
(
[0] => Final-Recipient: RFC822; !HERE! Action
[1] => !HERE!
)
Note also that your pattern will fail if the "any text" contains new lines. Use the DOTALL modifier /.../s to allow the dot to also match new lines. Also note that if the text " Action" appears elsewhere in the message it will cause your regular expression to fail. Matching dot is dangerous. Try to find a more specific pattern if possible.
$Pattern = '/Final-Recipient:[^;]+[;|<|\s]+([^\s|^<|^>]+)/i';
The following expression turned out to be the best for my problems, because sometimes there are lines of the following kind:
Final-Recipient: LOCAL;<example#rambler.ru>
I am going to suggest a method that does not use them, which requires extra busywork.
<?php
$message = 'This is a MIME-encapsulated message --o3ONXoEH016763.1272152184/zvm19.host.ru The original message was received at Fri, 23 Apr 2010 03:35:33 +0400 (MSD) from roller#localhost ----- The following addresses had permanent fatal errors ----- "Flucker" ----- Transcript of session follows ----- 451 grl.unibel.by: Name server timeout Message could not be delivered for 2 days Message will be deleted from queue --o3ONXoEH016763.1272152184/*.host.ru Content-Type: message/delivery-status Reporting-MTA: dns; zvm19.host.ru Arrival-Date: Fri, 23 Apr 2010 03:35:33 +0400 (MSD) Final-Recipient: RFC822; !HERE! Action: failed Status: 4.4.7 Last-Attempt-Date: Sun, 25 Apr 2010 03:36:24 +0400 (MSD) --o3ONXoEH016763.1272152184/zvm19.host.ru Content-Type: message/rfc822 Content-Transfer-Encoding: 8bit Return-Path: Received: (from *#localhost) by *.host.ru (8.13.8/Zenon/Postman) id o3MNZX5h059932; Fri, 23 Apr 2010 03:35:33 +0400 (MSD) (envelope-from *#roller.ru) Date: Fri, 23 Apr 2010 03:35:33 +0400 (MSD) Message-Id: <201004222335.o3MNZX5h059932#*.host.ru> From: *
#*.ru';
$left_delimiter = 'Final-Recipient: RFC822; ';
$right_delimiter = ' Action';
$left_delimiter_pos = strrpos($message, $left_delimiter);
$right_delimiter_pos = strpos($message, $right_delimiter);
$desired_message_fragment = '';
if ($left_delimiter_pos !== false && $right_delimiter_pos !== false) {
$fragment_start = $left_delimiter_pos + strlen($left_delimiter);
$fragment_length = $right_delimiter_pos - $fragment_start;
$desired_message_fragment = substr(
$message, $fragment_start, $fragment_length
);
}
var_dump($desired_message_fragment);
a bit late....
but has been asked in terms of how to solve a problem that is not quite his requirements Op perhaps has joined multiple lines onto one line?(imho).
This might help others....
I'm assuming that op is trying to parse the Final-Recipient header field of a delivery status notification.
The spec for the Final-Recipient field can be seen here: https://www.rfc-editor.org/rfc/rfc3464#page-15
If the problem is broken down, op can pull the final recipient field as a single field (Final recipient followed by a char/blank line on the next line.
e.g.
Original-recipient: rfc822;some-email-that-does-not-exist#gmail.com
Final-recipient: rfc822;some-email-that-does-not-exist#gmail.com
Action: failed
Status: 5.1.1 (Remote SMTP server has rejected address)
Final recipient is followed by the start of the next field, Action which has A on the next line. ie not followed by a space or blank line.
then all he has to do is split the line on ; and take the second part
ie
String[] twoparts = "Final-recipient: rfc822;some-email-that-does-not-exist#gmail.com".split(";",2) // 2 here means (2-1) = 1 match
String email = twoparts[1]