Sending E-Mail in PHP - php

I tried almost everything. I just want to send e-mail.
My First Code:
<?php
$to = "yourgmail#gmail.com";
$subject = "HELLO";
$body = "HI!";
try {
mail($to, $subject, $body);
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
It isn't working. No error message, not working.
My Second Code:
function send_mail('mygmail#gmail.com','yourgmail#gmail.com','HELLO','HI!')
{
$headers = '';
$headers .= "From: $from\n";
$headers .= "Reply-to: $from\n";
$headers .= "Return-Path: $from\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "#" . $_SERVER['SERVER_NAME'] . ">\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Date: " . date('r', time()) . "\n";
mail($to,$subject,$body,$headers);
}
It isn't working too.
Please help me :)

Does this work?
First part:
<?php
$to = "yourgmail#gmail.com";
$subject = "HELLO";
$body = "HI!";
$from = "mygmail#gmail.com";
send_mail($to,$from,$subject,$body);
?>
Second part:
<?php
function send_mail($to,$from,$subject,$body) {
$headers = '';
$headers .= "From: $from\n";
$headers .= "Reply-to: $from\n";
$headers .= "Return-Path: $from\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "#" . $_SERVER['SERVER_NAME'] . ">\n";
$headers .= "MIME-Version: 1.0\n"; $headers .= "Date: " . date('r', time()) . "\n";
mail($to,$subject,$body,$headers);
}
?>
I'm not sure if you can send mail via your gmail using php mail function, never looked into it, this should work for name#yourdomain.com

Related

Google Api Gmail - strange emails

I am using this function for send emails.
function sendMessage($to, $subject, $messageBody, $Cc)
{
$service = getGoogleGmailService();
$message = new \Google_Service_Gmail_Message();
$rawMessageString = "From: 'My name' <me>\r\n";
$rawMessageString .= "To: <" . $to . ">\r\n";
if (!empty($Cc)) {
$rawMessageString .= "Cc: <" . $Cc . ">\r\n";
}
$rawMessageString .= 'Subject: =?utf-8?B?' . base64_encode($subject) . "?=\r\n";
$rawMessageString .= "MIME-Version: 1.0\r\n";
$rawMessageString .= "Content-Type: text/html; charset=utf-8\r\n";
$rawMessageString .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
$rawMessageString .= $messageBody . "\r\n";
$rawMessage = strtr(base64_encode($rawMessageString), array('+' => '-', '/' => '_'));
$message->setRaw($rawMessage);
try {
$message = $service->users_messages->send('me', $message);
return $message->getId();
} catch (Exception $e) {
return $e->getMessage();
}
}
In the first photo it is how the email should be structured, in fact everything arrives ok. Every so often, however, I receive a strange email, as in the second photo, as if it were still encoded or something else.
What could it be ?

Mail function always calling the else part

I have two domain name. For example https://example.com and https://example2.com.
I am sending test mail using mail function.
I am able to send the email from https://example.com and I am getting the success but the same code I am using for https://example2.com and I am not getting the email it's always calling the else part.
Test mail
<?PHP
$sender = 'xxx#xx.com';
$recipient = 'zzz#zz.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
$errorMessage = error_get_last()['message'];
echo $errorMessage;
}
?>
The From header is not valid as the next one is concatenated right after it:
$headers = 'From:' . $sender;
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Should be something like:
// add \r\n
$headers = 'From: ' . $sender . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html;charset=UTF-8" . "\r\n";
Try this simple php mail function
$to = $email_id;
$subject = "";
$message = "Your mail Body Content. you also use here HTML Tags for better performence. ";
$headers = 'From: xyz#domainname.com' . "\r\n" .
'Reply-To: xyz#domainname.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers.= "MIME-Version: 1.0" . "\r\n";
$headers.= "Content-type:text/html;charset=UTF-8" . "\r\n";
if(mail($to, $subject, $message, $headers)){
echo "Message accepted";
}else{
echo "Error: Message not accepted";
}
Check if mail function is enabled on example2.com by using below:
if ( function_exists( 'mail' ) )
{
echo 'mail() is available';
}
else
{
echo 'mail() has been disabled';
}
If disabled, enable it via php.ini or in cPanel.

Sending an HTML e-mail with PHP?

I'm trying to send an HTML email with PHP. The body currently looks like this:
$body = "Hello," . "<br />" . "<br />" . "We sent you an e-mail to confirm your account has been "
. "created." . "<br />" . "<br />" . "Thank you!";
Would that properly separate the lines? When I declare my headers, would I also need to use the same format for line breaking?
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: donotreply#webmaster.net";
Would I have to switch those new lines at the end of each header from "\r\n" to "<br />"?
Is better using library but to have an idea try this:
function sendemail( $to, $from, $subject, $title, $html, $cc = "", $bcc = "" )
{
$message = "\n";
$message .= "<!doctype html><html><head><title>$title</title></head><body>";
$message .= $html;
$message .= "</body></html>";
$headers = "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: $from \r\n";
$headers .= "Reply-To: $from \r\n";
$headers .= ( $cc != "" ) ? "Cc: $bcc \r\n" : "";
$headers .= ( $bcc != "" ) ? "Bcc: $bcc \r\n" : "";
return mail( $to, $subject, $message, $headers, "" );
}

