Mailhog无法从PHP脚本,localhost,docker,Ubuntu发送消息

ztigrdn8  于 2024-01-06  发布在  Docker
关注(0)|答案(1)|浏览(136)

我的PHP脚本无法向Mailhog客户端发送消息。我使用的是Slim PHP框架。以下是我尝试的方法:-
容器已启动并运行,并设置了sendmail路径:
x1c 0d1x的数据

  1. docker exec -it php cat /usr/local/etc/php/conf.d/local.ini
  2. error_reporting=E_ALL
  3. sendmail_path="mhsendmail --smtp-addr=mailhog:1025"

字符串
本地安装的Sendmail sudo apt-get install sendmail

docker-compose.yaml

  1. version: '3'
  2. services:
  3. #PHP Service
  4. php:
  5. build:
  6. context: .
  7. dockerfile: Dockerfile
  8. container_name: php
  9. restart: unless-stopped
  10. tty: true
  11. environment:
  12. SERVICE_NAME: php
  13. SERVICE_TAGS: dev
  14. working_dir: /var/www
  15. volumes:
  16. - ./:/var/www
  17. - ./docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
  18. networks:
  19. - app-network
  20. #Mailhog
  21. mailhog:
  22. container_name: mailhog
  23. image: mailhog/mailhog
  24. ports:
  25. - "1025:1025"
  26. - "8025:8025"
  27. networks:
  28. - app-network

Dockerfile

  1. FROM php:8.1-fpm
  2. # Copy composer.lock and composer.json
  3. # COPY composer.lock composer.json /var/www/
  4. # Set working directory
  5. WORKDIR /var/www
  6. # Install dependencies
  7. RUN apt-get update && apt-get install -y \
  8. build-essential \
  9. libpng-dev \
  10. libjpeg62-turbo-dev \
  11. libfreetype6-dev \
  12. locales \
  13. libzip-dev \
  14. zip \
  15. jpegoptim optipng pngquant gifsicle \
  16. vim \
  17. unzip \
  18. git \
  19. curl
  20. # Clear cache
  21. RUN apt-get clean && rm -rf /var/lib/apt/lists/*
  22. # Install extensions
  23. RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable pdo_mysql
  24. RUN docker-php-ext-configure gd --with-freetype --with-jpeg
  25. RUN docker-php-ext-install gd
  26. # Install composer
  27. RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
  28. RUN groupadd -g 1000 www
  29. RUN useradd -u 1000 -ms /bin/bash -g www www
  30. # Copy existing application directory contents
  31. COPY . /var/www
  32. # Copy existing application directory permissions
  33. COPY --chown=www:www . /var/www
  34. # Change current user to www
  35. USER www
  36. # Expose port 9000 and start php-fpm server
  37. EXPOSE 9000
  38. CMD ["php-fpm"]


在我的PHP脚本中:

  1. $to = '[email protected]';
  2. $subject = 'Hello from Ubuntu!';
  3. $message = 'This is a Mailhog test';
  4. $headers = "From: [email protected]\r\n";
  5. if (mail($to, $subject, $message, $headers)) {
  6. echo "SUCCESS";
  7. } else {
  8. echo "ERROR";
  9. }


在到达我的端点时,脚本返回ERROR?Any idea?

zpqajqem

zpqajqem1#

使用PHPMailer,因为它集成了SMTP支持-无需本地邮件服务器即可发送。完整代码在这里:

  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. use Psr\Http\Message\RequestInterface;
  4. use Psr\Http\Message\ResponseInterface;
  5. use Slim\Factory\AppFactory;
  6. use PHPMailer\PHPMailer\PHPMailer;
  7. $app = AppFactory::create();
  8. $app->post('/send-email', function (RequestInterface $request, ResponseInterface $response) {
  9. $data = $request->getParsedBody();
  10. $recipient = $data['recipient'];
  11. $subject = $data['subject'];
  12. $body = $data['body'];
  13. $recipientName = $data['recipient_name'];
  14. $mail = new PHPMailer(true);
  15. try {
  16. //Server settings
  17. $mail->isSMTP();
  18. $mail->Host = 'mailhog'; // Set the SMTP server to send through
  19. $mail->SMTPAuth = true; // Enable SMTP authentication
  20. $mail->Username = ''; // SMTP username
  21. $mail->Password = ''; // SMTP password
  22. $mail->Port = 1025; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
  23. //Recipients
  24. $mail->setFrom('[email protected]', 'Mailer');
  25. $mail->addAddress($recipient, $recipientName); // Add a recipient
  26. // Content
  27. $mail->isHTML(true); // Set email format to HTML
  28. $mail->Subject = $subject;
  29. $mail->Body = $body;
  30. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  31. $mail->send();
  32. $response->getBody()->write(json_encode(['message' => 'Email sent successfully']));
  33. return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
  34. } catch (Exception $e) {
  35. $response->getBody()->write(json_encode(['error' => $mail->ErrorInfo]));
  36. return $response->withHeader('Content-Type', 'application/json')->withStatus(500);
  37. }
  38. });
  39. $app->run();

字符串

展开查看全部

相关问题