In PHP, I have a string like this:
$string = "user@domain.com MIME-Version: bla bla bla";
How do i get the email address only? Is there any easy way to get the value??
In PHP, I have a string like this:
$string = "user@domain.com MIME-Version: bla bla bla";
How do i get the email address only? Is there any easy way to get the value??
You can use preg_replace to do it.
for emails:
$pattern = "/[^@s]*@[^@s]*.[^@s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);
for urls:
$pattern = "/[a-zA-Z]*[://]*[A-Za-z0-9-_]+.+[A-Za-z0-9./%&=?-_]+/i";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $string);
Resources
PHP manual entry: http://php.net/manual/en/function.preg-replace.php
Credit where credit is due: email regex taken from preg_match manpage, and URL regex taken from: http://www.weberdev.com/get_example-4227.html
Dont need to do any conversion first of all. So you can get rid of $code = (string)$code . '';
and all the strval() calls. You $message can be set as thus:
$message = "Winner information follows:rnEmail: ".$email."rnConfirmation Code: ".$code;
Also, it doesnt look like you are including the code in your second email. So the whole thing changed would look like this:
<?php
$myFile = "winners.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$email = $_POST['email'];
$code = $_POST['code'];
$message = "Winner information follows:rnEmail: ".$email."rnConfirmation Code: ".$code;
fwrite($fh, $email);
fwrite($fh, $code);
mail("loganhsnow@gmail.com", "Winning Notice ST", $message);
mail($email, "Winning Notice CT", 'Congrats, you won a free amazon gift card at logansnow.tk. If the following confirmation code matches the one in our records you will receive your reward. The code follows: '.$code);
fclose($fh);
?>
One other thing to note is that you might want to put $email through some sanitizaiton checks to verify it is a valid email address.
I think you need to use the PropertyAccessor.
$PR_SMTP_ADDRESS = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
$smtpAddress = $recip.PropertyAccessor.GetProperty($PR_SMTP_ADDRESS)
See here (Warning! VBA): https://msdn.microsoft.com/en-us/VBA/Outlook-VBA/articles/obtain-the-e-mail-address-of-a-recipient
This code assumes names are in column A. It further assumes that the name of the address book you are tapping into is named "Contacts", and that they are formatted according to your diagram.
Option Explicit
Private Sub GetAddresses()
Dim o, AddressList, AddressEntry
Dim c As Range, r As Range, AddressName As String
Set o = CreateObject("Outlook.Application")
Set AddressList = o.Session.AddressLists("Contacts")
'Change this range accordingly
Set r = Range("A1:A25")
For Each c In r
AddressName = c.Value
For Each AddressEntry In AddressList.AddressEntries
If AddressEntry.Name = AddressName Then
c.Offset(0, 1).Value = AddressEntry.Address
Exit For
End If
Next AddressEntry
Next c
End Sub
If the addresses are in the Global Address List, In Outlook, go to Tools--> Address Book. Then use the drop-down list to identify which list your addresses are in. Replace "Contacts" in the code with the name of the address book the addresses are stored in.
I didn't write this, I found it on Ozgrid and modified a couple of things to fit your situation. It may take a little tweaking for your application. Hope this helps or gets you going in the right direction.
If you're not sure which part of the space-separated string is the e-mail address, you can split the string by spaces and use
on each substring.