PHP: Why email not support html

I try to send an email from my web site, I received email but not support HTML.
Here is my code:-
////////
$headers = "From: $email\n" . "Reply-To: $email\n";
$headers .= "Register: Super market\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
//$header = "From: $email\n" . "Reply-To: $email\n";
$subject = "Register in Super market";
$email_to = EMAIL;
$emailMessage = "<b>NO :</b> " . $aid."\n";
$emailMessage .= "<b>Name :</b> " . $firstname.' '.$fathername.' '.$familyname. "\n";
$emailMessage .= "<b>Tell :</b> " . $tell . "\n\n";
$emailMessage .= "<b>Fax :</b> " . $fax . "\n\n";
$emailMessage .= "<b>Email :</b> " . $email . "\n\n";
//use php's mail function to send the email
#mail($email_to, $subject ,$emailMessage ,$headers );
Update:
Hi every one.
I set an question here and solved successful
But now i need to add some file as attachment.
So, I have 2 variable, That save the file upload into DB like this:-
$filenameword
$filenamezip
How can set this tow variable as file attachment in email inbox.
////////
$headers = "From: $email\n" . "Reply-To: $email\n"; // See the s at the end of $headers
$headers .= "Register: Super market\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
//$header = "From: $email\n" . "Reply-To: $email\n";
$subject = "Register in Super market";
$email_to = EMAIL;
$emailMessage = "<b>NO :</b> " . $aid."\n";
$emailMessage .= "<b>Name :</b> " . $firstname.' '.$fathername.' '.$familyname. "\n";
$emailMessage .= "<b>Tell :</b> " . $tell . "\n\n";
$emailMessage .= "<b>Fax :</b> " . $fax . "\n\n";
$emailMessage .= "<b>Email :</b> " . $email . "\n\n";
//use php's mail function to send the email
mail($email_to, $subject ,$emailMessage ,$headers );
You have a typo in your code.
You are assigning header strings to $headers but you're passing $header to the mail function.
Also please no # before functions. It's a bad practice.
////////
$headers = "From: $email\n" . "Reply-To: $email\n"; // See the s at the end of $headers
$headers .= "Register: Super market\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
//$header = "From: $email\n" . "Reply-To: $email\n";
$subject = "Register in Super market";
$email_to = EMAIL;
$emailMessage = "<b>NO :</b> " . $aid."\n";
$emailMessage .= "<b>Name :</b> " . $firstname.' '.$fathername.' '.$familyname. "\n";
$emailMessage .= "<b>Tell :</b> " . $tell . "\n\n";
$emailMessage .= "<b>Fax :</b> " . $fax . "\n\n";
$emailMessage .= "<b>Email :</b> " . $email . "\n\n";
//use php's mail function to send the email
mail($email_to, $subject ,$emailMessage ,$headers ); // See the s at the end of $headers

Simple script to send email?

I use the code to send email:
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
But I got:
sendmail: fatal: chdir /Library/Server/Mail/Data/spool: No such file or directory
And I don't know how to add attachment and importantly I need to specify the Content-Type for attachment, can anyone help me?
Thanks!
<?php
$to = "someone#example.com";
$subject = "Test mail";
$from = "someonelse#example.com <someonelse#example.com>";
$message = "Hello! This is a simple email message.";
// let's say you want to email a comma-separated-values
$tofile = "col1,col2,col3\n";
$tofile .= "val1,val1,val1\n";
$filename = "filename.csv";
$random_hash = md5(date('r', time()));
$attachment = chunk_split(base64_encode($tofile));
$headers = "From: " . $from . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-MSMail-Priority: Normal\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$body = "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Type: text/plain; charset=\"UTF-8\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= $message;
$body .= "\r\n\r\n";
$body .= "\r\n--PHP-mixed-" . $random_hash . "\r\n\r\n";
$body .= "--PHP-mixed-".$random_hash."\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "Content-Type: text/comma-separated-values; name=\"" . $filename . "\" charset=\"UTF-8\"\r\n";
$body .= "Content-Disposition: attachment; filename=\"" . $filename . "\"\r\n\r\n";
$body .= $attachment;
$body .= "\r\n--PHP-mixed-".$random_hash."--";
mail($to, $subject, $body, $headers);
?>

Categories