I have multiple emails in one column..
How can send email these email with comma separated?
smith1#gmail.com smith2#gmail.com
$emails = explode(',', $project->emails);
if (!empty($project->emails)) {
Mail::to($emails)->send(new ProjectMail( $project ));
}
coming this error
Swift_RfcComplianceException: Address in mailbox given [smith1#gmail.com smith2#gmail.com] does not comply with RFC 2822, 3.6.2.
First of all, you need to transform this string in an array, that is separated by the comma, using explode() function:
$mails = 'smith1#gmail.com smith2#gmail.com';
$explodedMails = explode(' ', $mails);
This will give you following structure:
=> [
"smith1#gmail.com",
"smith2#gmail.com",
]
Then, you send the email to all the given addresses in $explodedMails array:
Mail::to($explodedMails)->send(new ProjectMail( $project ));
This will send one email to all the addresses from the $explodedEmail array:
I think you can use str_replace and replace space between mail with comma
str_replace("",',',$yourEmailVar);
or you can explode your mails and use cc method for sending mails to all!
Use an explode function and instead of replacing with a comma,replace with the blank space since apparently in the variable mentioned by you,there does not seem to be any commas. Write the code as follows :
$mail_ids = 'smith1#gmail.com smith2#gmail.com';
$mailArray = explode(' ', $mail_ids);
The above explode function will give you an array in proper format like ['smith1#gmail.com','smith2#gmail.com'] and then you can use that array for your purpose of sending email ids.
Related
I am trying to use swiftmailer in my project so that I can send html newsletter to multiple users. I have searched thoroughly but all i got never worked for me. I want to paste more than one recipient in the form input field seperated by comma and send the html email to them.
I set the recipients to a variable($recipients_emails) and pass it to setTo() method in the sending code, same with the html_email.
Questions are:
Q1 How do i send to more than one recipient from the recipient input field.
I tried this:
if (isset($_POST['recipients_emails'])) {
$recipients_emails = array($_POST['recipients_emails'] );
$recipients_emails= implode(',',$recipients_emails);
}
Q2 How do I make the Html within heredoc tag. when i tried concatenating like this ->setBody('<<<EOT'.$html_email.'EOT;', 'text/html'); , my message would appears with the heredoc tag.
if (isset($_POST['html_email'])) {
$html_email = $_POST['html_email'];
}
How do I have input from $_POST['html_email']; to be within EOT tag;
this in part of swiftmailer sending script ;
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipients_emails)
->setBody($html_email, 'text/html');
Nota bene : Am still learning these things.
According to this document
// Using setTo() to set all recipients in one go
$message->setTo([
'person1#example.org',
'person2#otherdomain.org' => 'Person 2 Name',
'person3#example.org',
'person4#example.org',
'person5#example.org' => 'Person 5 Name'
]);
You can input array directly into setTo, setCc or setBcc function, do not need to convert it into string
You should validate the input-data by first exploding them into single E-Mail-Adresses and push the valid Data into an Array. After this you can suppy the generated Array to setTo().
<input type="text" name="recipients" value="email1#host.com;email2#host.com;...">
On Submit
$recipients = array();
$emails = preg_split('/[;,]/', $_POST['recipients']);
foreach($emails as $email){
//check and trim the Data
if($valid){
$recipients[] = trim($email);
// do something else if valid
}else{
// Error-Handling goes here
}
}
Ok so first of all you need to create a variable that form your heredoc contents. The end tag of heredoc has to have no whitespace before it and then use that variable. (to output variables inside heredoc you need to wrap them with curly braces) see example..
$body = <<<EOT
Here is the email {$html_email}
EOT;
$message = Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($recipients_emails)
->setBody($body, 'text/html');
I'm trying to send emails after a post creation but trying to omit sending email to the creator of the post.
I get demo3#demo.com, demo2#demo.com, demo4#demo.com by doing
$emails = rtrim($emails,', ');
I'm trying to remove demo3#demo.com
$owners_email = Session::get('user_email'); // which is demo3#demo.com
$emails = rtrim($emails,$owners_email);
but I get demo3#demo.com, demo2#demo.com, demo4
$emails = join(', ', array_diff(array_map('trim', explode(',', $emails)), [$owners_email]));
In detail, you can do these steps individually if you want:
explode(',', $emails): turn your email list into an array
array_map('trim', ...): remove whitespace from the emails
array_diff(..., [$owners_email]): remove the owner's email from the array
join(', ', ...): collapse the emails back into a list
How about this?
http://php.net/manual/en/function.str-replace.php
str_replace($owners_email.",","",$emails);
I'm trying to use sendgrid to send emails from my app. To add email addresses via php, according to the sendgrid docs, you use the following:
$email = new SendGrid\Email();
$email
->addTo("example#email.com")
where addTo is used for each email address. I would like like to generate the addTo list dynamically, for example I have a list of 3 emails in a php variable that I would like to send mails to:
$email_addresses = 'example#email1.com, example#email2.com. example#email3.com';
I've tried the following to echo out individual ->addTo properties per email address however it doesn't work:
$email = new SendGrid\Email();
$email
$string = $email_addresses;
$string = preg_replace('/\.$/', '', $string); //Remove dot at end if exists
$array = explode(', ', $string); //split string into array seperated by ', '
foreach($array as $value) //loop over values
{
echo '->addTo("'.$value.'")<br>';
}
Does anyone know what I'm doing wrong here?
There's a setTos method in sendgrid-php that handles arrays: https://github.com/sendgrid/sendgrid-php#settos
Using a PHP script file to filter emails. Everything is working properly, but it seems that if I were to initiate a conversation between a cell phone (Verizon Wireless) and the SMTP server, Verizon changes the format of outgoing messages.
For example, I need to respond to an email using XXX#vtext.com but Verizon will respond with XXX#vzwpix.com which cannot be responded to. So I created the following code to try and remove and replace the #vzwpix.com but it still isn't sending the mail. I know the code is working however because if I change the $from in the mail function to XXX#vtext.com the code works and sends the message.
//Parse "from"
$from1 = explode ("\nFrom: ", $email);
$from2 = explode ("\n", $from1[1]);
if(strpos ($from2[0], '<') !== false)
{
$from3 = explode ('<', $from2[0]);
$from4 = explode ('>', $from3[1]);
$from = $from4[0];
}
else
{
$from = $from2[0];
}
if(strpos ($from[1], '#vzwpix.com') !== false) {
str_replace('#vzwpix.com', '#vtext.com', $from);
}
var_dump( mail( $from, "Hi", "What is the email:"));
I am using var_dump just for developmental purposes by the way.
I did some research and I think the best way to do this maybe is to explode the XXX#vzwpix.com at the # symbol, then run a str_replace('vzwpix.com','vtext.com', $from5); However, that didn't work and then I thought maybe implode it after running a str_replace?
If the bad domains are static, why don't you explode on 'vzwpix' and then implode on 'vtext'? This should replace each of the bad instances in you address string.
You forgot to get the result of your str_replace
$from_with_vtext=str_replace('#vzwpix.com', '#vtext.com', $from[1]);
My following script runs fine when a single email address is entered into the textarea, but once two are entered, no emails are sent. What am I doing wrong?
if($_POST['submit']=='Send Email') {
$email_addresses = explode(",\n", $_POST['email']);
foreach($email_addresses as $email_address){
$email_address = trim($email_address);
send_mail( 'noreply#noreply.com',
$email_address,
'Test Email',
"Hello This Email Is A Test");
}
}
var_dump($email_addresses) results in this
array(1) { [0]=> string(39) "email1#test.com email2#test.com" }
You're using the same variable name twice
foreach($email_addresses as $email_addresses)
so on the second loop, the source is overwritten
Edit:
please post the output of
var_dump($_POST['email']);
var_dump(explode(",\n", $_POST['email']));
It should be:
foreach($email_addresses as $email_address){
$email_address = trim($email_address);
send_mail( 'noreply#noreply.com',
$email_address,
'Test Email',
"Hello This Email Is A Test");
}
Also splitting using explode on ",\n" separator isn't a good idea (people can send ",\r\n" in some cases). Provide multiple fields or use preg_split().
If it doesn't work try var_dump($email_address); after the explode() function to get the information what exactly happens with the input (and so you can see that input is actually correct).
UPDATE: As you can clearly see there is no \n in $email_address. It depends on your HTML form.
For a quick fix just explode(', ', $email_addresses); Also - you missed , in your input, which you require to explode that string.
obviously a#b.com b#c.com is not a valid email, and my psychic powers tell me that this is not the correct way to do this. What if I enter something else than an email address? Try to sanitise the data you receive from user. You can not trust user input blindly.
foreach($email_addresses as $email_addresses){
Means that you are overwriting the source array ($email_addresses) with the first entry in the array, because they are the same variable name. Unfortunately, PHP throw an error on this, so you end up rewriting your array with the first email address, and then prematurely (to your needs) exiting the loop (though this is expected and logical behaviour).